### c++ beginner - use loop for odd/even number

Pages: 12
This is the description of the prac im doing. The code below is what I have come up so far. I also attached the pseudocode as well. And for me it's confusing somehow. However, I'm wanting to ask that how to use a while loop to force odd even number.

Write a program that prints all the odd or even integers between any two integers (inclusive) entered by the user. The user should be able to select whether odd or even integers are to be printed.
Use a while loop to force correct entry for the odd/even choice (use type char for choice).
The program should determine which is the smaller of the two integers entered. If the second integer is smaller, then swap – but do NOT use the swap() function. Pass the numbers and your choice of odd or even to a function.
Your code should contain the odd/even conditional statements before a while loop, and use a loop increment of two, looping from the smaller number towards the larger number. This code should be in a separate function, not in main().

PSEUDOCODE
If second integer is smaller
Swap (do NOT use the swap() function)
End if
Prompt and read choice of even or odd
Invoke foo() to print
void foo()
If (num1 is odd and choice is even) or (num1 is even and choice is odd)
End if
Print all even or odd (as per choice) from num1 to num2 (inclusive)

 ``1234567891011121314151617181920212223242526`` `````` int main() { int number; int remainder = 1; char choice; cout << "Please enter the first integer: "; cin >> number; cout << "Please enter the second integer: "; cin >> number; while ( choice ) { if ( number % 2 == 0) { cout << "\t\t " << number << " is even.\n"; } else { cout << "\t\t " << number << " is odd.\n"; } } getch(); }``````
a while loop to get even numbers say between 11 and 42.

 ``12345678`` ``````int low = 11; int high = 42; int i = low + low%2; //if low is odd, adds 1 to make it even. while(i <= high) { cout << i << endl; low+=2; }``````

without a swap, you can use std::max and std::min...
eg
if you read in x and y
low = min(x,y);
high = max(x,y);
Last edited on
whoaa im so sorry but is there any different explanation you could give me. i actually do not understand a bit :(
What is the first thing within jonnin's code excerpt that you do not understand?

If it's the %, then see: https://www.cprogramming.com/tutorial/modulus.html
this is what i have got so far.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` `````` #include #include #include #include using namespace std; void fooInt( char choice, int int1, int int2 ); //---------------------------------------------------------------------- int main() { int number; int numberOdd; int numberEven; cout << "Please enter the first integer: "; cin >> numberOdd; cout << "Please enter the second integer: "; cin >> numberEven; while ( number ) { if ( number % 2 == 0) { cout << "\t\t " << numberEven << " is even.\n"; } else { cout << "\t\t " << numberOdd << " is odd.\n"; } } fooInt(); } //--------------------------- void fooInt( char choice, int int1, int int2 ) { while ( int1 <= int2 ) { if(choice == 'o' || choice == 'O') { if(int1 % 2 != 0) { cout << int1 << endl; } } else if(choice == 'e' || choice == 'E') { if(int1 % 2 == 0) { cout << int1 << endl; } } } } ``````

Have you even compiled this?

Line 8 says fooInt takes 3 arguments.
Line 35 calls fooint with no arguments. This is an error.

you need to toss that, stop for a min, and follow the instructions.

Write a program that prints all the odd or even integers between any two integers (inclusive) entered by the user.

ok, read in 2 values. they do not represent anything more than 2 numbers, could be anything. Its not the number of evens or odds.

-------------------
The user should be able to select whether odd or even integers are to be printed.
ok, so you ask the user and get a response. You had this part, or close to it.
------------------------
The program should determine which is the smaller of the two integers entered.
get to here. Print out, for now (its not required, but you can see if you are on the right track) the smallest number.
get them arranged so you can use them, starting at small, ending at high, in your code.
once you have that much, get it running and tested. Then proceed to the next part.
can you please explain for me the swap part. I've done a bunch of code and I'm feeling like I misunderstood it
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include template typename std::enable_if_t< std::is_integral_v, T> getInp(const std::string& prm) { const auto notsp {[&]() {while (std::isspace(static_cast(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }}; T n {}; while ((std::cout << prm) && (!(std::cin >> n) || notsp())) { std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n'; std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), '\n'); } std::cin.ignore(std::numeric_limits::max(), '\n'); return n; } void process(int firNum, int secNum, char option) { if (firNum > secNum) { firNum += secNum; secNum = firNum - secNum; firNum -= secNum; } for (firNum += (firNum % 2) == (option == 'e'); firNum <= secNum; firNum += 2) std::cout << firNum << ' '; std::cout << '\n'; } int main() { auto firNum {getInp("Enter first number: ")}; auto secNum {getInp("Enter second number: ")}; char choice {}; while ((std::cout << "Odd or Even: (o/e): ") && (std::cin >> choice) && ((choice = std::tolower(choice)) != 'o' && choice != 'e')) { std::cout << "Invalid option\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), '\n'); } process(firNum, secNum, choice); }``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include int main() { int low{0}, high{0}; std::cout << "Enter first number: "; std::cin >> low; std::cout << "Enter second number: "; std::cin >> high; // SWAP int temp{0}; if (high < low) { temp = low; low = high; high = temp; } char choice{'-'}; std::cout << "Enter choice 'O' odd 'E' even numbers: "; std::cin >> choice; int start{low}; if (choice == 'O') { if(low % 2 == 0) start++; } if (choice == 'E') { if(low % 2 == 1) start++; } for(int i = start; i <= high; i += 2) std::cout << i << ' '; std::cout << '\n'; return 0; }``````
omg. thank you so much. I did the same thing. i'm so glad that i got the right answer.
Great minds think alike :)
Here's hoping your marker sees it the same way.
and what i said works differently. Maybe I misread it, I thought it said to NOT use a swap.

min gives you the smallest of 2 numbers.
max gives you the largest of 2 numbers. Actually I think they extended them to work with N numbers, but we can ignore that today.

you only have 2 numbers, x and y. Lets say x is 42, and y is 31.
you want start and end or high and low or some words like that variables.

start = min (x,y); //what is the smallest value of 42 and 31? its 31. start is 31 now.
end = max (x,y); //and the largest of 42 and 31 is 42. end is 42.
It said not to use the swap() function. Which I read as not using std::swap()...

A variation on swapping two numbers that doesn't require a temporary variable:

 ``12345678910111213141516171819202122`` ``````#include int main() { int a; int b; std::cout << "Enter number a: "; std::cin >> a; std::cout << "\nEnter number b: "; std::cin >> b; std::cout << "\nBefore swapping: a = " << a << ", b = " << b << '\n'; // swap the numbers a = a + b; b = a - b; a = a - b; std::cout << "After swapping: a = " << a << ", b = " << b << '\n'; }``````

 ```Enter number a: 12 Enter number b: 465 Before swapping: a = 12, b = 465 After swapping: a = 465, b = 12```
Yeah - that's what I used in my code above as the type is int. Don't do it with float/double though.
seeplus, I didn't see it, ooops!

Don't use the +/- variation with floats or doubles, if the numbers to be swapped added together overflow an int.

Using a temp is still the better solution for a "roll your own" algorithm.
There's problems with precision - adding and subtracting float/doubles may not give you the same original numbers.
the xor variation can't overflow.
a^=b
b^=a
a^=b
swapped :)
the best way is some sort of assembly though... which may be what std::swap really does (?)
register 1 = a
register 2 = b
a = register 2
b = register 1
or the same thing with push/pops on the system stack, something like that. I dunno anymore what the best assembler one is, but doing it in c++ seems like it won't generate the best code.
Last edited on
xor swap fails to produce the expected result if a and b alias.

std::swap compiles to the following code
 ``` mov eax, dword ptr [rdi] mov ecx, dword ptr [rsi] mov dword ptr [rdi], ecx mov dword ptr [rsi], eax ret```

https://godbolt.org/z/zK4Tb8dPv
Last edited on
Pages: 12