Question 9
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<int>> g(3, std::vector<int>(2, 1));
g[1].push_back(5);
g.push_back({7});
int total = 0;
for (const auto& row : g)
for (int x : row) total += x;
std::cout << g.size() << ' ' << g[1].size() << ' ' << g[3][0] << ' ' << total << '\n';
return 0;
}
- A 4 3 7 18 Correct
- B 3 3 7 18
- C 4 2 7 13
- D 4 3 5 18
Correct Answer: Option A
Three rows of two 1s (sum 6), row 1 gets an extra 5, and a fourth row {7} is added: 4 rows, row 1 has 3 elements, total 6 + 5 + 7 = 18.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!