School assignment using while loops

Started by
25 comments, last by deathknight2005 14 years, 9 months ago
Quote:Original post by Haptic
Well you already have a while loop there that goes through all of the numbers (albeit backwards), so for question 4, why not just add a few lines that calculates and outputs the square of the current number.

To calculate if a number is odd or not you need to divide it by 2 and get the remainder. In C, this is done with the % operator. So 3%2 = 1.
If the answer is zero, it's even, if it's 1 its odd. You just need to add this condition to your loop.

As a side note - you are setting the value of 'counter' before you set the value of secondNum which is a bug.


I'm a little confused about % operators. Can someone provide an example within a code or something?
Advertisement
if you remember doing division in elementary school you often were left with remainders. this is where the % operator comes in as it returns the remainder of the division between the two numbers.

Now figure out what you need to divide by to figure out if it's an odd or even number and you've solved one of your problems.

If you don't remember/know much of your math; a square is a number times itself;

once you solve the odd question you should be able to change some minor things to get the sum of all even numbers.

I would tackle this problem by solving each individual problem then combining them together.
% returns the 'remainder' of a division.

So for example, if you divide 11 by 2:
2 goes into 11 five whole times - with 1 remainder.
So 11%2 will return 1.

cin >> number;// if i divide number by 2, what will the remainder be?int rem = number % 2; if(rem == 0){    // number is even because 2 fits perfectly into it}else{    // number is odd because 2 DOES NOT fit perfectly into it}


Some more examples:
8 % 3 = 2
4 % 1 = 0
12 % 5 = 2
- Haptic
As stated already the % operator or modulus operator calculates and returns the remainder of the division.
So for example:
if 1/2 = 0 with a remainder of 1 then 1%2 = 1
if 2/2 = 1 with a remainder of 0, then 2%2 = 0
if 3/2 = 1 with a remainder of 1, then 3%2 = 1
if 4/2 = 2 with a remainder of 0, then 4%2 = 0
if 5/2 = 2 with a remainder of 1, then 5%2 = 1

Notice the alternating pattern of 1s, and 0s in this case when we divide by a value of 2.
You can use this method to obtain numbers that are even or odd.

This loop would display the values that are even and odd from 1 though 10.


for( int i = 1; i <=10; ++i )
{
if( i%2 )
cout i << " is an odd number." << endl;
else
cout i << " is an even number." << endl;
}





also rereading your original post it seems like you're gonna need to make a do-while loop to prompt the user for values.
The reason you are having trouble with setw is because it's part of iomanip, so in order for your code to compile you will need to add #include<iomanip> to the top of your file.

It basically tells the stream to output the next piece of data with a set width you provide to the setw function. It will output that many characters worth of width regardless of whether or not the data fills it all up. If it doesn't, it will output blank space to fill in the rest. You can also align the output left or right, say...

cout << setw( 5 ) << right << someInt;
// will output value in someInt to the console with width of 5, right aligned

(right and left are members of std, so you either need to use the namespace std or type the fully resolved std::right, std::left)
Quote:Original post by SilentEcho
The reason you are having trouble with setw is because it's part of iomanip, so in order for your code to compile you will need to add #include<iomanip> to the top of your file.

It basically tells the stream to output the next piece of data with a set width you provide to the setw function. It will output that many characters worth of width regardless of whether or not the data fills it all up. If it doesn't, it will output blank space to fill in the rest. You can also align the output left or right, say...

cout << setw( 5 ) << right << someInt;
// will output value in someInt to the console with width of 5, right aligned

(right and left are members of std, so you either need to use the namespace std or type the fully resolved std::right, std::left)

Thanks
Quote:Original post by Haptic
% returns the 'remainder' of a division.

So for example, if you divide 11 by 2:
2 goes into 11 five whole times - with 1 remainder.
So 11%2 will return 1.

*** Source Snippet Removed ***

Some more examples:
8 % 3 = 2
4 % 1 = 0
12 % 5 = 2


Thanks for that, but I have no idea where to start in putting it into my code. I've tried
    if (counter%2 == 1)    remainder = odd;    if (counter%2 == 0)    remainder = even;

(after putting remainder, odd, and even as int variables) and
cout << setw(odd) << odd << endl;

after the counter--;. Not sure if I'm starting out right with this..
Quote:Original post by Ultimatepuff
I have no idea where to start in putting it into my code.

Haven't followed the entire thread, but I assume this is about problem 2:
Quote:Output all odd numbers between firstNum and secondNum.


You already know how to find out whether a number is odd or even. There are more ways to do it, but the suggested manner should do fine.

We start the loop with the highest number. First, let's find out if this number is odd or even:
if (secondNum % 2 == 1) remainder = odd;else remainder = even; // we don't need to test again: if it's not odd, it has to be even.

Let's see some examples of what the output should look like in several cases:

firstNum = 10 secondNum = 21output: 11, 13, 15, 17, 19firstNum = 11secondNum = 28output: 13, 15, 17, 19, 21, 23, 25, 27firstNum = 7secondNum = 16output: 9, 11, 13, 15

As you may notice, we can start our counter at either secondNum - 1 if the number is even, or secondNum - 2 if the number is odd. Let's use that in our code:

if (secondNum % 2 == 1) counter = secondNum - 2; // odd;else counter = secondNum - 1; // even

Again, as you can see in the example outputs, the only thing left to do is to decrease this counter by 2 every iteration, until it is equal or smaller to firstNum. There's no need to keep checking for the odd/even property, because if say, a number is odd then every time you add or substract a multiple of 2, you get a new number that is also odd. Same goes for even numers. The codez:

while (counter >= firstNum){  cout << counter << "\n";  counter -= 2;}
Quote:Original post by WanMaster
Quote:Original post by Ultimatepuff
I have no idea where to start in putting it into my code.

Haven't followed the entire thread, but I assume this is about problem 2:
Quote:Output all odd numbers between firstNum and secondNum.


You already know how to find out whether a number is odd or even. There are more ways to do it, but the suggested manner should do fine.

We start the loop with the highest number. First, let's find out if this number is odd or even:
if (secondNum % 2 == 1) remainder = odd;else remainder = even; // we don't need to test again: if it's not odd, it has to be even.

Let's see some examples of what the output should look like in several cases:

firstNum = 10 secondNum = 21output: 11, 13, 15, 17, 19firstNum = 11secondNum = 28output: 13, 15, 17, 19, 21, 23, 25, 27firstNum = 7secondNum = 16output: 9, 11, 13, 15

As you may notice, we can start our counter at either secondNum - 1 if the number is even, or secondNum - 2 if the number is odd. Let's use that in our code:

if (secondNum % 2 == 1) counter = secondNum - 2; // odd;else counter = secondNum - 1; // even

Again, as you can see in the example outputs, the only thing left to do is to decrease this counter by 2 every iteration, until it is equal or smaller to firstNum. There's no need to keep checking for the odd/even property, because if say, a number is odd then every time you add or substract a multiple of 2, you get a new number that is also odd. Same goes for even numers. The codez:

while (counter >= firstNum){  cout << counter << "\n";  counter -= 2;}


Wow, thanks! This really helped. Now off to work on step 3..

This topic is closed to new replies.

Advertisement