Thread: Sum of all elements in an array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Sum of all elements in an array

    Hello,

    I have recently been introduced to arrays, but I cannot seem to get this program to calculate the sum of all elements correctly:

    Code:
    #include <stdio.h>
    int main()
    {	
    	int i, m, col=0, row=0;
    	int matrix[5][5]={{4, 6, 25, 88, 5}, 
    			  {34, 5, 300, 4, 65}, 
                              {78, 43, 11, 90, 125}, 
                              {98, 585, 12, 63, 21}, 
                              {45, 35, 9, 5, 1}};
                              
    	printf("In the array:");
    	printf("\n");
    	
    	for(m = 0 ; m < 5 ; m++)
    	{   
    	    for(i = 0 ; i < 5 ; i++)
    	    {	printf("%d ", matrix[m][i]);
    		col+= matrix[m][i];
    		}	
    	
    	row = col + matrix[m][i];
    	printf("\n");
    	}
    	printf("\n\n");
    	printf("the sum of the elements is %d", row);
    	return 0;
    }
    The output should look like this:

    In the array:
    4 6 25 88 5
    34 5 300 4 65
    78 43 11 90 125
    98 585 12 63 21
    45 35 9 5 1


    the sum of the elements is 1757

    Im getting 1762 as the sum

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you sum up the whole column, why are you adding the current row+column slot to it over again? Shouldn't you just tally up all your columns (from zero each time), and add each column to the overall sum?


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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    All you need is a simple int variable I'll call sum:


    Code:
      int sum;
      for(m = 0, sum = 0; m < 5 ; m++)
      {   
        for(i = 0 ; i < 5 ; i++)
        { printf("%d ", matrix[m][i]);
          sum+= matrix[m][i];
        }	
        printf("\n");
      }
    
      printf("\nThe sum of the array's values is: %d\n", sum);
    Try to organize your indentation, so that:

    1) You're using spaces, (2 to 5) instead of tabs (which the forum software handles badly).

    2) Line up the "head" of each loop, with it's closing brace (or it's opening and closing
    brace).

    3) Keep your program as simple (clear) as possible. Bugs are always waiting to ambush
    unnecessary code. Here, if you don't need to calculate the row sums - don't do it.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Thanks for the help, problem is solved.

    @Quzah lol - I have actually no idea [rather stupid now that I think about it..]

    @Adak thanks for your advice -- I actually use spaces of 3, because the program I'm using can't seem to handle tabs all that well, but I will be using the curly braces correctly from now on..

    -- also, in your example sum needs to be assigned the value 0
    Last edited by SilentPirate007; 05-02-2010 at 07:02 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SilentPirate007 View Post
    Thanks for the help, problem is solved.

    @Quzah lol - I have actually no idea [rather stupid now that I think about it..]

    @Adak thanks for your advice -- I actually use spaces of 3, because the program I'm using can't seem to handle tabs all that well, but I will be using the curly braces correctly from now on..

    -- also, in your example sum needs to be assigned the value 0
    The forum software shows a tab was being used in some of the code you posted. I'm sure the forum software is wrong, and you're right.

    for(m = 0, sum = 0; m < 5 ; m++)

    Again, I'm sure you're right, and the obvious assignment in red, is quite "wrong".

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Quote Originally Posted by Adak View Post
    The forum software shows a tab was being used in some of the code you posted. I'm sure the forum software is wrong, and you're right.

    for(m = 0, sum = 0; m < 5 ; m++)

    Again, I'm sure you're right, and the obvious assignment in red, is quite "wrong".
    sorry, didn't expect to find it there....

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No worries, it's the nature of programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screen output of array elements
    By Lego_TeCh in forum C Programming
    Replies: 4
    Last Post: 07-26-2009, 10:51 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

Tags for this Thread