Arrays in C++
Arrays are used to store multiple values of same type like integer, string or float. The elements in the array are indexed starting from 0. Any element in the array can be accessed by its index number.
The following program shows a string type array named fruits which stores five elements. The elements in the array which are fruit name is accessed through its index number.
#include<iostream>
using namespace std;
int main () {
string fruits [5] = {"Mango", "Apple", "Grapes", "Banana", "Orange"};
//Print the 3rd element
cout<<"The third element in the array fruits is "<<fruits[2]<<endl;
//Print the fifth element
cout<<"The fifth element in the array fruits is "<<fruits[4];
return 0;
}
Output:
The third element in the array fruits is Grapes
The fifth element in the array fruits is Orange
An array can be multidimensional. A two dimensional array is in tabular form having rows and columns. The following program shows a two dimensional array ‘num’ of type integer having 6 elements which has 2 rows and 3 columns. It prints the array elements with their index number.
#include<iostream>
using namespace std;
int main () {
//Array having 2 rows and 3 columns
int num[2][3] = {{0,1,2},{3,4,5}};
//Printing all the values with the index number
for (int i = 0; i< 2; i++) //row index
{
for (int j = 0; j<3; j++) //column index
{
cout<<"The element at index "<<"["<<i<<"]["<<j<<"] "<<"is "<<num[i][j]<<endl;
}
}
return 0;
}
Output:
The element at index [0][0] is 0
The element at index [0][1] is 1
The element at index [0][2] is 2
The element at index [1][0] is 3
The element at index [1][1] is 4
The element at index [1][2] is 5
A three dimensional array is also in a tabular form with one extra dimension. Its syntax is ‘data_type name_of_array[number of elements in one extra dimension][rows][columns];. The following program shows a three dimensional array of type integer which has (2*3*4=24) elements and prints its elements with their index number.
#include<iostream>
using namespace std;
int main () {
//Array having 2 elements in extra dimension, 3 rows and 4 columns
int num[2][3][4]={ { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{ {13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
//Printing all the values with the index number
for (int i = 0; i<2; i++) // element index
{
for (int j = 0; j<3; j++) // row index
{
for (int k = 0; k<4; k++) // column index
{
cout<<"The element at index "<<"["<<i<<"]["<<j<<"]["<<k<<"] "<<"is "<<num[i][j][k]<<endl;
}
}
}
return 0;
}
Output:
The element at index [0][0][0] is 1
The element at index [0][0][1] is 2
The element at index [0][0][2] is 3
The element at index [0][0][3] is 4
The element at index [0][1][0] is 5
The element at index [0][1][1] is 6
The element at index [0][1][2] is 7
The element at index [0][1][3] is 8
The element at index [0][2][0] is 9
The element at index [0][2][1] is 10
The element at index [0][2][2] is 11
The element at index [0][2][3] is 12
The element at index [1][0][0] is 13
The element at index [1][0][1] is 14
The element at index [1][0][2] is 15
The element at index [1][0][3] is 16
The element at index [1][1][0] is 17
The element at index [1][1][1] is 18
The element at index [1][1][2] is 19
The element at index [1][1][3] is 20
The element at index [1][2][0] is 21
The element at index [1][2][1] is 22
The element at index [1][2][2] is 23
The element at index [1][2][3] is 24