Types of Arrays in C++: C++ allows us to create multidimensional arrays. There are different types of arrays in C++. They are as follows:
- One Dimensional Array
- Two dimensional Array
- Multidimensional Array
One Dimensional Array in C++
A one-dimensional array in C++ can be defined as a group of elements having the same data type and the same name. And the individual elements are referred to using the common name and index of the elements.
Declaring One Dimensional Array in C++
The general form for declaring a one-dimensional array is given below:
Syntax: data_type array_name[array_size];
Example:
int arr [10];
Initializing One Dimensional Array in C++
The general form for initializing one-dimensional array is given below:
Syntax: data_type array_name[array_size] = {comma_separated_element_list};
Example:
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Example for One-Dimensional Array in C++
#include<iostream.h> #include<conio.h> void main() { clrscr(); int arr[10]; int i; int sum=0, avg=0; cout<<"Enter 10 array elements: "; for(i=0; i<10; i++) { cin>>arr[i]; sum = sum + arr[i]; } cout<<"\nThe array elements are: \n"; for(i=0; i<10; i++) { cout<<arr[i]<<" "; } cout<<"\n\nSum of all elements is: "<<sum; avg = sum/10; cout<<"\nAnd average is: "<<avg; getch(); }
Output:
Enter 10 array elements:
1
2
3
4
5
6
7
8
9
10
The array elements are :
1 2 3 4 5 6 7 8 9 10
Sum of all elememts is: 55
And average is : 5
Example 2
#include<iostream.h> #include<conio.h> void main() { clrscr(); int arr[5] = {1, 2, 3, 4, 5}; int i; for(i=0; i<5; i++) { cout<<"arr["<<i<<"] = "<<arr[i]<<"\n"; } getch(); }
Output:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Two Dimensional Arrays in C++
The two-dimensional arrays in C++ are used to represent the elements of an array in the form of rows and columns. And in the two-dimensional array, we require to refer 2 subscript operators which indicate the row and column. While the main memory of arrays is rows and sub-memory is columns.
Syntax: Datatype ArrayName[SIZE][SIZE];
Initialization of Two Dimensional Arrays
There are two different ways in which two-dimensional arrays can be initialized.
- first method
Example:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
- Second method
Example:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
Example for two – dimensional arrays in C++
#include<iostream> using namespace std; int main() { //an array with 3 rows and 2 columns. int x[3][2] = {{0,1}, {2,3}, {4,5}}; //output each array element's value for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << "Element at x[" << i << "][" << j << "]: "; cout << x[i][j]<<endl; } } return 0; }
Output:
Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Three dimensional Array (or) Multi-dimensional Array in C++
The three-dimensional arrays are same as of two-dimensional arrays. The main difference is we have to use three loops instead of two loops for additional one dimension in three-dimensional arrays.
Example:
#include<iostream> using namespace std; int main() { //initializing the 3-dimensional array int x[2][3][2] = { { {0,1}, {2,3}, {4,5} }, { {6,7}, {8,9}, {10,11} } }; //output each element's value for(int i = 0; i < 2; ++i) { for(int j = 0; j < 3; ++j) { for(int k = 0; k < 2; ++k) { cout<<"Element at x[" << i << "][" << j << "][" << k << "] = " << x[i][j][k]<<endl; } } } return 0; }
Output:
Element at x[0][0][0] = 0
Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
Element at x[0][2][1] = 5
Element at x[1][0][0] = 6
Element at x[1][0][1] = 7
Element at x[1][1][0] = 8
Element at x[1][1][1] = 9
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11