472,806 Members | 1,786 Online

# Summation Questions

1. Given that two int variables, total and amount , have been declared, write a sequence of statements that:
- initializes total to 0
- reads three values into amount , one at a time.

After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ).

>>so, first I have to initialize total to 0
total = 0;

>>then I don't know what to do to "read three values into amount, one at a time,"

>>After reading the values, total becomes
total +=amount;

So, does the code altogether becomes
total=0;
total+=amount;
?

2. Given that two int variables, total and amount , have been declared, write a loop that reads non-negative values into amount and adds them into total . The loop terminates when a value less than 0 is read into amount . Don't forget to initialize total to 0.

This is the code I wrote which I know is not correct..

total =0;
for (amount = 1; amount > 0; amount++)
{
total +=amount
}

Nov 17 '07 #1
4 12132
sicarie
4,677 Expert Mod 4TB
1. Given that two int variables, total and amount , have been declared, write a sequence of statements that:
- initializes total to 0
- reads three values into amount , one at a time.

After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ).

>>so, first I have to initialize total to 0
total = 0;

>>then I don't know what to do to "read three values into amount, one at a time,"

>>After reading the values, total becomes
total +=amount;

So, does the code altogether becomes
total=0;
total+=amount;
?
So what do your class notes and your book say about reading integers in? What are some ways to do it? What do you need to do before you just read the variables in (hint: you would have had to do it to total before you set it to 0)

2. Given that two int variables, total and amount , have been declared, write a loop that reads non-negative values into amount and adds them into total . The loop terminates when a value less than 0 is read into amount . Don't forget to initialize total to 0.

This is the code I wrote which I know is not correct..

total =0;
for (amount = 1; amount > 0; amount++)
{
total +=amount
}

So you know your code is not correct there, i see two specific procedural steps you need to take before you add amount to total. What are they? (Remember, compilers are not smart, they only do EXACTLY what you tell them. No more, no less).
Nov 18 '07 #2
When you said I needed to do something to read the integer, did you mean I had to do "scanf ("%d", &total);" first?

How can I read three values into one integer, one at a time then? Do I just use three random values?
Nov 18 '07 #3
sicarie
4,677 Expert Mod 4TB
When you said I needed to do something to read the integer, did you mean I had to do "scanf ("%d", &total);" first?
I would recommend trying it. There is no substitute for experience and playing around with the code yourself. Have you set up any sort of actual program that you can play around with this and test it yourself? (PS - Yes, you are right - something along those lines, though I'm not sure that what you have there will work properly the first time)

How can I read three values into one integer, one at a time then? Do I just use three random values?
Your loop will take care of that. Just think of it procedurally. Right now your loop is set to execute infinitely.

Expand|Select|Wrap|Line Numbers
1. total =0;
2. for (amount = 1; amount > 0; amount++)
3. {
4. total +=amount
5. }
So you set amount to 1, and then as long as it is above 0, it will execute, adding 1 each time. I would recommend using a while loop there, then you can test 'while amount is not zero'. Then, you need to do three things each time. What are those three things?
Nov 18 '07 #4
oler1s
671 Expert 512MB
You can't do question two if you aren't solid on question one. So try getting that done correctly. Getting question one done means writing a compileable program that runs properly. So a few random statements here and there don't qualify. You need things like includes, main, declaring your variables, and so on.

And programming isn't something you stare out and instantaneously figure out the code. You are going to have to sit and type, and you might make mistakes. You're going to have fiddle with the code...
Nov 18 '07 #5