2. Provide the output of the following code snippets exactly as it would appear
ID: 3827023 • Letter: 2
Question
2. Provide the output of the following code snippets exactly as it would appear on the console. If there are multiple spaces, denote the number of spaces below your answer. (Assume the appropriate libraries are included.)
a. int j = 2, number = 1;
switch (j)
{ case 1: number *= 3;
case 2: number += 2;
case 3: number *= number;
default: number--;
break;
}
cout << number << " ";
b. cout << 2 * 4 / 3;
c. string s = "3";
int t = 3;
cout << s + s << 3 + 3 << " 3 + 3 " << t + t;
d. double number = 23.325;
cout << fixed; cout << setprecision(2); cout << number;
e. double number = 23.325; //cout << fixed; cout << setprecision(2);
cout << number; f. cout << setw(5) << left << "left" << "." << setw(6) << right << "right";
g. string book = "Catch-22";
book.erase(2, 3);
cout << book;
h. cout << "\ "";
i. for (int m = 2; m <= 15; m += 3)
{ cout << m << " "; }
j. int loop_count = 0;
for (int j = 0; j <= 100; j++)
{ loop_count++; }
cout << loop_count;
k. int x = 0, y = 0, loop_count = 0;
while (x < 5)
{ while (y < 5) { loop_count++; }
x++; y = 0; }
cout << loop_count;
Explanation / Answer
a. int j = 2, number = 1;
switch (j)
{ case 1: number *= 3;
case 2: number += 2;
case 3: number *= number;
default: number--;
break;
}
cout << number << " ";
a) 8
answer will be 8 as no breaks are used
b)cout << 2 * 4 / 3
b) 2
answer will be 2 (8/3 = 2)
c. string s = "3";
int t = 3;
cout << s + s << 3 + 3 << " 3 + 3 " << t + t;
c) 336 3 + 3 6
d. double number = 23.325;
cout << fixed; cout << setprecision(2); cout << number;
d) 23.32
fixed 2 decimal points
e. double number = 23.325; //cout << fixed; cout << setprecision(2);
cout << number;
e) 23.325
f) cout << setw(5) << left << "left" << "." << setw(6) << right << "right";
f) left . right
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.