【问题标题】:Matrix Output is garbage data矩阵输出是垃圾数据
【发布时间】:2014-03-11 00:22:12
【问题描述】:

所以,我在这里要做的是打印出一个矩阵或只是一个数组。现在,输入文件有一组整数。我的问题是,当我输出矩阵时,它是一堆随机数。它也是个位数,所以我认为它不像那些与指针相关的地址。输入文件中的第一个整数应该是数组长度,或者我在这里称之为“col”。

 #define MAX 10
 int main()
 {
    int col,row; /* array dimensions */
    int i,j,k; /* counter variables */
    int n; /* temporary variable for elements*/
    int temp[MAX*MAX]; /* temporary array for row counting */
    int counter=0; /* counter variable for number of rows */
    int matrix[MAX][MAX]; /* input matrix */
    FILE *in; /* pointer for input file of elements */

/* INPUT */
printf("\nPlease enter your desired number of columns and the elements of your matrix.\n");
fscanf(in,"%d",&temp[0]); /* Enter matrix length */

col = temp[0]; /* assign integer value to col variable */

if( col < 1 || col > MAX){ /* Standard Error for invalid input */
    fprintf(stderr,"\nInvalid Length\n");
    exit(1);
}

/* PROCESSING */

while(fscanf(in,"%d",&n)!=EOF){ /* determine number of rows */
    temp[counter++]=n;
}

if(counter % col == 0){ /* standard error for exceeding/lesser amount of input*/
    fprintf(stderr,"\nInvalid amount of input\n");
    exit(1); /* quits out the program for error */
}
else{
    row = counter / col; /* determine number of rows */
}

for(i = 0; i < row; i++){ /* read the elements */
    for(j = 0; j < col; j++){
        for(k=1; k < counter; k++){
            matrix[i][j] = temp[k];
        }
    }
}

/* OUTPUT */

fprintf(stdout,"\nHere is your matrix:"); /* Matrix Header */

for(i=0; i < row; i++){ /* print out the input matrix */
    fprintf(stdout,"\n");
    for(j = 0; j < col; j++){
        fprintf(stdout,"%3d ",matrix[i][j]);
    }
}
fprintf(stdout,"\n");

输入: 2 1 2 3 4 5 6

输出: 请输入您想要的列数和矩阵的元素。

Here is your matrix:
  6   6
  6   6
  6   6

^ 现在我不知道我们的 UNIX 系统是在告诉我一些关于我自己的事情还是什么。我现在很害怕。

编辑:好的,我删除了代码行之间的 cmets。 编辑:我编译并测试了程序。

【问题讨论】:

  • 你能发布你的完整程序而不是分解它吗?
  • 你能把输入和输出贴出来吗?
  • @reign_man 我修好了,下次试试suggesting an edit

标签: c arrays matrix output


【解决方案1】:

问题来了,每个矩阵 i,j 都会得到相同的 k。

   for(i = 0; i < row; i++){ /* read the elements */
        for(j = 0; j < col; j++){
            for(k=1; k < counter; k++){
                matrix[i][j] = temp[k];
            }
        }
    }

change it to,

int count=0; 
   for(i = 0; i < row; i++){ /* read the elements */
        for(j = 0; j < col; j++){
                count++;
                matrix[i][j] = temp[count];
        }
    }

【讨论】:

  • 哦,是的!我现在注意到了。感谢修复一切
【解决方案2】:

如果这是完整的程序,您似乎从未fopen()ed 文件。

for(i = 0; i < row; i++){ /* read the elements */
    for(j = 0; j < col; j++){
        for(k=1; k < counter; k++){
            matrix[i][j] = temp[k];
        }
    }
}

你的问题。矩阵有多少维?您使用了多少个 for 循环?没错。

因为对于最内层循环的每次迭代 i 和 j 保持不变,而 k 从 1 变为计数器,你正在一遍又一遍地重写 matrix[i][j],直到你总是达到相同的结束值ķ。您的代码相当于:

for(i = 0; i < row; i++){ /* read the elements */
    for(j = 0; j < col; j++){
        matrix[i][j] = temp[counter-1];
    }
}

【讨论】:

  • 哦,我确实打开了文件。我想在这篇文章中我忘了添加它。这篇文章最初被剪裁成我认为重要的部分来表达我的观点。不过还是谢谢。
  • 但我说的和其他人说的完全一样?
  • 哦,是的,对不起,我从下往上阅读了这些页面。我已经习惯了我在论坛和 reddit 上的阅读风格。
猜你喜欢
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多