If you want to record the scores of 50 students in a C++ programming exam, how would you do it? If you haven’t learned about arrays yet, you can only use individual variables to store the scores.
int score1 = 95;
int score2 = 80;
int score3 = 85;
…………
int score50 = 100;
If you want to calculate the average score of the class, you would have to write a very long formula: (score1 + score2 + … + score50) / 50. What if there are 500 students in the entire grade? The code would be excessively long!
We can use an array to store the scores: int score[50]. Here, int represents that the scores are integers, score is the name of the array followed by an index. [50] indicates that it can hold 50 consecutive students.
score[0] = 95; // Score of student 1 is 95
score[1] = 87; // Score of student 2 is 88
score[2] = 92; // Score of student 3 is 98
// …
score[49] = 100; // Score of student 50 is 100
The magic of arrays is that they can become the best friends with for loops! The previous method of assigning values one by one is still too cumbersome. If we want to input scores from the keyboard, we can do it like this:
for(int i = 0; i < 50; i++) {
cin >> score[i]; // Input each student’s score 50 times in a loop
}
With just 3 lines of code, we solve the problem of inputting 50 scores! Similarly, calculating the total score and average score becomes very easy:
int sum = 0;
for(int i = 0; i < 50; i++) {
sum = sum + score[i]; // Accumulate each student’s score into sum
}
double average = sum / 50.0;
cout << “The class average score is:” << average << endl;
Isn’t it super convenient?
1. One-Dimensional Arrays1. DefinitionData Type Array Name[Constant Expression]Data Type: Integer, Float, Character, Boolean, etc.Array Name: A valid variable name.Constant Expression: An integer that limits the number of elements, which is the length of the array. The length of the array cannot be a variable.2. Array ElementsArray Name[Index], where the index can be any integer expression. Accessing an index out of range is called an out-of-bounds error. The index allows flexible control over the elements in the array.Array indices start from 0. For example, in int num[10], the first element is num[0].2. Array Initialization1. FormatData Type Name Array Name[Constant Expression] = { First Value, Second Value, … }2. It is important to pay attention to the initialization of array elements; otherwise, it can lead to errors. If the number of initial values is less than the number of array elements, the remaining elements will be initialized to 0. If the array is not initialized, the values of the array elements will be random.3. Element Initialization(1) All elements initialized, assigning initial values to all elements. For example, int a[5] = {0, 1, 2, 3, 4}.(2) Partially initialized elements, assigning initial values to only some elements. For example, int a[5] = {0, 1, 2}. The uninitialized parts default to 0.(3) To initialize all array elements to 0, it can be simplified to {0}. For example, int a[5] = {0}.3. Array Out-of-BoundsAccessing an array element that is not within the array’s storage space is called an out-of-bounds error. Out-of-bounds errors are common in programming; the system does not prompt for out-of-bounds access, and the program does not immediately show an error. Care must be taken when programming. Out-of-bounds access can cause the program to access memory units beyond the array’s boundaries, leading to memory corruption.4. Two-Dimensional Arrays1. Format:Data Type Array Name [Constant Expression 1][Constant Expression 2]Constant Expression 1 represents rows, and Constant Expression 2 represents columns. The indices for rows and columns also start from 0. Initialization values are similar to one-dimensional arrays; they can be fully assigned or partially assigned. For partial assignments, uninitialized values default to 0.2. Referencing Two-Dimensional Array ElementsArray Name [Index 1][Index 2]5. Multi-Dimensional ArraysWhen an array has multiple indices, it is called a multi-dimensional array, such as a three-dimensional array or four-dimensional array.C++ Collection:1. C++ Programming Beginner’s Preparation Guide2. Basic Knowledge of C++ Language3. C++ Input and Output4. Essential Vocabulary for C++ Beginners5. C++ Selection Structures6. C++ Loop Structures7. Nested Loops in C++