# Sum of all elements in an array

• 05-02-2010
SilentPirate007
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
• 05-02-2010
quzah
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.
• 05-02-2010
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. :cool:
• 05-02-2010
SilentPirate007
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
• 05-02-2010
Quote:

Originally Posted by SilentPirate007
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". :p
• 05-02-2010
SilentPirate007
Quote: