Thread: printing contents of array vertically

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    printing contents of array vertically

    Hi,
    I have written a program from a book I'm learning from. Everything works except for the end when the contents of an array is to be displayed in a table.
    Code:
    #include <stdio.h>
    
    main()
    
    {
    	int sweatshirts[7][4],
                        size,      /*index*/                                                   
                    college;    /*index*/
    
    
    	for(college=0; college<7; ++college)
    	{
    		printf("\nPlease enter the number of sweatshirt sizes for college %d." , college+1);
    			for(size=0; size<4; ++size)
    			{
    				printf("\nSize(%d): " , size+1);
    					scanf("%d" , &sweatshirts[college][size]);
    			}
    
    	}
    
    
    					for(college=0; college<7; ++college)
    					{
    						
    						for(size=0; size<4; ++size)
    						{
    							
    							printf("\n%d " , sweatshirts[college][size]);
    /*this loop is just to test the output numbers are correct*/	
    							if(size == 3)
    							printf("\n");					
    							
    						}
    					}
    
    	
    
    /*output numbers will go in this table in columns under 1-7*/
    
          /*printf("\n                                      College                  \n");
    	printf("\n                       1     2     3     4     5     6     7    Size total        ");
    	printf("\n");
    	printf("\n        Small\n");
    	printf("\n       Medium\n");
    	printf("\nSize\n");
    	printf("\n        Large\n");
    	printf("\n      X_Large\n");
    	printf("\nCollege total\n");
    	printf("\nTotal quantity on hand\n");  */
    	
    			
    
    }
    The table is on the bottom in /*comments*/. The last for loop is just a test to see if the output is correct. But it will eventually be in the table. Problem is I cant get each "size" for each of the 7 "stores" to print out vertically under each numbered column like:
    1 3
    2 4
    3 5
    4 6 I have tried using /t in the for loop to try and make the numbers start in a new position to the right, but no luck.
    Last edited by richdb; 03-08-2006 at 10:25 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    for(Size = 0 to 3)
    {
      int TotalAmount = 0;
    
      for(College = 0 to 6)
      {
        TotalAmount += sweatshirts[College][Size];
      }
    
      printf("%d %d\n", Size, TotalAmount);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    That didn't work out. I'm not sure you meant for the TotalAmount to accumulate the contents of the array.
    Code:
     TotalAmount += sweatshirts[College][Size];
    It gave this:
    4 4
    4 8
    4 12
    4 16
    4 20
    4 24
    4 28 This is what I'm trying to do: Each vertical column under 1-7 is the number of sizes small through xlarge for that particular college, top to bottom. Numbers 1-7 represent the different colleges. All of my original code works correctly, I just need to get the output into this table in vertical rows for each college.

    Code:
                                        college
                           1      2      3      4       5      6      7
    ------------------------------------------------------------------------------
    small                  5      4      2      0      9      7      1
    large                  1     24      21     8     65      8      9
    medium                 11    33      77     5      2      9      0
    X_large                44    21      65     9      1     34     65

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each size
        for each college
            print the number for this college and size
    What exactly was the problem again?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    sample code for aligning the code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        srand(0);
        int i,j,a[7][7];
        
        for(i=0;i<7;i++)
        {
            for(j=0;j<7;j++)
                a[i][j]=rand()%10;
        }
        
        printf("\n                                      College                  \n");
    	printf("\n%25d%5d%5d%5d%5d%5d%5d\t\t%5s\n",1,2,3,4,5,6,7,"Size total");
        printf("------------------------------------------------------------------------------\n");
        for(i=0;i<7;i++)
        {
            printf("%25d",a[i][0]);
            for(j=1;j<7;j++)
            {
                printf("%5d",a[i][j]);
            }
            printf("\n");
        }
        
        getchar();
        return 0;
    }
    
    /*my output
    
                                          College
    
                            1    2    3    4    5    6    7         Size total
    ------------------------------------------------------------------------------
                            8    9    8    7    5    7    5
                            5    0    2    3    0    2    1
                            7    1    5    5    7    0    6
                            1    5    6    7    3    1    0
                            5    8    6    0    0    7    7
                            7    1    7    6    7    3    8
                            3    7    7    4    2    1    8
    
    */
    ssharish2005

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Your output looks right, but unfortunately I don't know all of C yet so I don't understand srand. I prompted the user to enter the sizes manually. The program runs like:
    Please enter the number of sizes for college 1.
    Size(1): 5

    Size(2): 6

    Size(3): 8

    Size(4): 7

    Please enter the number of sizes for college 2.
    Size(1): 4

    Size(2): 2

    Size(3): 77

    Size(4): 3

    Please enter the number of sizes for college 3.
    Size(1): 66

    Size(2): 4

    Size(3): 52

    Size(4): 1

    ...and so on. Each Size(x) is small through xlarge. Then I need the sizes to line up in the table vertically under each college (1-7) as you did.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    dont worry about the srand and rand fucntion. i was lazy to eneter the vales so thought of using random fucntion which actually geaneates some random value to initialzes the array. srand was basiclly used to seed the rand fucntion so that it wont genereate the same sequnce of numbers all the time.

    ssharish2005

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Can you redo that loop more simply without the srand and getchar(). I still can't see how your getting your output.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a better idea. YOU redo the loop. Post where you get stuck. We're not here to do everything for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    The code in my original post hasn't changed. There is this last for loop only to make sure the input numbers are output.
    Code:
    for(college=0; college<7; ++college)
    					{
    						
    						for(size=0; size<4; ++size)
    						{
    							
    							printf("\n%d " , sweatshirts[college][size]);
    /*this loop is just to test the output numbers are correct*/	
    							if(size == 3)
    							printf("\n");					
    							
    						}
    					}
    If you run it, you will see that the numbers come out correctly, but in this format:
    1
    2
    3
    4

    5
    6
    7
    8 and so on. I need them to come out next to each other like this under the dotted line:

    Code:
          college
                           1      2      3      4       5      6      7
    ------------------------------------------------------------------------------
    small                  5      4      2      0      9      7      1
    large                  1     24      21     8     65      8      9
    medium                 11    33      77     5      2      9      0
    X_large                44    21      65     9      1     34     65

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    printf("\n%d " , sweatshirts[college][size]);
    This code is run for each item, do you see *anything* that may indicate a linebreak there that perhaps should be called only once per line instead?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    That newline should be there because I want the output to display vertically. The results dont go left to right, they go from top to bottom under each college.

    The problem is I cant get them to be side by side:

    Code:
                  college
                          1   2   3   4   5   6   7
                     --------------------------------
    small                 1   4   6
    large                 2   5   3
    xlarge                3   6   1

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an idea, how about actually reading my first reply? That's all you need to do. Nothing more, nothing less. You don't return after every single size, because then you wouldn't be on the same row, now would you? No. So how on earth do you think you're going to be able to print the rest of the row across, if you've already returned to another line?

    You're not even trying to think out this process. You're just throwing ........ at the wall hoping some of it sticks.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    If you read my replies, you would know that the final for loop is just to test the numbers. Its not supposed to be part of the program. It returns after each size because thats the only way I can get the different sizes to separate for now. When I can get them into the table correctly, that for loop will be replaced with the correct one. I also said I have tried to get the cursor position to move right after each size with /t but that didn't work. Without returning after each size you get this:
    Code:
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    17 18 19 20
    21 22 23 24
    25 26 27 28
    
    I need this:       /t  /t  /t
                        1  5   9  |
                        2  6  10  |
                        3  7  11  |
                        4  8  12  \/
    Last edited by richdb; 03-09-2006 at 08:47 PM.

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just swap the Size and College loops
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 03:04 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Printing an Array to the screen
    By simhap in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2001, 11:16 AM