Question:
An example was given from a textbook.
Task: Students were asked to rate the quality of food in the student cafeteria on a scale of 10. Put 40 responses into an array of integers and sum the results of the polls.
int main()
{
setlocale(LC_ALL, "Russian");
const int respenseSize = 40, frequencySize = 11;
int responses[respenseSize] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
int frequency[frequencySize] = { 0 };// ?
for (int answer = 0; answer < respenseSize; answer++)
++frequency[responses[answer]];
cout << "Рейтинг" << " " << "Частота" << endl;
for (int rating = 1; rating < frequencySize; rating++)
cout << " " << rating << " " << frequency[rating] << endl;
return 0;
}
Question: int frequency[frequencySize] = { 0 };
did we reset the first initial value? so that on the screen as we launched the program there was no 0 0 in the column or something deeper. The code is all clear except for this part. Thank you.
Answer:
There is the following chain of quotes from the C++ standard that explains how array elements are initialized when an array of the form is initialized
int frequency[frequencySize] = { 0 };
The first thing to note is that arrays are aggregate types . Therefore, we turn to the section where the initialization of aggregate types 8.5.1 Aggregates is described:
7 If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equalinitializer, from an empty initializer list (8.5.4).
This excerpt from the standard says that if there are fewer initializers than there are elements in the array, then the elements of the array that do not have an explicit initializer are initialized as if empty curly braces were specified for each element. That is, it can be represented as follows
int frequency[frequencySize] = { 0, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} };
Let's now turn to the standard for an explanation of what it means when there are empty curly braces. In paragraph 3 of section 8.5.4 List-initialization, it says
— Otherwise, if the initializer list has no elements, the object is value-initialized
What does value-initialized mean? This is written in section 8.5 Initializers of the standard
8 To value-initialize an object of type T means:
– otherwise, the object is zero-initialized .
And finally, the concept of zero-initialized means (8.5 Initializers)
6 To zero-initialize an object or reference of type T means:
– if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T ;
That is, all array elements that have not been explicitly given initializers are initialized to zero. Therefore, in the end, this entry
int frequency[frequencySize] = { 0 };
is equivalent to the following entry:
int frequency[frequencySize] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
And, as a result, we get that the record
int frequency[frequencySize] = { 0 };
is equivalent to writing
int frequency[frequencySize] = {};
Keep in mind that the last two entries are not equivalent in C. For the C language, the entry
int frequency[frequencySize] = {};
is wrong, and the compiler will issue an error message.