[英]Writing a 2-dimensional array table in C. How would I go about doing this. About neatness (such as dividing lines) as well
我需要写一个程序
定义一个名为settings的二维数组,其大小,大小和大小足以容纳下表:
0 1 2 3 4 10 11 12 13 14 20 21 22 23 24
用表中的值初始化数组
在这个领域,我曾被教过一些Java,但实际上很难输出数字。 我相信数组中的所有内容都是正确的,并且我正在尝试使用if语句输出。 任何方法都行得通,我需要学习如何以文字形式输出表格。 同样在示例中,他将每个数字都整齐地“装箱”。 这是可能的,还是仅是我上面给出的示例?
这是到目前为止我一直在处理的代码。
main() {
#define column 5
#define row 3
int i = 0;
int j = 0;
int table[row][column] =
{
{0, 1, 2, 3, 4},
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24}
};
if(i<3) {
if(j<5) {
return table[i][j];
j++;
}
i++;
}
return 0;
}
我已经测试过了,它输出正确的表
# include <stdio.h>
int main()
{
static const int column = 5;
static const int row = 3;
int table[row][column] =
{
{0, 1, 2, 3, 4},
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24}
};
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < column; ++j)
{
printf("%d ", table[i][j]);
}
printf("\n");
}
getchar(); // this means you have to press enter to exit the console
return 0;
}
试试这个,它输出一个带有标题和或多或少带框的表:
# include <stdio.h>
int main(){
int table[3][5] =
{
{0, 1, 2, 3, 4},
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24}
};
printf(" | 1 | 2 | 3 | 4 | 5\n----------------------------\n");
for(int i = 0; i < 3; ++i)
{
printf("%d |",i);
for(int j = 0; j < 5; ++j)
{
if(table[i][j] > 9)
{
printf(" %d |", table[i][j]);
} else {
printf(" %d |", table[i][j]);
}
}
printf("\n----------------------------\n");
}
getchar(); // good idea from Joe
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.