• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

redpirkCommented:
dont you want to replace

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

with

sum = sum + number[count];

??
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.