【发布时间】:2015-11-13 17:02:29
【问题描述】:
我正在尝试从键盘接收用户输入,以填充矩阵。到目前为止,这是我想出的,我不确定为什么它不输出用户输入。
编辑:我将 for 循环中的 [nRows][nCols] 更改为实际循环的适当值。 (k,o),(i,j)。根据用户的建议,还将变量名称 o 更改为 p,因为 o 类似于 0。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nRows;
int nCols;
//Setting up the Grid
printf("Enter number of Rows: \n");
scanf("%i",&nRows);
/*
for(int k = 0; k <= nRows; ++k){
printf("Fill Rows: \n");
scanf("%i",&nRows);
}
*/
printf("Enter number of Columns: \n");
scanf("%i",&nCols);
/*
for(int k = 0; k <= nCols; ++k){
printf("Fill Cols: \n");
scanf("%i",&nCols);
}
*/
int matrix[nRows][nCols];
for(int k = 0; k < nRows; ++k){
for(int p = 0; p < nCols; ++p){
printf("Enter value for Matrix[%i][%i]: ",nRows,nCols);
scanf("%i",&matrix[k][p]);
}
}
for(int i = 0; i < nRows; ++i){
for(int j = 0; j < nCols; ++j){
printf("%i\t",matrix[i][j]);
}
printf("\n");
}
return 0;
//col[n]+row[n] == userinput
}
【问题讨论】:
-
在输入和输出循环中将
matrix[nRows][nCols]更改为matrix[i][o]和matrix[i][j]。顺便说一句,o是一个错误的变量名选择,因为它类似于0 -
@EugeneSh。谢谢你。问题解决了
-
@robinhood46 请将您的编辑添加为答案并将其标记为已接受。它可以帮助人们轻松地看到问题已解决。
-
在调用
scanf()函数时,一定要检查返回值(不是参数值),保证操作成功 -
这一行:
printf("Enter value for Matrix[%i][%i]: ",nRows,nCols);会使用户混淆他们输入的内容。建议:printf("Enter value for Matrix[%i][%i]: ", k, p);