24.7k views
1 vote
Which of the following is not a correct way to initialize the array named n? Lütfen birini seçin:

a. array< int, 5 > n = {0,7,0,3,8,2};
b. array< int, 5 > n = {0,7,0,3,8 };
c. array< int, 5 > n = {9, 1,9};
d. array int, 5 > n = {7};

1 Answer

1 vote

Final answer:

The incorrect way to initialize the array named n is option a, which provides 6 values, exceeding the declared size of the array, which is 5.

Step-by-step explanation:

The question is about initializing a fixed-size array using the array template in C++. When initializing such an array, the number of elements specified during the array declaration must match the number of elements provided in the initializer list, unless fewer elements are provided, in which case the remaining elements are default-initialized. In this case, the array named n is declared with a size of 5.

Option a. array< int, 5 > n = {0,7,0,3,8,2}; is not the correct way to initialize the array named n because it contains 6 values, which exceeds the declared array size of 5.

Options b, c, and d are all valid ways to initialize the array n. In option b, the initializer list fills the array exactly. Option c provides fewer elements than the array's declared size, which is allowed, with the rest being default-initialized to 0. Option d initializes the first element of the array and leaves the remaining elements to be default-initialized.

User Johan Karlsson
by
8.7k points