School assignment using while loops

Started by
25 comments, last by deathknight2005 14 years, 9 months ago
Well I've managed to do all the parts except for part 3.. how would I go about doing it? I've added the variables counterR (for even numbers, don't know if I had to do that but I did) and sum (for the sum). I'm not sure what to put inside the while statement though..
Advertisement
Quote:Original post by Ultimatepuff
Well I've managed to do all the parts except for part 3.. how would I go about doing it? I've added the variables counterR (for even numbers, don't know if I had to do that but I did) and sum (for the sum). I'm not sure what to put inside the while statement though..

I assume by now you are able to create a while loop that prints all the even numbers instead of odd numbers, right? Good. [smile]

So the only thing left to do, is to add them all up and print the final sum. All the even numbers are already available to you in that loop (you're already able to print them after all). So what we need is a variable outside the scope of the while loop that can keep track of the sum. Looking at your code, I guess that's where the variable named betweenNum is for, but you could also declare a new one.

Before the entering the loop, this variable should hold the value zero, because before we have started adding up the even numbers, the sum would be zero. Imagine a situation where the input would be 18 and 20: in this case there isn't a single even number between them, and therefor the sum should be zero (or non-existent, or undefined, or what ever you want to called it). So it's consistent. As a side node, it's good to always initialize you variables to 'something', because otherwise it's probably going to hold some unpredictable garbage.

Now all we have to do is use this variable to add up our even numbers. We can just add every number to it we encounter in our loop and that should give us the sum. After we exit the loop, we can print its value.
This problem is actually very similar to the problem WanMaster helped you with, except in this case it involves even numbers and instead of printing the numbers, you must get the sum of them all.

I would have simply summed all of the even numbers and printed the odd numbers in the same loop, however, because of the way the program is structured so far, and because it is a project that demonstrates the use of loops, you can just use another loop for this problem. That way, you can use the same counter variable as well.

In this case, you must make sure sum = 0 at the very beginning of your code (otherwise it is an uninitialized variable).

Actually, a simple way to do it is to do the exact opposite of WanMaster's code (so that it makes sure the numbers are even, not odd).

if (secondNum % 2 == 1)
counter = secondNum - 1; // odd, but we want even
else
counter = secondNum - 2; // even

So let's take a few numbers for example:

15: Since the remainder of 15/2 is 1 (15 % 2 == 1), counter will begin at 15 - 1 which is 14, an even number.

14: Since the remainder of 14/2 is 0 (14 % 2 == 0), counter will begin at 14 - 2 which is 12, again an even number.

Next, you can go about it the same way as the other problem. By looping through and decrementing the counter variable by 2, until it is >= to firstNum. The only difference in this case is that with each loop you want to add the counter to sum.

sum = 0; while (counter >= firstNum){  sum += counter;  counter -= 2;}


See, it is very similar to the code for the other problem, with some small differences. Once that is done, all you do is display the sum.

I hope that helped!
Quote:Original post by WanMaster
Quote:Original post by Ultimatepuff
Well I've managed to do all the parts except for part 3.. how would I go about doing it? I've added the variables counterR (for even numbers, don't know if I had to do that but I did) and sum (for the sum). I'm not sure what to put inside the while statement though..

I assume by now you are able to create a while loop that prints all the even numbers instead of odd numbers, right? Good. [smile]

So the only thing left to do, is to add them all up and print the final sum. All the even numbers are already available to you in that loop (you're already able to print them after all). So what we need is a variable outside the scope of the while loop that can keep track of the sum. Looking at your code, I guess that's where the variable named betweenNum is for, but you could also declare a new one.

Before the entering the loop, this variable should hold the value zero, because before we have started adding up the even numbers, the sum would be zero. Imagine a situation where the input would be 18 and 20: in this case there isn't a single even number between them, and therefor the sum should be zero (or non-existent, or undefined, or what ever you want to called it). So it's consistent. As a side node, it's good to always initialize you variables to 'something', because otherwise it's probably going to hold some unpredictable garbage.

Now all we have to do is use this variable to add up our even numbers. We can just add every number to it we encounter in our loop and that should give us the sum. After we exit the loop, we can print its value.


So if I add the code
while(counterR >= firstNum)                 {                               cout << (sum+=counterR) << " ";                                 counterR -= 2;                 }

(sum being the 'new variable'), is this what I would have to do after initializing the sum as 0? Probably not seeing as it doesn't work well for me..
Quote:Original post by deathknight2005
This problem is actually very similar to the problem WanMaster helped you with, except in this case it involves even numbers and instead of printing the numbers, you must get the sum of them all.

I would have simply summed all of the even numbers and printed the odd numbers in the same loop, however, because of the way the program is structured so far, and because it is a project that demonstrates the use of loops, you can just use another loop for this problem. That way, you can use the same counter variable as well.

In this case, you must make sure sum = 0 at the very beginning of your code (otherwise it is an uninitialized variable).

Actually, a simple way to do it is to do the exact opposite of WanMaster's code (so that it makes sure the numbers are even, not odd).

if (secondNum % 2 == 1)
counter = secondNum - 1; // odd, but we want even
else
counter = secondNum - 2; // even

So let's take a few numbers for example:

15: Since the remainder of 15/2 is 1 (15 % 2 == 1), counter will begin at 15 - 1 which is 14, an even number.

14: Since the remainder of 14/2 is 0 (14 % 2 == 0), counter will begin at 14 - 2 which is 12, again an even number.

Next, you can go about it the same way as the other problem. By looping through and decrementing the counter variable by 2, until it is >= to firstNum. The only difference in this case is that with each loop you want to add the counter to sum.

*** Source Snippet Removed ***

See, it is very similar to the code for the other problem, with some small differences. Once that is done, all you do is display the sum.

I hope that helped!


Thanks, but I've tried that and when I compile and run it, it would just output the even numbers with a little sum added to them

I started playing around a little with this and it worked! Thanks

[Edited by - Ultimatepuff on July 21, 2009 3:21:48 PM]
I might be coming in too late, but I decided to solve the problem myself, here's what I got. I compiled and ran the program and everything outputs as expected:

//challenge#include <iostream>using namespace std;int firstNum = 1;int secondNum = 0;int Sumofevens;int square(int a){    return (a*a);}int main(){    while(firstNum > secondNum)    {    cout << "Enter two numbers, the first being lesser: ";    cin >> firstNum >> secondNum;    if (firstNum > secondNum) cout << "Error, enter again.\n";    }    int i;        i = 1;    cout << "the odd numbers between firstNum and secondNum are: \n";    while(firstNum+i<secondNum)    {        if ((firstNum+i)%2 == 1) // if it is odd     cout <<  firstNum+i << "\n";     i++;     }          i = 1;     cout << "The sum of all even numbers are: \n";     while(firstNum+i<secondNum)     {     if ((firstNum+i)%2 == 0) // if it is even     Sumofevens+=firstNum+i;     i++;     }     cout << Sumofevens << endl;     i = 1;     cout << "The numbers and their squares are: \n";     while (firstNum+i<secondNum)     {      cout << firstNum+i << "^2 = " << square(firstNum+i) << endl;     i++;     }     i = 0;     cout << "Countdown sequence: \n";     while (secondNum-i>=firstNum)     {           cout << secondNum-i << endl;           i++;     }cin >> i; //pause before exiting program}                                     
I see what you are doing.

You are writing the sum to the console in each loop. That won't work. You have to add counter to the sum with each loop, and at the very end of the loop, print it. If you print it during each loop it will, like you said, only print increasing numbers each time.

I think you misunderstood what I said when I told you to print sum. I meant after the loop.

This will not work:

while(counterR >= firstNum)
{
cout << (sum+=counterR) << " ";
counterR -= 2;
}

Because look at where you are printing. Every single loop will print something, but you only want to print the total sum.

You have to do this:

sum = 0; while (counter >= firstNum){  sum += counter;  counter -= 2;}cout << sum << endl;


If you do that, sum will continually add the counter variable to itself, and, when the loop is over, print the value to the console.

EDIT: Oh, it looks like you figured it out yourself!

This topic is closed to new replies.

Advertisement