Home > Software engineering >  How to format a matrix in c with specific additional symbols?
How to format a matrix in c with specific additional symbols?

Time:09-07

I want to make a function that formats a matrix that's passed like this:

1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45

Into a matrix like this:

1  2  3  | 6
4  5  6  | 15
7  8  9  | 24
=============
12 15 18 | 45

I somewhat got the part with vertical bars, but I have no idea how to do the equals part, here's my take on vertical bar:

for (int i = 0; i < n; i  )
    for (int j = 0; j < n; j  )
        if (j == n - 2)
            printf("%d | ", mat[i][j]);
        else if (j == n - 1)
            printf("%d\n", mat[i][j]);
        else if (i == n - 1)
            printf("%d  ", mat[i][j]);
        else printf("%d   ", mat[i][j]);

Printing === before the last row just ends up like this:

1  2  3 | 6
4  5  6 | 15
7  8  9 | 24
===
12 ===
15 18   | 45

I tried everything, but it failed miserably, any suggestions?

CodePudding user response:

Here is demonstrated a straightforward approach to output your array.

#include <stdio.h>

int main( void ) 
{
    enum { N = 4 };
    int a[N][N];

    for ( int i = 0; i < N; i   )
    {
        a[i][N-1] = 0;
        for ( int j = 0; j < N - 1; j   )
        {
           a[i][N-1]  = a[i][j] = i * N   j   1; 
        }
    } 

    for ( int i = 0; i < N; i   )
    {
        if ( i == N - 1 )
        {
            for ( int j = 0; j < 3 * N   1; j   )
            {
                putchar( '=' );
            }
            putchar( '\n' );
        }

        for ( int j = 0; j < N; j   )
        {
            if ( j != N - 1 )
            {
                printf( "%-3d", a[i][j] );
            }
            else
            {
                printf( "| %-2d", a[i][j] );
            }
        }

        putchar( '\n' );

    }  
}

The program output is

1  2  3  | 6 
5  6  7  | 18
9  10 11 | 30
=============
13 14 15 | 42

You can write a more general function that outputs such an array by using the approach. What you need to do so is to calculate the length of the last element of the array (in the array above the length of the last element 45 is equal to 2) and instead of such a call of printf

printf( "%-3d", a[i][j] );

to use

printf( "%-*d", len   1, a[i][j] );

CodePudding user response:

so I did a little of modifications on your code like , you outer for loop must loop extra one time to print that = symbol , that's why I used that flag called printedHorizontalLineFlag so that not to go out of array boundaries.

and here is the full edited code :

    #include <stdio.h>

void func(int column, int rows, int mat[rows][column])
{
    int printedHorizontalLineFlag = 0;

    for (int i = 0; i < rows; i  )
    {
        for (int j = 0; j < column; j  ) {
            if (j == column - 2) {
                printf("%d\t| ", mat[i][j]);
            }
            else if (j == column - 1) {
                printf("%d\n", mat[i][j]);
            }
            else if (printedHorizontalLineFlag == 1 && i == rows - 1) {
                printf("%d\t", mat[i][j]);
            }
            else if (printedHorizontalLineFlag == 0 && i == rows - 1) {
                printf("============================\n");
                i--;
                printedHorizontalLineFlag = 1;
                break;
            }
            else {
                printf("%d\t", mat[i][j]);
            }
        }
    }

}

int main()
{
    int mat[][4] = {{1, 2, 3, 6},
                   {4, 5, 6, 15},
                   {7, 8, 9, 24},
                   {12, 15, 18, 45}};
    func(4, 4, mat);

    return 0;
}

and here is image of the output :

enter image description here

  • Related