【发布时间】:2014-05-05 16:32:25
【问题描述】:
大家好,我正在尝试将矩阵存储在字符数组中,然后将其打印出来。
我写的代码:
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
int j;
int row=0;
int col=0;
int temp=0;
char c;
int array[3][2] = {{}};
while((c=getchar()) !=EOF && c!=10){
if((c==getchar()) == '\n'){
array[col++][row];
break;
}
array[col][row++]=c;
}
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
printf("%c ", array[i][j]);
}
printf("\n");
}
}
使用文本文件,例如:
1 2 3 4
5 6 7 8
9 1 2 3
我希望能够将其打印回给用户,但是我的代码输出的是:
1 2 3 4
3 4 5 6
5 6 7 8
我无法弄清楚我的代码出了什么问题,我无法弄清楚我是如何在一个循环中退出迭代的,或者它与没有正确处理新行有关。谢谢!
【问题讨论】:
-
char数组在哪里?为什么
char c?为什么不int c? C 中不允许使用 AFAIK 空初始化程序 (int array[3][2] = {{}};)。 -
你能向我解释一下我的代码是如何工作的吗?此外,当我更改为 int c 并打印出 int 而不是 chars 时,我相信它打印的是地址编号,而不是存储在每个数组中的元素。
-
应该是
array[3][4]
标签: c arrays matrix printing io