More array advice!

This is a discussion on More array advice! within the C Programming forums, part of the General Programming Boards category; Well from the first page it seems everyone is at that part of the semester with arrays...lol My question is, ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Talking More array advice!

    Well from the first page it seems everyone is at that part of the semester with arrays...lol My question is, I understand how the input works, but now I have to understand how the ouput works?

    This program asks for a 5 X 5 array. Then prints out the sum for each of the rows, and then the columns.

    EX:
    Row totals: 12 5 15 12 23
    Column totals: 30 34 45 23 12

    You guys get the idea..

    Code:
    #include<stdio.h>
    
    main ()
    {
    	int a[5][5];
    	int sum=0, row=0, col=0, x=0, n=0;
    
    	for ( row = 0; row < 5; row++ ) 
    	{
    		printf("Enter row %d (5 digits): ", row+1);
    
    		for ( col = 0; col < 5; col++ ) 
    		{
    			scanf("%d", &a[row][col]);
    			sum += a[row][col];
    		}
    	}
    
    		printf("Row totals: %d \n", sum );
    	
    return 0;
    }
    I think I would just need to use more for statements, becasue this code gives me the sum of all the numbers. However I am confused where?

    CJ

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'd be best served making 2 more arrays. This would be the easiest method.
    Code:
    int rowsums[ROWS] = {0};
    int colsums[COLS] = {0};
    int x,y;
    
    for( x = 0; x < ROWS; x++ )
        for( y = 0; y < COLS; y++ )
        {
            rowsums[x] += array[x][y];
            colsums[y] += array[x][y];
        }
    Then just print out each array.

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

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    to make it easier to start of with, i suggest breaking up the program into several loops, not just 1 loop and a nested loop.

    first loop would do the input for the array, no summing up.

    another 2 loops for summing the rows and summing the columns.

    each time u do the sum of one of the rows or columns, print it out and go on to the next one.

    i'll just do an example here, and i think u'll beable to work it out from there.

    Code:
    // assume array is initialised
    int total;
    int row, col;
    
    for(row = 0; row < 5; row++)
    {
    	total = 0;
    	for(col = 0; col < 5; col++)
    	{
    		total += array[row][col];
    	}
    	printf("%d ", total);
    }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    WOW...I like both of those ways!

    And for once I understand both of them...lol

    I'm try both and see which one I like better..

    However, there is a second part so I will most likely be back!

    CJ

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Told ya I get confuzed again!

    I need to get the lowest quiz score from each of them! My current for statement returns the last quiz entry. I tried putting the printf inside the second 'for' loop, but it just mirrored the numbers back to me.

    Code:
     printf("\n");
    printf("Lowest score for each quiz: ");
    	
                    for(col = 0; col < 5; col++)
    	{	
    		
    		for(row = 0; row < 5; row++)
    		{
    			low = a[row][col];
    			
    			if ( a[row][col] < a[row][col])
    				low = a[row][col];
    		}
    				printf("%d ", low);
    	}
    Any tips?

    CJ

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    Code:
    for(row = 0; row < 5; row++)
    		{
    			low = a[row][col]; // don't think this should be here
    			
    			if ( a[row][col] < a[row][col])
    				low = a[row][col];
    		}
    well first u should initialise low to the highest possible number.

    and low should only take the current value if the current value is lower than 'low'

    ie
    Code:
    if(low > current)
          low = current;

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Kyro
    Code:
    for(row = 0; row < 5; row++)
    		{
    			low = a[row][col]; // don't think this should be here
    			
    			if ( a[row][col] < a[row][col])
    				low = a[row][col];
    		}
    well first u should initialise low to the highest possible number.

    and low should only take the current value if the current value is lower than 'low'

    ie
    Code:
    if(low > current)
          low = current;
    I've tried that already and it either gives me zero or the last array of numbers.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    take a look at the code below

    compare it to the code you've written.

    Code:
    int main()
    {
    	int a[5][5] = 
    	{
    		{1,4,2,7,0},
    		{8,5,7,3,6},
    		{2,4,1,7,5},
    		{9,4,0,2,7},
    		{1,5,3,6,3}
    	};
    	int col, row, low;
    
    	printf("\n");
    	printf("Lowest score for each quiz: ");
    	
        for(row = 0; row < 5; row++)
    	{	
    		low = a[row][0];
    		
    		// col = 1 since low == a[0][col]
    		for(col = 1; col < 5; col++)
    		{
    			
    			if ( a[row][col] < low)
    				low = a[row][col];
    		}
    		printf("%d ", low);
    	}
    
    	return 0;
    }
    
    /*
    Output:
    
    Lowest score for each quiz: 0 3 1 0 1
    */

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Kyro
    take a look at the code below

    Code:
    int main()
    {
    	int a[5][5] = 
    	{
    		{1,4,2,7,0},
    		{8,5,7,3,6},
    		{2,4,1,7,5},
    		{9,4,0,2,7},
    		{1,5,3,6,3}
    	};
    	int col, row, low;
    
    	printf("\n");
    	printf("Lowest score for each quiz: ");
    	
        for(row = 0; row < 5; row++)
    	{	
    		low = a[row][0];
    		
    		// col = 1 since low == a[0][col]
    		for(col = 1; col < 5; col++)
    		{
    		                low = a[0][col];  // This where I had mine and it was messing up!  Where you have it works!
    			if ( a[row][col] < low)
    				low = a[row][col];
    		}
    		printf("%d ", low);
    	}
    
    	return 0;
    }
    
    /*
    Output:
    
    Lowest score for each quiz: 0 3 1 0 1
    */
    OK...so when you put it in the first for loop it keeps it on the top row correct? Me putting it in the inside for gave a value of zero and since nothing is lower then zero...

    CJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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