Two Dimensional Array in C

The Two Dimensional Array in C language is nothing but an Array of Arrays. If the data is linear, we can use the One Dimensional. However, to work with multi-level data, we have to use the Multi-Dimensional Array.

Two Dimensional Array in C is the simplest form of MultiDimensional. In Two Dimensional Array, data is stored in rows and column-wise. We can access the record using both the row index and column index (like an Excel File).

Declaration of Two Dimensional Array in C

The basic syntax or the declaration of two dimensional array in C Programming is as shown below:

Data_Type Arr_Name[Row_Size][Column_Size]
  • Data_type: This will decide the type of elements that will accept by it. For example, If we want to store integer values, then we declare the Data Type as int, If we want to store Float values, then we declare the Data Type as float, etc.
  • Arr_Name: This is the name you want to give to this two dimensional array in C. For example, students, age, marks, employees, etc
  • Row_Size: Number of Row elements it can store. For example, if Row_Size =10, then it will have 10 rows.
  • Column_Size: Number of Column elements it can store. For example, if Column_Size = 8, then it will have 8 Columns.

2D Array Declaration Example

int Employees[4][3];

  1. Here, we used int as the data type to declare. So, above C two dimensional array will accept only integers. If you try to add float values, it will through an error.
  2. Employees – the name of it.
  3. The Row size of it is 4. It means Employees will only accept four integer values as rows.
    • If we try to store more than 4 values, then it will throw an error.
    • We can store less than 4. For Example, If we store two integer values, then the remaining 2 values will assign to the default value (Which is 0).
  4. The Column size of it is 3. It means C Two Dimensional Array Employees will only accept 3 integer values as columns.
    • If we try to store more than 3, it will throw an error.
    • We can store less than 3. For Example, If we store 1 integer value, the remaining 2 values will be assigned to the default value (Which is 0).

Two Dimensional Array in C Initialization

We can initialize the Two Dimensional Array in multiple ways. Before this, Please refer to the One-Dimensional and Multi-Dimensional articles in C Programming

C Two Dimensional Array Initialization First Approach

int Employees[4][3] = { 10, 20, 30, 15, 25, 35, 22, 44, 66, 33, 55, 77 };

The first three elements will be 1st row, the second three elements will be 2nd row, the next 3 elements will be 3rd, and the last 3 elements will be 4th. Here we divided them into 3 because our column size = 3.

The above C two dimensional array statement can also be written as

int Employees[4][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Here, We surrounded each row with curly braces ({}). It is always good practice to use curly braces to separate the rows.

Second Approach

int Employees[ ][ ] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Here, We haven’t mentioned the row size and column size. However, the compiler is intelligent enough to calculate the size by checking the number of elements inside the row and column.

We can also write this two dimensional array in c as

int Employees[ ][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Third Approach to Initialize Two Dimensional Array in C

int Employees[2][3] = { {4},
                        {6, 7} 
                      };

Here, we declared Employees with row size =2 and column size = 3, but we only assigned 1 column in the 1st row and 2 columns in the 2nd row. In these situations, the remaining values will assign to default values (0 in this case).

The above one will be:

Employees[2][3] = { {4, 0, 0},
                    {6, 7, 0} 
                  };  
//It means

Employees[0][0] = 4

Employees[0][1] = 0

Employees[0][2] = 0

Employees[1][0] = 6

Employees[1][1] = 7

Employees[1][2] = 0

Fourth Approach

The above three ways of initializing two dimensional array in C are good for storing a small number of elements. What if we want to store 100 rows or 50 column values? It will be a nightmare to add all of them using any of the approaches mentioned above. To resolve this, we can use the For Loop in C Programming concept here:

int rows, columns, Employees[100][50];

for (rows =0; rows < 100 ; rows++)
 {
   for (columns =0; columns < 50; columns++)
    {
      Employees[rows][columns] = rows + columns;
 }

Say,

Employees[3][4] = 3+4 = 7

Access C Two Dimensional Array Elements

We can access the Two Dimensional Array in C Programming elements using indexes. Using the index, we can access or alter/change each element present in the array separately. The index value starts at 0 and ends at n-1, where n is the size of a row or column.

For example, if an array of Student[8][5] will store 8 row elements and 5 column elements. To access or alter 1st value, use Student[0][0], to access or alter 2nd row 3rd column value, then use Student[1][2], and to access the 8th row 5th column, then use Student[7][4]. Let’s see the example of a C Two Dimensional Array for a better understanding:

int Employees[4][3] = { {10, 20, 30},
                        {15, 25, 35},
                        {22, 44, 66},
                        {33, 55, 77}
                      };

//To Access the values in the Employees[4][3]

Printf("%d", Employees[0][0]) = 10

Printf("%d", Employees[0][1]) = 20

Printf("%d", Employees[0][2]) = 30

Printf("%d", Employees[1][0]) = 15

Printf("%d", Employees[1][1]) = 25

Printf("%d", Employees[1][2]) = 35

Printf("%d", Employees[2][0]) = 22

Printf("%d", Employees[2][1]) = 44

Printf("%d", Employees[2][2]) = 66

Printf("%d", Employees[3][0]) = 33

Printf("%d", Employees[3][1]) = 55

Printf("%d", Employees[3][2]) = 77


//To Alter the values in the Employees[4][3]

Employees[2][1] = 98; - It will change the value of Employees[2][1] from 44 to 98

For a large number of rows and columns, we can access them using For Loop. Let’s use the above Employees[4][3]

int rows, columns;

for (rows = 0; rows < 4; rows++)
 {
  for (columns =0; columns < 3; columns++)
   {
     Printf("%d", Employees[rows][columns]);
   }
 }

Two Dimensional Array in C Example

In this program, We will declare 2 and initialize them with some values. Next, we will declare one more Two dimensional to store the sum of those 2 arrays in.

#include<stdio.h>

int main()
{
  int a[2][3] = { {15, 25, 35}, {45, 55, 65} };
  int b[2][3] = { {12, 22, 32}, {55, 25, 85} };
  int rows, columns, Sum[2][3];
  
  for(rows = 0; rows < 2; rows++)
   {
    for(columns = 0; columns < 3;columns++)
     {
      Sum[rows][columns] = a[rows][columns] + b[rows][columns];  
     }
   }
  
  printf("\n Sum Of those Two Arrays are:\n");
  for(rows = 0; rows < 2; rows++)
   {
    for(columns = 0; columns < 3; columns++)
     {
      printf("%d, ", Sum[rows][columns]);
     }
   }
  
  return 0;
}
Two Dimensional Array in C

Let us see the execution of the C two dimensional array iteration wise

Analysis

Row First Iteration: The value of the row will be 0, and the condition (rows< 2) is True. So, it will enter into the second for loop.

Column First Iteration of Two Dimensional Array in C
The value of the column will be 0, and the condition (columns< 2) is True. So, it will start executing the statements inside the loop until the condition fails.
Sum[rows][columns] = a[rows][columns] + b[rows][columns];
Sum[0][0] = a[0][0] + b[0][0] = 15 + 12
= 27;

Second Iteration
The value of the column will be 1, and the condition (columns< 3) is True. Since we didn’t exit from the inner loop (Columns loop), the row value will be 0
Sum[0][1]= a[0][1] + b[0][1] = 25 + 22;
Sum[0][1]= 47;

Column 3rd Iteration
columns= 2, and the condition (2 < 3) is True.
Sum[0][2] = a[0][2] + b[0][2] = 35 + 32
Sum[0][2] = 67;

After incrementing, the value of columns= 3, and the condition (columns < 3) will fail. So, it will exit from the loop.

Now, the value of the rows will increment and starts the second iteration of two dimensional array in c.

Row Second Iteration
The value of the row= 1, and the condition (rows < 2) is True. So, it will enter into a second for loop.

Column First Iteration
The value of the column= 0, and the condition (columns< 3) is True.
Sum[1][0] = a[1][0] + b[1][0] = 45 + 55;
Sum[1][0] = 100;

Second Iteration
column= 1, and the condition (columns< 3) is True.
Sum[0][1]= a[1][1] + b[1][1] = 55 + 25;
Sum[0][1]= 80;

Column 3rd Iteration
columns= 2, and the condition (columns< 3) is True.
Sum[1][2] = a[1][2] + b[1][2] = 65 + 85;
Sum[1][2] = 150;

After incrementing, the value of columns= 3, and the condition (columns < 3) will fail. So it will exit from the loop.

Now, the value of the rows will increment, it means rows= 2. Condition (rows < 2) will fail, so it will exit from the loop.

The next for loop in C two dimensional array program will traverse as we explained above. However, instead of summing, it will display the values one by one with comma separation using the printf statement inside them.

The final output of the Sum is:
Sum[2][3] = { {27, 47, 67}, {100, 80, 150} };

Comments are closed.