I have a c++ class test tomorrow that counts towards my final module grade. I am a beginner and very inexperienced in programming and I have been stressing a lot about this test. I was wondering if anyone would be able to help me with this question, any help would be greatly appreciated.
Cheers
Create a single program that:
• Demonstrates a ‘for’ loop that produces the output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.
• Demonstrates a ‘do’ – ‘while’ loop that does exactly the same, just the other way round. However, only odd numbers should be displayed, i.e. the output is: 1, 3, 5, 7, 9.
• Demonstrates a ‘while’ loop that uses the ‘read-ahead’ technique: it asks the user to enter numbers between (and including) 100 and 150, entering a zero terminates the loop. If numbers less than 100 or larger than 150 are entered an error message is shown (e.g. “Error: 250 is not allowed”) also if a number ending in 0 is entered then a message “Your number is divisible by 10” gets displayed. After termination of the loop the program displays the smallest and largest valid numbers that were entered.
Example run of the ‘while’ loop:
Inputs Corresponding Outputs
172 Enter another number
9 Error: 9 is not allowed
170 Your number is divisible by 10. Enter another number
0 The largest valid number you entered was 172 whilst the smallest was 170.
For the first bullet, you'll need to think along the lines of for( /* starting point */ ; /* boolean condition */ ; /* increment or decrement */ )
For the second bullet, you'll need about the same thing, but adding a modulus in there. You can either do x % 2 == 0 or x % 2 != 1
And lastly, for the third bullet, you'll need if( num == 0 ) break; ALONG WITH if statements with conditions of
> 100 and < 150
And again, you'll want to think in terms of modulus as well, especially for that divisible by 10 part. I would recommend vectors for easy storage of inputs.