Introduction to Array in C++
Array is a collection of values having same data type and size. Elements of array are internally stored in consecutive locations. We can access individual elements of array by using index of array elements where index is an integer value starting from 0.
Types of Array in C++
There are three types of array:
- One dimensional Array
- Two dimensional Array
- Multi dimensional Array
One dimensional Array in C++
In one dimensional array, we use only one subscript to specify the size or refer any array element. Syntax for declaring an array is:
<Type> Array_name[N];
Type represents valid data type of C++ like int, float, char etc.
Array_name is the array name defined by the programmer.
[ ] are called subscript used to define array size in them.
N is the maximum number of values that can be stored in array.
Example
int ARR[4];
Here ARR is a one dimensional array which can store maximum 4 values in it. Elements of array are referred as ARR[0],ARR[1],ARR[2] and ARR[3]. Elements of array are stored as follows:
Suppose ARR[0] is stored at memory location 1000.
ARR[1] gets stored at location 1004.
ARR[2] is stored at memory location 108 and so on.
There is a gap of 4 between two consecutive addresses because size of int data type is considered as 4 bytes here .
Program for one dimensional array in C++
#include<iostream>
using namespace std;
int main()
{
int ARR[3];
ARR[0]=10;
ARR[1]=5;
ARR[2]=20;
cout<<"ARR[0]="<<ARR[0];
cout<<"\nARR[1]="<<ARR[1];
cout<<"\nARR[2]="<<ARR [2];
return 0;
}
Output
ARR[0]=10 ARR[1]=5 ARR[2]=20
*In the above program, we have declared an array ARR having size 3. This array can contain three values. First element of array is named as ARR[0]. Second element is named as ARR[1] and Third element is named as ARR[2].
Initialization of one dimensional Array in C++
Syntax to initialize a one dimensional array is:
<Type> Array_name[N]={List_of_values};
Type represents valid data type of C++ like int, float, char etc.
Array_name is the array name defined by the programmer.
[ ] are called subscript used to define array size in them.
N specifies the size of array. It is optional while initializing array. Compiler automatically provides size to array as per number of values within the braces if we don’t specify array size during initialization.
Example
int ARR[3]={15,25,35}; //Initialization of int type array
Program to initialize one dimensional array in C++
#include<iostream>
using namespace std;
int main()
{
int ARR[3]={4,6,7}; //Initialization of array
int i;
cout<<"\nArray elements are:";
for(i=0;i<3;i++)
cout<<endl<<ARR[i];
return 0;
}
Output
Array elements are: 4 6 7
In the above program, we have initialized an array ARR having size 3. This array contains three values. First element of array i.e ARR[0] contains 4, second element of array i.e. ARR[1] contains 6 and third element ARR[2] contains 7. We can also leave the size blank while initializing the array.
Program to read and display one dimensional array in C++
#include<iostream>
using namespace std;
int main()
{
int ARR[3];
int i;
cout<<"\nEnter elements in Array :";
for(i=0;i<3;i++)
cin>>ARR[i];
cout<<"\nArray elements are:";
for(i=0;i<3;i++)
cout<<endl<<ARR[i];
return 0;
}
Output
Enter elements in Array :5 6 9 Array elements are: 5 6 9![]()

