Loop printing arrays

This is a discussion on Loop printing arrays within the C Programming forums, part of the General Programming Boards category; Hi everyone. I have the following little function: Code: int main() { int counter; int array[9] = { 1, 2, ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    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!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Awesome!! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 12-31-2005, 12:56 PM
  2. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 11:40 AM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Printing back arrays
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21