-
help with loops
I"m very lost with how i should write the code for the loop. I've been stumped for days. Any help would be greatly appreciated.
//-----------------------For_Loop----------------
void Call_The_For_Loop(int Numa1, int Numb1, int &sum1)
{ //purpose: uses the for loop to find the sum of the numbers between( and including) Numa1 and Numb1
sum1 = 0;
Put in the code for the for loop here
}// end For_Loop function
-
Code:
for(int i = Numa1; i<=Numb1; i++)
{
sum1 += i;
}
-
pesudocode:
Code:
somevalue = num1
somevalue2 = num2
biggest = whateveris bigger, num1 or num2
smallest = whatever is smaller, num1 or num2
loop initialization:
sum = 0, count = smallest
count < biggest
count++
loop body:
sum += count
This is far to easy for me to actually provide the loop for you. Read the above, it details (exhaustivly) what you need to do.
[edit]
**** it. Never mind. Everyone does everyone elses homework for them anyway. What's the point. No one wants to actually learn anything.
[/edit]
Quzah.
-
Ahhhhh, yes but your way was far better.
I assumed that Numb1 was largest.
-
thank you for the psuedocode. it helped me understand a little.
could you help exlpain to me how I would do the same thing using a while loop and do while loop.
-
Use the same as my pseudocode. Just change where you put the initializers, and where you put the check. A while loop has initializers before the loop itself, the check in the while( ) block, and the incrementors inside the loop. A do-while has the initializers before the loop, the incrementation inside the loop, and the check at the end of the cycle, in the while( ) block.
Quzah.
-
Code:
initialise counter
initialise sum
set counter to low number
do add counter to sum
increment counter
while counter is not greater than high number
Code:
initialise counter
initialise sum
set counter to low number
while counter is not greater than high number
add counter to sum
increment counter
I suck at pseudocode so hopefully this helps, if not then sorry :)