-
Loop printing arrays
Hi everyone. I have the following little function:
Code:
int main()
{
int counter;
int array[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for( counter = 0; counter <= 8; counter++ ) {
printf( "[%d]\n", array[counter] );
}
return 0;
}
It outputs the following:
Code:
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
That output is great but I would like to know how to get the output so that it is as follows:
Code:
[1][2][3]
[4][5][6]
[7][8][9]
Any help is greatly appreciated. Thanks!
-
Code:
int main()
{
int counter;
int array[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for( counter = 0; counter <= 8; counter++ )
{
printf( "[%d] ", array[counter] );
if((counter+1) % 3 ==0)
printf("\n");
}
getchar();
return 0;
}
/*my ouput
[1] [2] [3]
[4] [5] [6]
[7] [8] [9]
*/
ssharish2005
-