C语言 如何在C中显示矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14166350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a matrix in C
提问by hacks4life
I have a matrix.txt file wherein there is a matrix written this way :
我有一个 matrix.txt 文件,其中有一个这样写的矩阵:
1 2 3
4 5 6
7 8 9
I need to write a little C program that take this file as input and print this matrix in the same way as the .txt file.
我需要编写一个小的 C 程序,将这个文件作为输入并以与 .txt 文件相同的方式打印这个矩阵。
That means when the outpout of "./a.out matrix.txt" has to be exactly what's in my .txt file :
这意味着当“./a.out matrix.txt”的输出必须是我的 .txt 文件中的内容时:
1 2 3
4 5 6
7 8 9
My problem is that all that I can do is this function:
我的问题是我能做的就是这个功能:
void printMatrice(matrice) {
int x = 0;
int y = 0;
for(x = 0 ; x < numberOfLines ; x++) {
printf(" (");
for(y = 0 ; y < numberOfColumns ; y++){
printf("%d ", matrix[x][y]);
}
printf(")\n");
}
}
But this is not good at all.
但这一点都不好。
Anyone has an idea ?
有人有想法吗?
Thanks
谢谢
回答by Mihai8
Try this simple code
试试这个简单的代码
int row, columns;
for (row=0; row<numberOfLines; row++)
{
for(columns=0; columns<numberColumns; columns++)
{
printf("%d ", matrix[row][columns]);
}
printf("\n");
}
回答by Stjepan Mamu?a
I modified user1929959's code a little bit since I had some weird prints. If you like you can try copy-paste this code and see how it runs. Just a n00b student here. Hope I helped a bit (I'm struggling too) ;)
我修改了 user1929959 的代码,因为我有一些奇怪的打印。如果您愿意,可以尝试复制粘贴此代码并查看它是如何运行的。这里只是一个n00b学生。希望我有所帮助(我也很挣扎);)
MATRIX PRINT CODE
矩阵打印代码
void main ()
{
int matrix [3][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int row, column=0;
for (row=0; row<3; row++)
{
for(column=0; column<4; column++)
{printf("%d ", matrix[row][column]);}
printf("\n");
}
getchar();
}
回答by Devil5
simply what you need to add is: put //printf("\n"); in loop,that is responsible of printing of ROWS.so that, \n:it will change the row after complition of each row.
简单地你需要添加的是: put //printf("\n"); 在循环中,负责打印 ROWS.so,\n:它会在每行完成后更改行。
回答by jim gitonga
here is how to do it
这是如何做到的
#include <stdio.h>
#include <stdlib.h>
void main()
{
int matrix[3][3]={{1,2,3},
{4,5,6},{7,8,9}};
int columns,rows;
for(columns=0;columns<=2;columns++){
for(rows=0;rows<=2;rows++){
printf(" %d " ,matrix[columns][rows]);
}
printf("\n");
}
}
}

