Clicker Questions - 05-More-Review
(BTW, I’m omitting the “include” and “using” lines to conserve space. Just pretend that the correct ones are there.)
(Also, don’t print the final “newline” – just answer with the string that is printed.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
int main()
{
int i, n;
string s;
string rv;
rv = "";
while (cin >> n) {
for (i = 0; i < n; i++) {
if (cin >> s) rv.push_back(s[0]);
}
}
cout << rv << endl;
return 0;
}
|
input-1.txt
1 Fred
-
Question 1: What is the output of the program on the right when it runs with input-1.txt as standard input?
Answer
- The program reads an integer n, and then iterates n times.
- For each iteration, it reads a string, and if that was successful, appends the first character of the string to the string rv.
- The program repeats this process until reading n fails.
- Then it prints out rv.
So, with input 1, it reads the integer 1, then reads the string "Fred", and appends the 'F'
to rv. The answer is
F
input-2.txt
2
Marco
Polo
3
Drop
Dead
Fred
-
Question 2: What is the output of the program on the right when it runs with input-2.txt as standard input?
Answer
- It reads the number 2 and then "Marco" and "Polo". At this point, rv is "MP".
- It reads the number 3 and then "Drop", "Dead" and "Fred". At this point, rv is "MPDDF".
The answer is:
MPDDF
input-3.txt
3 A BB
CCC 2 Xerxes
Yassin Zelda
-
Question 3: What is the output of the program on the right when it runs with input-3.txt as standard input?
Answer
- It reads the number 3 and then "A", "BB" and "CCC". At this point, rv is "ABC".
- It reads the number 2 and then "Xerxes" and "Yassin". At this point, rv is "ABCXY".
- It tries to read a number, but it fails because the next word is "Zelda". So it falls out of the while() loop.
The answer is:
ABCXY
input-4.txt
3 7 5
3 4 7
6 8 3
9 8 3
-
Question 4: What is the output of the program on the right when it runs with input-4.txt as standard input?
Answer
- It reads the number 3 and then "7", "5" and "3". At this point, rv is "753".
- It reads the number 4 and then "7", "6", "8" and "3". At this point, rv is "7537683".
- It reads the number 9 and then "8" and "3". At this point, rv is "753768383".
- It tries to continue reading strings, but it reaches EOF, so the cin statement fails.
- It now tries to read an integer, but the cin is still in a failed state, so it out of the while() loop.
The answer is:
753768383
Class Stats, PDF Download