Arrays are simply "series" of variables of the same type.
Imagine you want 3 integers. You could name them myInt1, myInt2 and myInt3. But those names have very little meaning, and it's difficult to use them.
Instead, we can just make an array of 3 ints:
int myInt[3];
Now, we have three ints, grouped together under a single name. We can access them individually, with the accessor operator ([]) by using the index:
int theFirstInt = myInt[0];
This accessor + index notation allows us to loop over arrays pretty easily. Imagine I have an array with all the scores of a test and I want to sum them all:
1 2 3
|
int scores[20]; // Assign values to scores somewhere
int sum = 0;
for (int i = 0; i < 20; ++i) { sum += scores[i]; }
|
Imagine having to do that with 20 separately named integers! Or 50! Or 5000!
Thus, arrays allow:
-Grouping variables under the same name.
-Looping/iterating over all variables.
-Locating specific values by using their index. If I want the 5th score, I can access it with
scores[4]
.
That's all there is to it!