Thread: Printing array

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Printing array

    Hey, I am trying to print a 2D (3x3) array like this:
    Code:
    1 2 3
    4 5 6
    7 7 7
    I tried a while loop but it didnt work of course, could someone help be make it print like the above?
    Code:
    #include <stdio.h>
    #define NUM_ELEMENTS 3
    
    int main()
    {
    	int x[NUM_ELEMENTS][NUM_ELEMENTS]; //Declaration of the array
    	//int y[NUM_ELEMENTS][NUM_ELEMENTS];
    	//int z[NUM_ELEMENTS];
    
    	for(int a=0; a<NUM_ELEMENTS; a++)
    	{
    		for(int b=0; b<NUM_ELEMENTS; b++)
    		{
    			printf("Enter row &#37;d and column %d: \n",a,b);
    			scanf("%d",&x[a][b]);
    		}
    	}
    
    	printf("\n"); //Space
    	//int loop=0;
    
    	for(int i=0; i<NUM_ELEMENTS; i++)
    	{
    		for(int j=0; j<NUM_ELEMENTS; j++)
    		{
    			//while(loop != 3)
    			//{
    			printf(" %d ",x[i][j]); //Printing of array contents
    			//loop++;
    			//}
    		}
    	}
    }
    Right now it prints like this:
    Code:
    1
    2
    3
    4
    5
    6
    7
    7
    7
    Thanks
    Last edited by taurus; 09-04-2008 at 05:48 AM.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    you should print the 2-d array like as below
    Code:
    for(i = 0; i < NUM_ELEMENTS;i++) {
               for(j = 0; j < NUM_ELEMENTS;J++)
                            printf("&#37;d\t", x[i][j]);
               printf("\n");
    }
    now out put of your program will be in ur format..

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Ah yeah of course. Simple.

    Thanks

  4. #4
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Are the single-line comment identifiers // considered acceptable under c99 ANSI standard? They are used in C++, correct? Also, are you just leaving some of your code out ( ie variable initializers?) or can // be used outside of commenting ( as you have //int loop = 0)? I am just learning and this is unfamiliar to me, so I wanted to ask.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, // comments are C99 and C++. But not C89, but most compilers support them as a compiler extension (or just support them anyway...).

    No, // doesn't mean anything but a comment. Although / means divide, // is not double divide or anything

    Code:
    for(int i=0; i<NUM_ELEMENTS; i++)
    Isn't C89 anyway...

  6. #6
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Okay, but what about the second part of my query? Are we just generalizing the code to get to the point of the original question or are the // being used to initialize loop?
    Sorry, if I'm being daft, but I'm not going to learn if I don't ask.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No... I edited my post to include that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM