如何在C中显示矩阵

hac*_*ife 0 c printing matrix

我有一个matrix.txt文件,其中有一个以这种方式编写的矩阵:

1 2 3

4 5 6

7 8 9
Run Code Online (Sandbox Code Playgroud)

我需要编写一个小程序,将该文件作为输入,并以与.txt文件相同的方式打印此矩阵.

这意味着当"./a.out matrix.txt"的输出必须与我的.txt文件完全相同时:

1 2 3

4 5 6

7 8 9
Run Code Online (Sandbox Code Playgroud)

我的问题是我所能做的就是这个功能:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

但这根本不好.

有人有想法吗?

谢谢

Mih*_*ai8 7

试试这个简单的代码

int row, columns;
for (int row=0; row<numberOfLines; row++)
{
    for(int columns=0; columns<numberColumns; columns++)
        {
         printf("%d     ", matrix[row][columns]);
        }
    printf("\n");
 }
Run Code Online (Sandbox Code Playgroud)

  • 有趣还是有趣? (2认同)