“To array a man’s will against his sickness is the supreme art of medicine” Henry Ward Beecher
Arrays are compound data-types in that we can store a range of values.
For example, let’s say we need to write a program that needs to store 10 integer values. Well, one way to do that is by creating 10 variables and storing each number in each variable.
But the better way is to create an array of type integer and then assign all those values to this array.
Example:
#include <iostream>
using namespace std;
int main() {
int list[10] = {1,2,3,4,5,6,7,8,9,10};
return 0;
}
In this example the list variable is an array that can hold 10 values of type int and we just initialized it with 10 different numbers.
Alright, now let’s get into the structure of an array and see how we can create one:
Data-type array-name [number-of-elements];
`data-type`: the data-type declares what type of data can be stored in the array. For example if the data-type was `int` then we can only assign integer numbers to the target array.
`array-name`: arrays just like other types of variables needs name so that we can access its content in later times.
Note: the rules that apply to name of a variable also apply to the name of an array as well.
`[]`: After the name of an array, comes the brackets. The brackets are sign for the compiler and developers that the target variable is an array and not a variable that can take only single value.
`number-of-elements`: within the brackets we can declare how many elements we want this array to store.
Notes:
-
The number here is of type integer like (1, 10, 20, 1000 etc.).
For example if we set the value to 10, it means the number of elements
that the array can store is 10. -
If we only declare an array and not initialize it, then the
`number-of-elements` is mandatory. -
If we declare and initialize an array at the same time, then the
`number-of-elements` is optional. In such case if we skipped the number
of elements, then the compiler will automatically calculate the size of
the array. -
The total size in bytes that the array will have is the
multiplication of the `number-of-elements` to the byte-size of one
single element of the array.
For example if there are 10 elements in an array and the type of that array is int, that means the size of one element is 4 bytes and so 10 * 4 is 40 bytes the total size of the array.
Initialization of an array:
We can initialize an array at the time of declaration.
This is the structure that we use for initialization:
Data-type variable-name[number-of-elements] = { value_one, value_two , value_three , value_four , value_five , value_six};
`{}`: To initialize an array with values, we use curly brackets (AKA braces). Within the body of the braces, we set the values we want for the array to store.
Note: each value in the curly brackets is separated via comma ,.
Example:
int range[10] = { 1,2 ,3 , 4,5 ,6};
In this example, the array’s name is range the type is int and we assigned 6 values to this array.
Notes:
-
Because the size of the array is 10 elements but we just assign 6
elements, the first 6 items in the array will have these 6 elements and
the last 4 items of the array will have 0 as their initial
value. -
If the size of an array is set, we can’t initialize more values
than the declared size in that array.
Another example:
int myNumber[] = { 1000,200 ,325 , 466,587 ,600};
In this example, the array doesn’t have the size explicitly declared and so as a result, the actual size of this array is counted based on the number of values we assigned to the array. Which in this case because 6 values are assigned to the array, the size is 6.
Again, when only declaring an array, the size of the array is mandatory. The declaration below is wrong because we only declared the array but we didn’t set any size for it.
int myNumber[]; //WRONG…
Assigning and retrieving values to/from arrays:
Arrays have index number and it starts from 0. We use the index number to reach the target element in the array in order to retrieve or assign a value in that index.
This is how we use index numbers in arrays:
Array-name[index-number]
`array-name`: this is the name of the target array.
`[]`: after the name of an array, comes the brackets and within the brackets we set the index number.
Note: because the first item in an array is at index 0, the final item in that array will be at index number-of-elements -1.
For example let’s say an array has 10 items; the first element in that array is at index 0 and the final element will be at index number 9.
Example:
#include <iostream>
using namespace std;
int main() {
int age[10];
age[0] = 10;
age[1] = 20;
age[2] = 30;
age[3] = 40;
age[4] = 50;
age[5] = 60;
age[6] = 70;
age[7] = 80;
age[8] = 90;
age[9] = 100;
cout<<"The value at index 4 is: "<<age[4];
return 0;
}
Output:
The value at index 4 is: 50
As you can see, in order to assign a value to an element of the age array, we put the array’s name and brackets on the left side of the assign = operator and the target value on the right side. This will replace the value that is already in that memory-space (if any) with the new value.
Also calling the array with brackets and index number in other places (other than being used as LValues) will return the value that is stored in the target index. This is what we did when called age[4] in the cout <<.
Getting the total space of an array:
We can also use sizeof operator, in order to get the total space of an array in bytes.
Example:
#include <iostream>
using namespace std;
int main() {
int age[10];
cout<<"The size of the array is: "<< sizeof(age);
return 0;
}
Output:
The size of the array is: 40
Constant arrays:
An array can be constant as well. But you should know that a constant array should be initialized when the array is being declared.
You can’t create a constant array and in later times, assign values to its elements.
Example:
#include <iostream>
using namespace std;
int main() {
const int range[] = {1,2,3,4,5,6,7,8};
return 0;
}