hacks4life
hacks4life

Reputation: 693

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

Upvotes: 2

Views: 133138

Answers (6)

صياد
صياد

Reputation: 11

The code you've provided is a C program that creates and prints a specific pattern in a 5x5 matrix. Here's a breakdown of what the code does:

  1. Declaration of a 5x5 Array: The line int a[5][5]; declares a two-dimensional array a with 5 rows and 5 columns.

  2. Nested Loops to Fill the Array: The nested for loops iterate over the array indices. The outer loop with i goes over each row, and the inner loop with j goes over each column.

  3. Filling the Array with Values:

    • If j == 0, the first column of each row is set to the row number plus one (i+1).
    • If j is less than or equal to i (which means the current column index is less than or equal to the current row index), the current element is set to the value of the previous column in the same row minus one (a[i][j-1]-1).
    • If j is greater than i, the current element is set to 1.
  4. Printing the Array: Another set of nested for loops is used to print the array. The \t in printf("%d\t",a[i][j]); adds a tab space between the numbers for better readability.

  5. Output: The program will print a matrix where the first column of each row starts with the row number plus one and decrements by one until it reaches 1, after which all remaining values in the row are 1.

Here's what the output of the program looks like:

1   1   1   1   1   
2   1   1   1   1   
3   2   1   1   1   
4   3   2   1   1   
5   4   3   2   1   

#include<stdio.h>
int main()
{
    int a[5][5];
    int j,i;
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++){
            if (j == 0){
                a[i][j] = i+1;
            }else if (j<=i){
                a[i][j] = a[i][j-1]-1;
            }else{
                a[i][j]=1;
            }
        }
    }
    for (i = 0;i<5;i++){
        for (j=0;j<5;j++){
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }
    
    return 0 ;
} 

Upvotes: 1

Geovani Lopes Dias
Geovani Lopes Dias

Reputation: 3

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

Upvotes: 0

Mihai8
Mihai8

Reputation: 3147

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

Upvotes: 11

jim gitonga
jim gitonga

Reputation: 128

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

}

Upvotes: 0

Devil5
Devil5

Reputation: 1

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.

Upvotes: 0

smamusa
smamusa

Reputation: 31

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

Upvotes: 1

Related Questions