Array in C++ is a group of similar types of elements that have contiguous memory location.
Index starts from 0. We can store only fixed set of elements in C++ array.
Random Access
Easy to traverse, manipulate and sort
Two array types:-
- Single Dimensional Array
int arr[5]={10, 0, 20, 0, 30};
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
- Multidimensional Array - 2D, 3D and so on
They are known as rectangular arrays in C++ since they can be two dimensional or three dimensional. The data is stored in tabular form (row ∗ column) which is also known as matrix.
Example-
int main()
{
int test[3][3] =
{
{2, 5, 5},
{4, 0, 3},
{9, 1, 8}
};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout<< test[i][j]<<" ";
}
cout<<"\n"; //new line at each row
}
return 0;
}