hacks4life hacks4life - 2 months ago 14
C Question

How to display a matrix in C

I have a matrix.txt file wherein there is a matrix written this way :

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.

That means when the outpout of "./a.out matrix.txt" has to be exactly what's in my .txt file :

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

Answer

Try this simple code

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