Arrays are made of compound data type or data structure and where elements are stored at contiguous location. which means you can access those elements using their indices. All elements in an array are of the same type. and also arrays are fixed in size, which means it can’t shrink and grow in their runtime. So, if you want to increase or decrease length of an array. then you would have to go back to source code to change it’s size.
Why do we need array?
To answer this question, I really have one good example. So, think of situtation where we are storing scores of 3 different matches. First way of doing this could be something like this:
int test_match_0 = 18;
int test_match_1 = 42;
int test_match_2 = 130;
In this basically we’ve stored scores of different matches in these 3 different variables respectively. But here you can also use an array, as you can see all scores are integer type data. which means we can store these scores inside our array of integers, like this:
int match[] = {18, 42, 130};
And here you can access score of any match by using index. match[0]
will return 18. Although you can also store these values in variables and thats totally fine. But think of situation when there would be more than 100 matches. In that case it’s not good thing to create 100 different variables to store 100 different matches score.
Things to note in Arrays in C++:
- Fixed in Size: means you can’t change it’s size in runtime.
- Elements are of them type: All the items in arrays are of same type.
- Stored at contigious memory location: means you can access elements using their indexes.
- First element at 0.
- Last element at -1.
- There is no option to check if you are out of bounds. which means let say you defined an array of 5 elements. Now if you would try to access 6th element then it compiles without any error. So, you have to keep that in mind when you are doing any loop or accessing item from array.
Declaration and Intialzing and Array:
Declaring an array is pretty much simple, its format is something like this:
<type_of_data> array_name [<constant_no_fixed_size>]
So, it starts with defining type of data following array name and the size of the array.
int test_scores [10] // Array to store int type data of 10 different matches
Now, lets see initialization as its good to intialize while declaring an array.
int test_scores[5] {2,24,21,223,23}; // Fully initialized
std::string students[100] {'PK', 'Naruto'}; // 0 is PK and 1 would be Naruto and rest of them are intialized to 0
double temperature[30] {0}; // All elements are initialized to 0
int roll_no[] {2,3,4,6,9} // size automatically calculated
Always initialize your array, because if you won’t then there would be garbage value that you can’t rely on.
Accessing and Modifiying Elements in Array:
Accessing element in Array is very easy. All you need to know the index of given element in order to access that.
int test_scores[4] {232,3,43,435};
cout<< test_scores[0] << endl; // 232
cout<< test_scores[1] << endl; // 3
cout<< test_scores[2] << endl; // 43
cout<< test_scores[3] << endl; // 435
Similiary you can modify any given element by redefining item at given index.
test_score[1] = 343;
You may also like: Vectors in C++
So, thats it for array in general. I hope you liked this article, if you liked it then don’t forget to share this with other people.
Thanks for reading:)