How to display a matrix in C
100,408
Solution 1
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");
}
Solution 2
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) ;)
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();
}
Author by
hacks4life
Updated on April 27, 2020Comments
-
hacks4life about 4 years
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