Simple Q about a for increment...

Ok What I want to do is have each integer of an array added up and displayed with a printf. This should be easy but I can't figure it out! Please help here is my code so far...

#include <stdio.h>

main()
{      
int count;
int numbers[10];
int sum;

        for (count = 0 ; count < 10 ; count++)
        {
                printf ("Please enter a number: ");
                scanf ("%d", &numbers[count]);
        }
        for (count = 0  ; count < 10 ; count++)
        {
                sum = numbers[0] + numbers[1];
        }
        printf ("The sum of all the numbers entered is %d\n", sum);
}

Obviously the numbers[0] + numbers[1] wants to be replaced with something like

sum = numbers[0] + numbers[count]  but this doesn't work!

Cheers Jim
Jim2000Asked:
Who is Participating?
 
redpirkConnect With a Mentor Commented:
dont you want to replace

sum = numb[0] + numb[1];

with

sum = sum + number[count];

??
0
 
Jim2000Author Commented:
No! That won't (doesn't work).
0
 
redpirkCommented:
How does it not work?
the second loop, with my replacement, should work without problems?
Are you not getting the right result, or what goes wrong?

/Linus
0
 
redpirkCommented:
Ok, just to make sure, i compiled it myself, and it works fine!
The only thing that might be a problem, is that you dont
initialize sum to anything, you can do this by replacing

int sum;

with

int sum=0;

Then it should work.. If it doesent, tell me how it doesent!

/Linus
0
 
Jim2000Author Commented:
Yup! Cool I wasn't initializing sum to anything stuck an =0; in there an it was fine thanks! I new it had to be something silly!

Jim
0
All Courses

From novice to tech pro — start learning today.