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]