Self-test 3:
Control flow (Loops)
-
What will be printed by the following code segment?
int i = 9; while (i >= 0) { cout << i << " "; i = i-1; }Solution9 8 7 6 5 4 3 2 1 0
-
What will be printed by the following code segment?
int i = 0; while (i < 10) { cout << 9-i << " "; i = i+1; }Solution9 8 7 6 5 4 3 2 1 0
-
What will be printed by the following code segment?
int i = 9; while (i >= 0) { cout << i << " "; i = i-1; cout << i << " "; i = i-1; }Solution9 8 7 6 5 4 3 2 1 0
-
What will be printed by the following program?
int N = 100; int i = 0; int count = 0; while (i < N) { if (i%2 == 0) { count = count+1; } } cout << count << endl;SolutionNothing! The program will enter into an infinite loop because no codes modify i inside the while-loop. As a result, i will keep its value of zero forever and the while-loop condition is always true.
-
What is printed by the following program?
int i = 0; int j = 0; while (i < 3) { while (j < 3) { cout << j << " "; j = j+1; } cout << endl; i = i+1; }-
0 1 2 0 1 2 0 1 2
-
0 1 2 3 4 5 6 7 8
-
0 1 2
SolutionC.
When the outer while-loop executes for the first time, the inner while-loop executes 3 times. Hence, 3 numbers are printed. After that, j remains at the value of 3. So during the execution of the 2nd and 3rd times of the outer while-loop , the inner while-loop is not executed and only two newlines are printed.
-
-
Can the following code segment be compiled and executed? Can the code segment escape from the infinite loop? What will be the final results of a and b after getting out of the while loop?
int a = 100; int b = 200; while (true) { if (a >= b) continue; a = a+1; b = b-1; }SolutionIt can be compiled and executed without errors but it will end up in an infinite loop because the continue-statement will only go back to check the statement containing the boolean expression of the nearest loop, in this case, the statement: while (true).
-
Write C++ codes to print the following pattern?
******* ***** *** *
SolutionSolution 1:
cout << "*******" << endl; cout << " ***** " << endl; cout << " *** " << endl; cout << " * " << endl;
Alternative Solution:
for (int row = 0; row < 4; ++row) { // Output leading space for (int j = 0; j < row; ++j) cout << ' '; // Output leading space for (int j = 0; j < 2*(4-row)-1; ++j) cout << '*'; cout << endl; }
-
Write a program to print the checker pattern with varying size.
e.g. given: int SIZE = 4; 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 e.g. given: int SIZE = 5; 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
Solutionint SIZE = 4; int row = 0; int col = 0; while (row < SIZE) { col = 0; while (col < SIZE) { cout << (row+col)%2 << " "; col = col+1; } cout << endl; row = row+1; }
-
What is the output of the above program when the user input is 5?
#include <iostream> using namespace std; int main(void) { int i = 0, j; int number; cin >> number; i = number; while (i > 0) { j = 0; while (j < number-i) { cout << " "; j = j+1; } j = 0; while (j < i) { cout << "*"; j = j+1; } cout << endl; i = i-1; } }-
***** **** *** ** * -
* ** *** **** *****
-
***** *** *
-
***** **** *** ** *
SolutionA
-
-
What is printed by the following program?
int num = 3474; int sum = 0; while (num > 0) { sum = sum + num%10; num = num / 10; } cout << sum << endl;Solution18. Variable sum is the sum of all individual digits of variable num.