Please Help

Pages: 12
Nov 29, 2012 at 4:15pm
I need to write a program that prints all the even numbers between any two numbers using print_even that takes the initial and ending number as parameters, and prints all the intervening even numbers. I have to use a loop to implement this??


Any ideas?!?
Nov 29, 2012 at 4:22pm
Take in your first parameter as your starting point,
if(firstNum % 2 == 0) // The number is even

then print out that number and start incrementing by two.

if(firstNum % 2 != 0) // The number is odd

then increment by one, print the new number and start incrementing by two
Nov 29, 2012 at 4:40pm
Not understanding
Nov 29, 2012 at 5:25pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void print_even(int firstNum, int lastNum)
{
    int currentNum = firstNum;
    if(firstNum %2 == 0)    // The number is even
    {
        currentNum += 2;    // Increment by two since it is even
    }
    else                    // The number is odd
    {
        currentNum += 1;   // Increment by one since it is odd
    }

    // Print all even numbers until lastNum is reached
    while(currentNum < lastNum)
    {
        std::cout << currentNum << std::endl;
        currentNum += 2;
    }   
}



The print_even function would look something like this, firstNum % 2 checks the remainder after dividing firstNum by 2, so if firstNum is even then the remainder will be 0, otherwise it is an odd number if the remainder is not equal to 0.

So once you have the next even number after firstNum, you start incrementing by 2 and printing out the number each time until you reach the lastNum.
Last edited on Nov 29, 2012 at 5:27pm
Nov 29, 2012 at 6:28pm
A simpler, albeit slower, method is to just use a simple for loop:
1
2
3
4
5
void PrintEvenNumbers(int start, int end) {
   for (int i = start, i <= end; i ++)
      if (i % 2 == 0)
         cout << i << "\n";
}


The code above will print out the starting and ending numbers if they're even as well however. If you don't want to print them out, in case they're even, just change the for line to:
for (int i = start + 1; i < end; i ++)

Simple function.
Last edited on Nov 29, 2012 at 6:29pm
Nov 29, 2012 at 6:45pm
1
2
3
4
5
6
7
8
if(num == odd)
   num++;

while(num < end)
   {
    output num;
    num = num + 2
   }
Nov 30, 2012 at 11:33pm
None of these codes compile when I try it??
Nov 30, 2012 at 11:39pm
What errors are you getting? You can't just copy that code directly and run it. You will need the main function and to pass values in etc.
Nov 30, 2012 at 11:43pm
#include <iostream>
int main();
using namespace std;
void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}

// Print all even numbers until lastNum is reached
while(currentNum < lastNum)
{
std::cout << currentNum << std::endl;
currentNum += 2;
}
return 0;
}
Dec 1, 2012 at 12:52am
It doesn't work??
Dec 1, 2012 at 1:39am
You are defining a function inside main(). Move the function definition outside and it should work. If you put the definition after main you'll have to add a prototype before main so that when you call the function the compiler at least knows what it is.
Dec 1, 2012 at 1:55am
I am sorry, I am fairly new at this. How do I define it outside of main?
Dec 1, 2012 at 2:03am
Read this:

http://www.cplusplus.com/doc/tutorial/functions/

That should help speed you on your way. :) Pay special not to where the addition() function definition is in relation to main().

Also, in the 2nd page (Functions II), the part on declaring functions talks about putting function prototypes before main and definitions after main. Whether you do it this way or by simply putting the entire definition before main is up to you.
Last edited on Dec 1, 2012 at 2:07am
Dec 1, 2012 at 2:40am
Ok so I have put main outside and now, nothing runs when I compile it because I need to call my function inside of main, but again how?? This is where I am not getting it

#include <iostream>

using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}

// Print all even numbers until lastNum is reached

while(currentNum < lastNum)
{
std::cout << currentNum << std::endl;
currentNum += 2;
}
}
int main()
{
system ("PAUSE");
return 0;
}
Dec 1, 2012 at 9:29pm
Anyone able to help?
Dec 1, 2012 at 9:47pm
You need to actually call the function.
Take another look at the tutorial page linked above by Raezzor.
Dec 1, 2012 at 11:37pm
And I said move the function definition out of main() not move main() out of the everything else. :p
Dec 2, 2012 at 12:51am
Am I getting somewhere?

#include <iostream>

using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}
// Print all even numbers until lastNum is reached
int main ();
{
while(currentNum < lastNum)
std::cout << currentNum << std::endl;
currentNum += 2;
system ("PAUSE");
return 0;
}
}
Dec 2, 2012 at 1:27am
Not really. The operating system calls main(). The code then executes until it reaches the end of main (where it says return 0 in main). Do you see any problems relating to this in your code?

Also, please use the code tags (<> button on the right when responding or editing) to post your code. This way it'll keep all the indentations you use and make the code easier to read.
Last edited on Dec 2, 2012 at 1:28am
Dec 2, 2012 at 1:43am
#include <iostream>
int main;
using namespace std;

void print_even(int firstNum, int lastNum)
{
int currentNum = firstNum;
if(firstNum %2 == 0) // The number is even
{
currentNum += 2; // Increment by two since it is even
}
else // The number is odd
{
currentNum += 1; // Increment by one since it is odd
}
// Print all even numbers until lastNum is reached

{
while(currentNum < lastNum)
std::cout << currentNum << std::endl;
currentNum += 2;
system ("PAUSE");
return;
}
}
Pages: 12