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

Last Updated on December 28, 2023 by Ankit Kochar

Two-dimensional arrays are fundamental data structures in programming languages like C, allowing the storage and manipulation of data in a grid-like format. They are an arrangement of elements in rows and columns, forming a matrix structure that enables efficient organization and access to data. In C, two-dimensional arrays are extensively used to work with matrices, images, tables, and more, providing a versatile way to handle data in a structured manner. This article delves into the fundamentals of two-dimensional arrays in C, explaining their syntax, usage, and common operations

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
Two-dimensional arrays in C serve as powerful tools for handling structured data efficiently. Understanding their syntax, declaration, initialization, and manipulation is crucial for developing applications that involve matrices, tables, and grid-based datasets. Mastery of these arrays empowers programmers to efficiently work with multidimensional data structures, facilitating various computational tasks and data processing operations in C programming.

FAQs (Frequently Asked Questions) about Two-Dimensional Arrays in C:

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

1. How do you declare a two-dimensional array in C?
To declare a two-dimensional array in C, you specify the data type of the elements and the size of rows and columns using the syntax: data_type array_name[rows][columns];.

2. How are elements accessed in a two-dimensional array?
Elements in a two-dimensional array are accessed using indices for rows and columns, like array_name[row_index][column_index];.

3. Can the size of rows and columns in a two-dimensional array be dynamic in C?
In C, the size of arrays is determined at compile time. However, you can simulate dynamic behavior using pointers and dynamic memory allocation.

4. How can you initialize a two-dimensional array in C?
You can initialize a two-dimensional array during declaration by providing the initial values in a nested brace-enclosed list for rows and columns, such as int array[2][3] = {{1, 2, 3}, {4, 5, 6}};.

5. What are the common operations performed on two-dimensional arrays in C?
Common operations include traversing the array elements, performing matrix operations (addition, multiplication, etc.), finding specific elements, and manipulating data within the array.

6. Can a two-dimensional array store elements of different data types in C?
No, in C, all elements within an array must be of the same data type.

7. How is memory allocated for a two-dimensional array in C?
Memory for a two-dimensional array in C is allocated in a contiguous block, calculated as the product of rows and columns multiplied by the size of the data type.

Leave a Reply

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