Structured Data Type

Two Dimensional Array

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:

Two Dimensional Arrays

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

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] ;

  1. 2.6
  2. 4.8
  3. 6.7
  4. Syntax Error

Answer. Option C

Q2. Which of the following line of code will create a 2D Array of row 6 and column 4?

  1. int arr[4][6];
  2. int arr [6][4];
  3. arr [6][4];
  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}} ;

  1. arr [1][0] = 20;
  2. arr [0][1] = 20;
  3. arr [10] = 20;
  4. 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?

  1. A[0][0]
  2. A[0][1]
  3. A[1][1]
  4. 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?

  1. True
  2. False

Answer. Option B

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.