Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Syntax of Two Dimensional(2D) Array in C
3.
Initialization of Two Dimensional(2D) Array in C
4.
Accessing Elements of a Two-dimensional(2D) Array
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Two Dimensional Array

Author Shivam Verma
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction 

A two-dimensional array is also referred to as a multi-dimensional array. Two-dimensional arrays are the simplest form of multi-dimensional arrays. The two-dimensional array stores a list of arrays with similar data types. The two-dimensional array is organized as matrices which can be represented as a collection of rows and columns. All the elements of a two-dimensional(2D) array can be accessed randomly as well as with the help of their index. A two-dimensional array is also known as a matrix.

Also Read About, Sum of Digits in CC Static Function

Syntax of Two Dimensional(2D) Array in C

The syntax of a two-dimensional array is similar to that of a one-dimensional array, but here is the only change it has two subscripts. The syntax of the two-dimensional array is given below: 

data_type array_name[row_size][column_size];

In the above syntax, row_size specifies the number of rows, and column_size specifies the number of columns in the array. The total number of elements in the above-declared array is row_size*column_size. Consider we have an array two_dimen declared as-

int two_dimen[5][8];

Here, two_dimen is the array's name, 5 is the number of rows, and 8 is the number of columns.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Initialization of Two Dimensional(2D) Array in C

In the one-dimensional (1D) array, we don't need to specify the size of the array if the array is declared and initialized simultaneously. However, this will not work with two-dimensional (2D) arrays. We will have to define at least one (second) dimension of the array. There are two methods to initialize a two-dimensional array during declaration. Both methods only differ syntactically. 

First method

int arr[3][3]={{10, 11, 12}, {15, 14, 12}, {25, 64, 22}};

Second method 

int arr[3][3]={10, 11, 12, 15, 14, 12, 25, 64, 22};

In both methods, the array has 3 rows and 3 columns. In the first method, each set of inner braces represents one row. In the second method, the elements will be filled in the array in order, the first 3 elements from the left in the first row, the next 3 elements in the second row, and so on. Generally, the first initialization method is preferred as we can clearly understand and visualize the rows and columns of two dimensional(2D) array.

Accessing Elements of a Two-dimensional(2D) Array

Elements in Two-Dimensional(2D) arrays are accessed using the subscripts, i.e., row index and column index of the array.

For example:

int data=arr[3][2];

The above statement will take the 3rd element from the 4th row of the array. 

Here is an example of a two-dimensional array:

#include<stdio.h>
int main()
{


   /* an array with 3 rows and 3 columns*/
   int arr[3][3]={{10, 11, 12}, {15, 14, 12}, {25, 64, 22}};
   int i, j;
   /* output each array element's value */
   for(i=0;i<3;i++)
   {
       for(j=0;j<3;j++)
       {
           printf("Element at arr[%d][%d]: %d",i,j,arr[i][j]);
           printf("\n");
       }
   }
   return 0;
}

Output

Element at arr[0][0]: 10
Element at arr[0][1]: 11
Element at arr[0][2]: 12
Element at arr[1][0]: 15
Element at arr[1][1]: 14
Element at arr[1][2]: 12
Element at arr[2][0]: 25
Element at arr[2][1]: 64
Element at arr[2][2]: 22

In the above example, we used a nested 'for' loop to print or traverse all the two-dimensional(2D) array elements. We used the First 'for' loop to traverse the rows and another to traverse columns.

You can also read about dynamic array in c, Tribonacci Series and  Short int in C Programming

FAQs

  1. How many loops do we use to traverse a two-dimensional array?
    Ans: We use two loops to traverse a two-dimensional array.
     
  2. What is the syntax of a two-dimensional(2D) array?
    Ans: The syntax of a two-dimensional(2D) array is
    type array_name[row_size][column_size];
    Here type refers to the datatype. row_size refers to the number of rows, and column_size refers to the number of array columns.
     
  3. What is the use of a two-dimensional(2D) array in C?
    Ans: The use of a two-dimensional(2D) array in C are:
    1. The two-dimensional array is used to store a list of arrays with similar data types.
    2. Two-dimensional arrays are used to represent matrices.

Key Takeaways

In this blog, we learned about the syntax and initialization of two-dimensional arrays. We also discussed how we could access the element of the two-dimensional array.

This blog is over, but the thirst for learning is not over yet. To understand the difference between a one-dimensional array and a two-dimensional array, refer to thisYou can visit and read our library of curated blogs by clicking here

Check out this problem - Search In Rotated Sorted Array

Till then, have a nice day and Happy Coding! 

Previous article
One Dimensional Array in C
Next article
Types of Array in C
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass