PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Look at this declaration: it creates one array named scores with room for four int elements. The element type is int, the array name is scores, the element count is 4, and the initializer list supplies the starting values. The 4 does not identify an element. It tells C++ how many elements the array contains.
int scores[4] = {7, 4, 9, 2};What does the 4 mean in int scores[4] = {7, 4, 9, 2};?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The initializer list places its values in order. The first value, 7, goes into the first position, followed by 4, then 9, then 2. Every position has the declared type int. The declaration fixes this array at four elements, so the list describes four values for four positions.
When you read an element, the number in brackets is its index. Index 0 means the first element, index 1 means the second, index 2 means the third, and index 3 means the fourth. Therefore, an array with four elements has valid indices 0-3. Its final value, 2, is read with scores[3], not scores[4].
scores[0] // reads 7
scores[1] // reads 4
scores[2] // reads 9
scores[3] // reads 2Type the exact expression that reads the final value 2 from scores.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The same brackets have different meanings depending on where they appear. In int scores[4], the 4 declares how many elements scores contains. In scores[3], the 3 selects one element from that array. The declaration uses a count, while the access expression uses a zero-based index.
cout << scores[0] << endl; // prints 7
cout << scores[2] << endl; // prints 9
cout << scores[3] << endl; // prints 2