Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a matrix in C

Tags:

c

printing

matrix

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

like image 786
hacks4life Avatar asked Jan 30 '26 18:01

hacks4life


1 Answers

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");
}
like image 158
Mihai8 Avatar answered Feb 02 '26 09:02

Mihai8