Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Two Dimensional Array in C

C programming language is one of the most used programming languages around the world due to the many features it provides. C language provides a data structure array that is used to store the data. Arrays are widely used in any programming language and there are many types of arrays. We are going to discuss one such type of array known as two dimensional array in c, we will discuss all 2d array in c, with their syntax, and initialization, how to declare them, and some features that we can perform on them.

What is a 2D Array?

A two dimensional array in c is one type of array. The 2d array is an array that is organized in rows and columns. We can identify each element in a 2d array using the position of rows and columns, we also call them indices. For example, in a 3×3 2D array, the element in the second row and third column would be accessed using the indices (1, 2) or row 1, column 2.

Syntax of 2D Array

In this blog section, we will explain the syntax of the two dimensional array in c.

data_type array_name[m][n];

Where,

  • data_type: It will tell the type of data that has to be stored in each element.
  • array_name: It will tell the name of the array with which it will be referenced in the whole program.
  • m: It is the number of rows.
  • n: It is the number of columns.

The array will have a total of m x n elements.
For example, the 2D array in c of 20 rows and 10 columns will be declared like this.

int x[20][10];

Declaration and Initialization of Two Dimensional Array in C

We can declare the 2D array in c by using two methods.

  • Using Initializer List
  • Using Loops

Initializing using Initializer List
In the initializer list we have two methods to declare the two dimensional array in C.

  • First Method
    In the first method we can write all the elements together as shown in the below example.

    int a[4][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    Here the array contains 4 rows and 3 columns. We read the elements in the matrix from left to right so in this also the elements are read from left to right so after every 3 elements we go to the new row, and the new row starts to be filled.

  • Second Method
    This method is comparatively better as we have clearly specified the elements in each row. Let’s look at the example shown below.

    int a[4][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};

    In the above example we have nested brackets which shows the element of each row individually.

Initializing Two Dimensional Array in C using Loops
We can declare the array as normal after that we can initialize them with the help of loops.

Example of two-dimensional array in C
Let’s understand this better with the help of an example.

int a[m][n];

for(int i = 0; i < m; i++){
    for(int j = 0; j < n; j++){
        x[i][j] = i * j + 1;
    }
}

In the above example we have declared the array with of size m x n and we have initialized it by using loops.

Accessing the Elements of the 2D Array in C

You can access the elements of Two dimensional array in C using the rows and columns or indices shown below.

array_name[x][y]

Where

  • x: is the row index.
  • y: is the column index

Pointers and 2D Array in C

We can use the pointers for accessing the elements of the array. The single pointer points to an entire 1D array so to use the pointer in 2D array we have to create a new pointer for every row.

Example
Let’s understand the relations between pointers and 2D array with the help of an example.

Code Implementation

#include <stdio.h>

int main(void) {
    int arr[2][3] = {{31, 12, 11}, {81, 91, 110}};
    int *p[2];
    p[0] = arr[0]; 
    p[1] = arr[1]; 
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", p[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Output

31 12 11 
81 91 110 

Explanation of the above example
Here we have used pointers to traverse in the row and for every new row we have created a new pointer.

Examples of Two Dimensional Array in C

In this section, we will discuss various examples of two dimensional arrays in c.

Example 1: Taking the Input from the user and printing it.
In the above example, we will see the implementation of the above-mentioned example.

#include <stdio.h>
void main ()
{
    int arr[3][3],i,j;
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            printf("Enter a[%d][%d]: ",i,j);
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\n printing the elements ....\n");
    for(i=0;i<3;i++)
    {
        printf("\n");
        for (j=0;j<3;j++)
        {
            printf("%d\t",arr[i][j]);
        }
    }
}

Input

Enter a[0][0]: 8
Enter a[0][1]: 7
Enter a[0][2]: 6
Enter a[1][0]: 5
Enter a[1][1]: 4
Enter a[1][2]: 3
Enter a[2][0]: 2
Enter a[2][1]: 1
Enter a[2][2]: 0

Output

printing the elements ....

8       7       6
5       4       3
2       1       0

Explanation of the above example
In the above example we have declared an array of size 3 x 3. Then we have asked the user to give the input after receiving each input we have shown the matrix in the output.

Example 2: Summation in Two Dimensional Array in C
Now we will discuss the implementation of the above-mentioned example.

#include <stdio.h>
int main()
{
  float a[3][3], b[3][3], result[3][3];

  printf("Enter elements of 1st matrix\n");
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    {
      printf("Enter a%d%d: ", i + 1, j + 1);
      scanf("%f", &a[i][j]);
    }

  printf("Enter elements of 2nd matrix\n");
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    {
      printf("Enter b%d%d: ", i + 1, j + 1);
      scanf("%f", &b[i][j]);
    }

  // adding corresponding elements of two arrays
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    {
      result[i][j] = a[i][j] + b[i][j];
    }

  // Displaying the sum
  printf("\nSum Of Matrix:\n");

  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    {
      printf("%.1f\t", result[i][j]);

      if (j == 2)
        printf("\n");
    }
  return 0;
}

Input

Enter elements of 1st matrix
Enter a11: 1
Enter a12: 2
Enter a13: 3
Enter a21: 4
Enter a22: 5
Enter a23: 6
Enter a31: 7
Enter a32: 8
Enter a33: 9
Enter elements of 2nd matrix
Enter b11: 9
Enter b12: 8
Enter b13: 7
Enter b21: 6
Enter b22: 5
Enter b23: 4
Enter b31: 3
Enter b32: 2
Enter b33: 1

Output

Sum Of Matrix:
10.0    10.0    10.0
10.0    10.0    10.0
10.0    10.0    10.0

Explanation of the above example
In the above example we have taken two matrixes as input and then have added the two matrixes and printed the resulting matrix as output.

Common Applications of 2D Arrays

2D arrays in c have many applications that make them so useful. Some of the applications are explained below.

  • Storing and manipulating images or other graphical data.
  • Manipulating and storing matrices in linear algebra computations.
  • Implementing board games like checkers and chess.
  • Manipulating and representing data in spreadsheet programs.

Conclusion
In this article we have studied two dimensional array in c. We have understood how to declare them using the syntax, and along with this, we have studied various methods for their initialization. We have also discussed the relationship between pointers and two dimensional arrays. After that, we have ended with the applications of two dimensional array in c.

Frequently Asked Questions

Below are some of the frequently asked questions on two dimensional arrays.

1. Can a 2D array have different types of elements?
No, a 2D array in C can only have elements of the same data type.

2. How do you find the size of a 2D array in C?
To find the size of a 2D array in C, you can multiply the number of rows by the number of columns.

3. How do you free memory allocated to a 2D array in C?
To free the memory allocated to a 2D array in C, you use the free() function.

4. How do you pass a 2D array to a function in C?
To pass a 2D array to a function in C, you must specify the dimensions of the array in the function parameter.

5. How do you return a 2D array from a function in C?
You cannot directly return a 2D array from a function in C. Instead, you must return a pointer to the array.

Leave a Reply

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