【发布时间】: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。