Here are a few questions to help you clarify your understanding of more advanced looping topics.
What is wrong in the following code? (You can assume that all variables are declared and adequately named.)
x = 0; for (i = 0, i < 10, ++i) x += i;
The commas in the head need to be semi-colons.
There should be braces around the body (x being incremented):
x = 0;
for (i = 0; i < 10; ++i)
{
x += i;
}
What is wrong in the following code? (You can assume that all variables are declared and adequately named.)
quit = true; do ch = menu(); quit = process(ch); while (!quit);
There need to be braces around the two statements indented between do and while. Since quit is assigned inside the loop, it need not be initialized to true before the loop. (But if you really wanted to initialize it, it would make more sense to initialize it to false given the condition within the while.) do { ch = menu(); quit = process(ch); } while (!quit);
Given the following loop:
x = 3; for (j = start; j <= end; j++) { x *= 4; x %= 15; }
[Exactly] How many times does the loop execute? (You can assume start is less than or equal to end.)
end - start + 1
What is the value of x after the loop is done if start is 1 and end is 10000?
Let's see: x | j | after -------+---------+--------- 3 | | init 12 | 1 | *= 4 12 | 1 | %= 15 48 | 2 | *= 4 3 | 2 | %= 15 12 | 3 | *= 4 12 | 3 | %= 15 48 | 4 | *= 4 3 | 4 | %= 15 ... | ... | ... 12 | 9999 | *= 4 12 | 9999 | %= 15 48 | 10000 | *= 4 3 | 10000 | %= 15 Seems to come out to 3.
How could you tell what x's value would be if you weren't given the exact values of start and end? (Hint: It might be formulaic.)
According to our work above, whenever the number of iterations is odd, x ends up as 12. And whenever the number of iterations is even, x ends up as 3. (Watch out — don't fall into the trap that the value of x relates to the value of end. It is actually related to the end - start + 1.)
Someone else has written a function with prototype 'void menu(void);'. Their function will print this output on the screen for the user:
1) enter X 2) enter Y 3) Add Them 4) Quit Choice:
(Where the is the cursor's position.)
Write all code necessary to read and process the user's choice from this menu.
(Note again: You do NOT have to code the menu function since someone else already did that for you! You may, however, want to call it...)
(Hint: What looping structure is prefered for menu-like situations?)
(Hint 2: What branching structure is prefered for menu-like situations?)
bool quitting; char choice; quitting = false; do { menu(); cin >> choice; cin.ignore(numeric_limits<streamsize>::max(), '\n'); switch ( tolower(choice) ) { case '1': case 'x': { // input x } break; case '2': case 'y': { // input y } break; case '3': case 'a': case 't': { // total x and y } break; case '4': case 'q': { quitting = true; } break; default: { // invalidity message } break; } } while ( ! quitting );
TRUE✗ | FALSE✓ | A while loop is a post-test loop. | ||
---|---|---|---|---|
TRUE✓ | FALSE✗ | A for loop is a pre-test loop. | ||
TRUE✗ | FALSE✓ | A do loop is a pre-test loop. |
TRUE✓ | FALSE✗ | A do loop is an indefinite loop. | ||
---|---|---|---|---|
TRUE✓ | FALSE✗ | A while loop is an indefinite loop. | ||
TRUE✗ | FALSE✓ | A for loop is an indefinite loop. |
TRUE✗ | FALSE✓ | The @ operator can be used to replace a for loop with a single operation. | ||
---|---|---|---|---|
TRUE✗ | FALSE✓ | A do loop and a while loop are so fundamentally different that one cannot be used in place of the other. |
Rewrite the following while as a for structure. (Thought Provoker: There could be only one cout statement when you are done. But the behavior would slightly change and it would require you to use of a ?: operation.)
i = 1; while (i >= -10) { cout << i << ' '; i -= 2; } cout << i << endl;
for (i = 1; i >= -10; i -= 2) { cout << i << ' '; } cout << i << endl; Or, to answer the thought-provoker: for (i = 1; i >= -11; i -= 2) { cout << i << (i >= -10 ? ' ' : '\n'); } This version doesn't flush cout and has to check the value of i twice each loop — once in the for test and once in the cout.
Why can this loop be changed from a while loop to a for loop, anyway?
Since the beginning and ending values were known ahead of time, it was really more appropriate as a for loop. (Although the two loops are fundamentally identical in function, the for loop is typically reserved for situations which in the number of repetitions is known in advance.)
Show what is output by the loop (whether it is a while loop or a for loop). (Even if you did the "single cout" idea above, the output is actually the same — only the "behavior" during output changes.)
1 -1 -3 -5 -7 -9 -11
Show a do loop equivalent in execution to the following while loop. (Hint: You may need extra nesting...maybe even a branch...)
cout << "Enter a positive value: "; cin >> value; while (value <= 0) { cerr << "Invalid -- I said positive!\a\n\n"; cout << "Enter a positive value: "; cin >> value; }
do { cout << "Enter a positive value: "; cin >> value; if (value <= 0) { cerr << "Invalid -- I said positive!\a\n\n"; } } while (value <= 0);
Which form of the loop do you prefer — the do or the while? Why?
The while version repeats the prompt and read operations, but a good compiler will be able to optimize this away. The do version repeats the test of value's positiveness, which even fairly good compilers cannot avoid. In addition, the do loop has an extra level of nesting/indention which makes it a little uglier from a style perspective.
You are given this function:
bool toss(void);
which returns true when a coin comes up heads and false when the coin lands tails up.
Use this function in writing a loop to tell the user how many times their virtual coin came up heads and how many times it came up tails. You should keep count during a number of tosses which they will specify to you before-hand.
(Hints: This will require a nested branch. What kind of loop would be best for this situation — where the number of tosses is known ahead of time?)
short count, heads; // same type as tosses... heads = 0; for ( count = 0; count < tosses; count++ ) { if ( toss() ) { heads++; } } cout << "During the " << tosses << " coin tosses we got " << heads << " heads and " << tosses - heads << " tails.\n";