140k views
2 votes
Which of the following does not declare a 2-by-2 array and set all four of its elements to 0?

a. array, 2> b;
b[0][0] = b[0][1] = b[1][0] = b[1][1] = 0;
b. array, 2> b = {0};
c. array, 2> b;
for (auto const &row : b) {
for (auto &element : row) {
element = 0;
}
}
d. All of the above initialize all four of the array elements to 0.

User Fermmm
by
7.9k points

1 Answer

3 votes

Final answer:

Each of the options given correctly initializes a 2x2 array with all elements set to 0. Therefore, none of the options fail to declare and initialize the array, making the answer 'All of the above'.

Step-by-step explanation:

You want to know which option does not declare a 2-by-2 array and set all four of its elements to 0. Let's examine each option:

  • a. array, 2> b; b[0][0] = b[0][1] = b[1][0] = b[1][1] = 0; This line manually assigns all values in the array to 0. This correctly initializes a 2x2 array with zeros.
  • b. array, 2> b = {0}; This line uses aggregate initialization, where providing a single zero will default-initialize the rest of the array elements to 0 as well. This also correctly initializes a 2x2 array with zeros.
  • c. array, 2> b; Performing a loop with for (auto const &row : b) and for (auto &element : row), setting each element to 0, will also properly initialize all elements of the array to 0.

All provided code snippets properly initialize a 2x2 array with zeros.

Therefore, the answer is d. All of the above initialize all four of the array elements to 0.

User Chimbu
by
8.3k points