Arrays are used to store multiple values in a
Array single variable, instead of declaring separate
variables for each value.
an array is a data structure that is used to store
multiple values of similar data types in a
contiguous memory location.
For example, if we must store the marks of 4 or 5 students
then we can easily store them by creating 5 different
variables but what if we want to store marks of 100
students or say 500 students then it becomes very
challenging to create that numbers of variable and manage
them. Now, arrays come into the picture that can do it
easily by just creating an array of the required size.
Properties of Arrays in C++
An Array is a collection of data of the same data
type, stored at a contiguous memory location.
Indexing of an array starts from 0. It means the first
element is stored at the 0th index, the second at
1st, and so on.
Elements of an array can be accessed using their
indices.
Once an array is declared its size remains constant
throughout the program.
An array can have multiple dimensions.
Array Declaration in C++
In C++, we can declare an array by simply specifying the
data type first and then the name of an array with its
size inside a square brackets.
data_type array_name[Size_of_array];
Example: int arr [5];
int: It is the type of data to be stored in the array.
We can also use other data types such as char,
float, and double.
arr: It is the name of the array.
5: It is the size of the array which means only 5
elements can be stored in the array.
Another example
string cars[4];
We have now declared a variable that holds an array of four
strings. To insert values to it, we can use an array literal -
place the values in a comma-separated list, inside curly
braces:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of three integers, you could write:
int myNum[3] = {10, 20, 30};
Access the Elements of an Array
You access an array element by referring to the index
number inside square brackets [].
This statement accesses the value of the first element in
cars:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
Note: Array indexes start with 0: [0] is the first element. [1]
is the second element, etc.
Change an Array Element
To change the value of a specific element, refer to the
index number:
cars[0] = "Opel";
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
Exercise:
Create an array of type string called cars.
______ _______[4] = {"Volvo", "BMW", "Ford", "Mazda"};
Print the value of the second element in the cars array.
_______ _______[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << ____________;
Change the value from "Volvo" to "Opel", in the cars
array.
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
_______=_______ ;
cout << cars[0];
Loop through the elements in the cars array.
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
_____( _______= 0; __ < 4; _____);
{
cout << _____ << "\n";
}
Initialization of Array in C++
some most common ways to initialize an array. We can
initialize an array at the time of declaration or after
declaration.
1. Initialize Array with Values in C++
We have initialized the array with values. The values
enclosed in curly braces ‘{}’ are assigned to the array.
int arr[5] = {1, 2, 3, 4, 5};
Here, 1 is stored in arr[0], 2 in arr[1], and so on. Here the
size of the array is 5.
2. Initialize Array with Values and without Size in C++
We have initialized the array with values, but we have not
declared the length of the array, therefore, the length of an
array is equal to the number of elements inside curly
braces.
int arr[] = {1, 2, 3, 4, 5};
3. Initialize Array after Declaration (Using Loops)
We have initialized the array using a loop after declaring
the array. This method is generally used when we want
to take input from the user, or we want to assign
elements one by one to each index of the array. We can
modify the loop conditions or change the initialization
values according to requirements.
for (int i = 0; i < N; i++) {
arr[i] = value;
}
4. Initialize an array partially in C++
Here, we have declared an array ‘partialArray’ with size ‘5’
and with values ‘1’ and ‘2’ only. So, these values are
stored at the first two indices, and at the rest of the indices
‘0’ is stored.
int partialArray[5] = {1, 2};
5. Initialize the array with zero in C++
We can initialize the array with all elements as ‘0’ by
specifying ‘0’ inside the curly braces. This will happen in
case of zero only if we try to initialize the array with a
different value say ‘2’ using this method then ‘2’ is stored at
the 0th index only.
int zero_array[5] = {0};
Accessing an Element of an Array in C++
Elements of an array can be accessed by specifying the
name of the array, then the index of the element enclosed
in the array subscript operator []. For example, arr[i].
Example 1: The C++ Program to Illustrate How to Access Array Elements
Update Array Element
To update an element in an array, we can use the
index which we want to update enclosed within the
array subscript operator and assign the new value.
arr[i] = new_value;
Traverse an Array in C++
We can traverse over the array with the help of a loop
using indexing in C++. First, we have initialized an array
‘table_of_two’ with a multiple of 2. After that, we run a for
loop from 0 to 9 because in an array indexing starts from
zero. Therefore, using the indices we print all values stored
in an array.
Example 2: The C++ Program to Illustrate How to Traverse an Array
Size of an Array in C++
In C++, we can calculate the size of an array using sizeof()
operator trick. First, we find the size occupied by the whole
array in the memory and then divide it by the size of the
type of element stored in the array. This will give us the
number of elements stored in the array.
Syntax: data_type size = sizeof(Array_name) / sizeof(Array_name[index]);
Example 3: The C++ Program to Illustrate How to Find the Size of an Arra
y
Advantages of Array
Code Optimization: Less Code is required; one variable
can store numbers of value.
Easy to traverse data: Easy short the data using the
swapping technique
Random access: With the help of array index you can
randomly access any elements from the array.
Types of Array
• One Dimensional Array
• Two Dimensional Array
• Multidimensional Array
One dimensional Array is a group of elements having the
same data type and same name. Individual elements are
referred to using the common name and unique index of the
elements.
Syntax: Data type Arrayname[arraysize];
Example: int score[5]
Score [0] Score [1] Score [2] Score [3] Score [4]
5 10 15 20 25
The array variable score[5] is an integer data type and we
can store a maximum of 5 values into it. These values to
be stored or manipulated must be all integer in values.
Individual Value of Array Accessing One-
variable score [5]. Dimensional Array
Score [0] = 5; Score [0] = 5;
Score [1]=10; Score [1]=10;
Score [2]=15; Score [2]=15;
Score [3]=20;
Score [3]=20;
Score [4]=25;
Score [4]=25;
Other examples of One-dimensional Array
Declaration
int grades [100];
float temperature[50];
char username[120];
double salaries[250];
Example 4
Example 5
Example 6
Activity
Write a program using one-
dimensional array that will ask you
to give a series of numbers and
then program will check and display
the list of ODD and EVEN numbers
based on the given numbers by the
user.
Write a program using one-dimensional array
that will ask the user to input a range (starting
and ending numbers), and the program will
identify the odd and even numbers within that
range.
Two Dimensional Array