Sometimes data needs to be stored in more than one dimension. The data could have more sub categorical data, the data might be interconnected with other complex data for a relational purpose. For these reasons, Multi-dimensional Arrays are used in programming languages. Multi-dimensional arrays contain multiple number of rows and columns which makes the complex data storing process very efficient. In this article, we will look at the Two Dimensional Arrays, which is the most commonly used Multi-Dimensional Array.
Definition
- Two-Dimensional Arrays are simply an array of arrays where the data is stored in tabular format.
- Two-Dimensional Arrays are the simplest form of Multi-Dimensional Array where the data values are stored in multiple rows and columns.
Why do we need a 2D Array?
Often, data comes naturally in the form of a table or in rows and column’s structure. Example, spreadsheet data, the periodic table, multiplication table, Movie ratings, Employee and their salaries, etc. Such data needs to be stored in more than one dimension. Hence, we use Two-Dimensional Arrays in order to store the data efficiently. A Two Dimensional Array also makes it very easy to access every element in the array.
Two-Dimensional Array
A two-Dimensional array is a matrix structure. It consists of a number of Rows and Columns. Each data value is stored in matrix format according to the sequence of the data values specified. A Two-Dimensional Array can be visualized by looking at the below image:
The syntax for declaring a Two-Dimensional Array is:
Syntax
data_type array_name [x][y] ;
where,
data_type = Type of data to be stored in the array
x = number of rows
y = number of columns
Note – Elements of a 2D array are generally represented in the format arr[i][j] where ‘i’ is the number of rows and ‘j’ is the number of columns of the array.
Browse more Topics under Structured Data Type
Initializing a 2D Array
In order to understand how to initialize a Two Dimensional array with data values, we follow the following syntax,
Example
# Two-dimensional Array initialization
int arr[3][2] = {100, 10, 200, 20, 300, 30} ;
OR
int arr[3][2] = {{100, 10}, {200, 20}, {300, 30}} ; Â //The values will be filled row wise
The first method is a simple value initialization, where the values are stored in the array from left to right. i.e. first 2 elements will be stored in Row 1, the next two in row 2, and so on.
The second method uses the nested braces method. Here every set of braces represents a row. Each nested braces value will be stored in separate rows. In the above example, there are 3 inner braces, so there will be 3 rows in the Two-dimensional Array.
Accessing Elements of 2D Array
Just like the Indexing method used for accessing elements of a One-Dimensional Array, Two-Dimensional Arrays also use the Indexing method. The only difference is that the row and column indexes are used to access array elements.
Example 1
# To print a single array element from a 2D Array
#include
using namespace std;
int main()
{
int arr[4][2] = { { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 } };
int val = arr[2][1] ;
cout << val ;
}
Output –
31Â Â // Every 2D Array starts from 0x0 indexing
Example 2
# To print all the elements of a 2D Array
#include
using namespace std;
int main()
{
int arr[4][2] = { { 10, 11 }, { 20, 21 }, { 30, 31 }, { 40, 41 } };
cout << "Elements of 2D Array are : \n" ;
for (int x = 0; x < 4 ; x++)Â Â Â // Outer for loop for traversing rows
{
for(int y = 0; y < 2 ; y++)Â // Inner for loop for traversing columns
{
cout << "\t" << arr[x][y] ;Â Â // Print individual array element
}
cout << endl;
}
}
Output
Elements of 2D Array are :
10Â Â Â Â Â Â 11
20Â Â Â Â Â Â 21
30Â Â Â Â Â Â 31
40Â Â Â Â Â Â 41
Matrix Multiplication of 2D Array
Many arithmetic operations can be performed on Two Dimensional Arrays. One such common arithmetic operation is Matrix Multiplication. Let us look at the below code in order to understand how Matrix Multiplication can be done.
Example
#include
using namespace std;
int main()
{
int m1[4][4], m2[4][4], m3[4][4];
int i, j, row, col;
cout<<"Enter the no.of rows of the matrices to be added(max 4):";
cin>>row;
cout<<"Enter the no.of columns of the matrices to be added(max 4):";
cin>>col;
cout<<"\n Input 1st Matrix:\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<"Matrix1["<<i<<"]["<<j<<"]=Â ";
cin>>m1[i][j];
}
}
cout<<"\nInput 2nd Matrix:\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<"Matrix2["<<i<<"]["<<j<<"]=Â ";
cin>>m2[i][j];
}
}
cout<<"\nMultiplying Matrices...\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
m3[i][j]=m1[i][j]*m2[i][j];
}
}
cout<<"\nThe Resultant Matrix is:\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<"\t"<<m3[i][j];
}
cout<<endl;
}
}
Output
Enter the no. of rows of the matrices to be added(max 4):2
Enter the no. of columns of the matrices to be added(max 4):2
Input 1st Matrix:
Matrix1[0][0]=Â Â 1
Matrix1[0][1]=Â Â 2
Matrix1[1][0]=Â Â 3
Matrix1[1][1]=Â Â 4
Input 2nd Matrix:
Matrix2[0][0]=Â Â 1
Matrix2[0][1]=Â Â 2
Matrix2[1][0]=Â Â 3
Matrix2[1][1]=Â Â 4
Multiplying Matrices....
The Resultant Matrix is:
1Â Â Â Â Â Â Â 4
9Â Â Â Â Â Â Â 16
Two-Dimensional Character Array
Two-dimensional arrays are not only used to store numeric forms of data but can also store character data type values. In order to store characters in a two-dimensional array, we need to encase them in the Apostrophe mark i.e. ‘ ‘. Following is an example of how a 2D Array is used to store character data type –
#include
using namespace std;
int main()
{
char val [3][3]= { {'c', 'a', 't'}, {'m', 'a', 't'}, {'f', 'a', 't'} };
cout << val[2][0] ;
}
Output
f
FAQs on Two Dimensional Array
Q1. What will be the output of the following program?
double val[5][5] = {{2.0, 1.2, 4.3}, {2.6, 4.8, 3.6, 1.1}, {6.7, 9.9, 6.1}} ;
cout << val[2][0] ;
- 2.6
- 4.8
- 6.7
- Syntax Error
Answer. Option C
Q2. Which of the following line of code will create a 2D Array of row 6 and column 4?
- int arr[4][6];
- int arr [6][4];
- arr [6][4];
- arr {6}{4};
Answer. Option B
Q3. Which of the following statement will replace the value of 10 with 20?
int arr[3][3] = {{1, 34, 99}, {23, 10, 24}, {23, 45, 76}} ;
- arr [1][0] = 20;
- arr [0][1] = 20;
- arr [10] = 20;
- arr [1][1] = 20;
Answer. Option D
Q4. What will be the index variable be for the element in the first row and first column of an array?
- A[0][0]
- A[0][1]
- A[1][1]
- A[1][0]
Answer. Option A
Q5. Given an array where arr[x][y], x is the number of columns and y is the number of rows?
- True
- False
Answer. Option B
Leave a Reply