【问题标题】:Output of the c program comes to be zero while it takes input from user当 c 程序接受用户输入时,它的输出为零
【发布时间】:2015-01-26 18:25:05
【问题描述】:

我发布了要求用户输入矩阵的代码部分,但在显示它时仅显示 0.000000 作为其元素。请帮忙找出我在哪里做的不对。

#include<stdio.h>
main(){

int i,j,m,x,c,l;
double mat[100][100],temp[100],a[100][100],lt[100][100]={0.000},ut[100][100],maximum,per[100][100];
printf("Enter the size of square matrix: ");
scanf("%d", &m);

//enter the matrix
for(i=0;i<m;i++)
  {
printf("enter the row %d: \n", i+1);
for(j=0;j<m;j++){
scanf("%f",&mat[i][j]);
a[i][j]=mat[i][j];
}
  }

//show matrix
  printf("matrix =\n");
for(i=0;i<m;i++)
{
 for(j=0;j<m;j++)
 {
  printf("%f\t", mat[i][j]);
 }
  printf("\n");
}

【问题讨论】:

  • 打开编译器警告,您将了解到您需要scanf 中的%lf 才能扫描double。 (但%f 用于打印 double 很好。)
  • 谢谢它解决了问题。

标签: c arrays matrix printf scanf


【解决方案1】:

使用 lf 扫描双精度值。使用错误的格式说明符会导致未定义的行为

【讨论】:

  • 另外 OP 应该使用更具描述性的名称然后 i or j or c
  • @Rizier123 是的,我同意,但看到 0 的原因是这个
  • 谢谢。 i, j, c 在程序中用作计数器,因此我没有使用任何描述性名称。这只是代码的一部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 2015-10-28
  • 2014-08-19
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多