DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSC++ FUNDAMENTALS

Arrays: Counts and Zero-Based Indices

Reading · 3 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

The number in an array declaration is an element count

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.

CPPThe declaration creates four int elements in the array named scores.
int scores[4] = {7, 4, 9, 2};
CHECKPOINT 1Not answered

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.

Initializer values fill the array from left to 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.

the array int scores[4] = {7, 4, 9, 2}; shown as four ordered slotsThe declaration int scores[4] = {7, 4, 9, 2}; appears above four adjacent slots under the name scores. The slots contain 7, 4, 9, and 2 from left to right. Their index labels are 0, 1, 2, and 3. The 4 in the declaration is labeled as the element count, not as an index.int scores[4] = {7, 4, 9, 2};74924 = number of elementsscores0123four ordered slots are addressed by indices 0 through 3
A count of 4 produces indices 0-3.

The number in an access expression is a zero-based index

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].

CPPEach valid index reads one value from scores.
scores[0]  // reads 7
scores[1]  // reads 4
scores[2]  // reads 9
scores[3]  // reads 2
CHECKPOINT 2Not answered

Type 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.

Brackets mean count in a declaration and index in an expression

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.

CPPDirect access expressions read selected values from the same array.
cout << scores[0] << endl;  // prints 7
cout << scores[2] << endl;  // prints 9
cout << scores[3] << endl;  // prints 2
Previous · Nested FunctionsNext part · Quiz