# how to sum 500 numbers

This is a discussion on how to sum 500 numbers within the C Programming forums, part of the General Programming Boards category; Hello all, Could anyone please help me writing a code to sum a 500 numbers from 2-arrays. heres what i ...

1. ## how to sum 500 numbers

Hello all,

heres what i wrote so far
Code:
```unsigned int a[500];
unsigned int b[500];
int i;

for ( i=0; i<=1000 ; i++)

.......
}```
what shall i do next to get the sum of all the values ?

thank you

2. How do you store a value?
How do you add to that value?

Quzah.

3. thanks quzah ... yeah the problem is i am not sure hot to add to that value .. so i am a newbie to programming

4. Well, you know how to multiply, as we see that in the code. Surely you know what symbol is used for addition? (Hint: It's '+'.)

5. haha
well, thank you
is something like that correct ?
Code:
```unsigned int a[500];
unsigned int b[500];
int i;
unsigned int sum=0;
for ( i=0; i<=1000 ; i++)
}```
thanks

6. You need to give a size for the answer array. Your loop goes to 1000 but you only have 500 pairs of numbers.

7. Yes, something like that, but definitely not that:
Code:
```unsigned int a[500];
unsigned int b[500];
int i;
unsigned int sum=0;
for ( i=0; i<=1000 ; i++)
}```
Egad! Where are those other 500 numbers coming from? a and b are only defined for indexes 0 to 499, but your loop tries to index all the way through 999. Also, you should probably give answer a size. I reckon 500 would be good there too, once you change your loop. This sounds like a perfect opportunity to put in a plug for named constants (i.e. not bad juju magic numbers like 500 and 1000) and indentation:
Code:
```#define SIZE 500  // hooray for constants...want to change the size?  only one place to change it instead of 4
unsigned int a[SIZE];
unsigned int b[SIZE];
int i;
unsigned int sum = 0;

for (i = 0; i < SIZE; i++) {
sum += answer[i];  // that's shorthand for sum = sum + answer[i]
}```

8. Code:
```
unsigned int a[500];
unsigned int b[500];
int i;
unsigned int sum=0;
for ( i=0; i<=500 ; i++)
}```
?

9. thank you guys

10. If you think that 2 is the same number as 500, this is going to be a long thread.

11. This thread teaches us: You can do a man's work for him, but you can't teach a man to think.

Quzah.

12. Code:
`unsigned int answer[2];`
Why would you only make answer an array of 2 ints? Your loop runs i from 0 to 500 (should be 499, see below), and tries to access answer[i], which will be invalid for any values of i other than 0 or 1.

Code:
`for ( i=0; i<=500 ; i++)`
Drop that = sign. Your array has 500 elements, but the valid indexes are 0..499 (it's always 0..size-1 in C). Your loop goes one too far, and tries to access a[500] and b[500], which don't exist.

I think you just missed my post when you were replying, it gives a good solution to all your array size and loop troubles. Scroll up a little...

13. thank you anduril462 ... I appreciate your help ... and for the others , bravo