0

I am trying to print a 2d array that has a maximum of 3 digit numbers that are aligned when printed. For example, with a simple printf, it looks like this:

[0, 232, 20, 96, 176, 0, 0]
[0, 0, 24, 0, 0, 176, 0]
[0, 0, 0, 0, 0, 0, 0]

I would like it to be printed with all the commas aligned along the columns with additional whitespace, like this:

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]

How can I do this with printf?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
rookie
  • 1,168
  • 3
  • 14
  • 25
  • 2
    Any decent book or tutorial should have information about the field-width modifiers of [`printf`](https://en.cppreference.com/w/c/io/fprintf). – Some programmer dude Mar 20 '20 at 17:56
  • try using printf("%4d",array[index][index]); so that it reserves 4 spaces irrespective of 2 or 3 digits number – Sudarshan Mar 20 '20 at 17:57

3 Answers3

2

Here you are.

#include <stdio.h>

int main(void) 
{
    enum { M = 3, N = 7 };
    int a[M][N] =   
    {   
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 }
    };

    int width = 4;

    for ( size_t i = 0; i < M; i++ )
    {
        putchar( '[' );
        for ( size_t j = 0; j < N; j++ )
        {
            if ( j != 0 ) putchar( ',' );
            printf( "%*d", width, a[i][j] );
        }
        printf( "]\n" );
    }

    putchar( '\n' );

    return 0;
}

The program output is

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]

You can change the value of the variable width if you want for example to output larger values than values consisting from 3 digits. That is the output is enough flexible.

If you want to place the array output in a separate function then the corresponding function can look the following way provided that the compiler supports variable length arrays.

#include <stdio.h>

void format_output( size_t m, size_t n, int a[m][n], int width )
{
    for ( size_t i = 0; i < m; i++ )
    {
        putchar( '[' );
        for ( size_t j = 0; j < n; j++ )
        {
            if ( j != 0 ) putchar( ',' );
            printf( "%*d", width, a[i][j] );
        }
        printf( "]\n" );
    }
}

int main(void) 
{
    enum { M = 3, N = 7 };
    int a[M][N] =   
    {   
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 }
    };

    int width = 4;

    format_output( M, N, a, width );

    putchar( '\n' );

    return 0;
}

The program output is the same as shown above that is

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You can use the width prefix to specify the minimum width for the printf conversions: printf("%4d", x); will print int variable x padded to the left with enough spaces to produce at least 4 characters.

If you know the maximum width of any number in the array, you can hardcode this number in the format string. Otherwise you can compute the required width and use %*d and pass an extra argument to specifying the computed width.

Here is a modified version:

#include <stdio.h>

int main(void) {
#define M  3
#define N  7
    int a[M][N] = {
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 },
    };

    int width = 0;

    /* compute the required width */
    for (size_t i = 0; i < M; i++) {
        for (size_t j = 0; j < N; j++) {
            int w = snprintf(NULL, 0, "%d", a[i][j]);
            if (width < w) {
                width = w;
            }
        }
    }

    /* print the arrays */
    for (size_t i = 0; i < M; i++) {
        printf("[");
        for (size_t j = 0; j < N; j++) {
            if (j != 0) printf(", ");
            printf("%*d", width, a[i][j]);
        }
        printf("]\n");
    }

    return 0;
}

Output:

[  0, 232,  20,  96, 176,   0,   0]
[  0,   0,  24,   0,   0, 176,   0]
[  0,   0,   0,   0,   0,   0,   0]
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

use this format of printf printf("%4d", arr[i][j]);

int main()
{
    int arr[3][7] = { {0, 232, 20, 96, 176, 0, 0}
    ,{0, 0, 24, 0, 0, 176, 0}
    ,{0, 0, 0, 0, 0, 0, 0} };
    for (int i = 0; i < 3; i++)
    {
        printf("[");
        for (int j = 0; j < 7; j++)
        {
            if (j < 6)
                printf("%4d,", arr[i][j]);
            if (j == 6)
                printf("%4d", arr[i][j]);

        }
        printf("]");
        printf("\n");
    }
}

PS:amount of space can be changed as needed ,with changing "%4d".

hanie
  • 1,863
  • 3
  • 9
  • 19
  • But where are the square brackets and commas? – Ardent Coder Mar 20 '20 at 18:03
  • 1
    @ArdentCoder sorry I forgot to add them, Thanks for reminding. I edited my post. – hanie Mar 20 '20 at 18:08
  • However, this time you have too many spaces because of **%6d**. Adjust that according to what OP has demonstrated. – Ardent Coder Mar 20 '20 at 18:10
  • Okay, but now what do you mean by "PS:amount space can't be changed as needed with changing "%4d"? Did you mean **can**? – Ardent Coder Mar 20 '20 at 18:17
  • 1
    @ArdentCoder Thanks for all comments ,it seems like I'm not typing and seeing well today. – hanie Mar 20 '20 at 18:19
  • Haha, no worries. I will leave an upvote, but there is still some scope of improvement. You can give attention to grammar and explaining the logic in comments. Moreover, take care of formatting and structuring your answer. And you need not thank me for the comments because I see you actively helping many people out here. So, consider my comments as a token of appreciation in order to improve your answers :) – Ardent Coder Mar 20 '20 at 18:20
  • 1
    @ArdentCoder I hope to learn more about programming with help of users of this site ,and I will always be happy to improve my codes with helpful comment.and I appreciate them a lot. – hanie Mar 20 '20 at 18:37