2

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

abelenky
  • 63,815
  • 23
  • 109
  • 159
hacks4life
  • 691
  • 5
  • 16
  • 38

5 Answers5

11

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");
}
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Mihai8
  • 3,113
  • 1
  • 21
  • 31
1

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();
}
Community
  • 1
  • 1
smamusa
  • 31
  • 7
0

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.

Devil5
  • 1
0

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");
}

}

0

I know this question is old, but struggling with the same problem, I've tried to write a function for it; after some try-and-error, my solution must get its order passed as parameters; this way, it could work with any number of rows and columns:

void printMatrix(int row, int col, int m[raw][col]) {
    int i, j;
    for (i = 0; i < row; i++) {
        printf("%d-line: ", i + 1);
        for (j = 0; j < col; j++) {
            printf("%d ", m[i][j]);
        }
        printf("\n");
    }
 }
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 11 '23 at 18:59
  • Just why? How exactly I was unclear? – Geovani Lopes Dias Aug 17 '23 at 23:27