Introducing two dimensional array in C (2D array)

Before starting two dimensional array in C we need know about matrix. You might have know about matrix. Let’s see the matrix and try to understand it.

two-dimensional-array-in-c-2D-array-in-c-300x130

Here the matrix has 3 rows and 3 columns. In computer programming we use two dimensional array to perform matrix operation.

Two dimensional array can be said as an array of arrays. Sometimes they are also called matrix.

Basic syntax of two dimensional (2D) array in C

The syntax of 2D array or declaration of two dimensional array looks like this.

// syntax of 2D array or declaration of 2D array in C

arrayType arrayName[sixeOfRow][sixeOfColumn];   // main syntax of 2D array

int myArray[4][3];    // declare integer type array

float studentHeight[2][3];   // float type array
.......................................

Initialize two dimensional (2D) array in C language

We can initialize the value of a two dimensional array by several methods. Let’s see some generally used method of initialization of 2D array in C.

// initialization of 2D array in C

int arrayName[5][3]={{0,1,2},{1,2,3},{2,3,4},{3,4,5},{4,5,6}};   // initialization method 1

int myArray[3][4] = {   // initialization method 2
    {11, 12, 13, 14},
    {21, 22, 23, 24},
    {31, 32, 33, 34},
    };

int myArr[2][5] = {11, 12, 13, 14, 15, 21, 22, 23, 24, 25};   // initialization method 3

But in case of 2D array we must declare at least the number of rows in the array. We can ignore the number of column if want. But the declaration of the size of row is must here in case of two dimensional array in C.

// error in declaration of 2D array in C

int serNo[3][2] = {1, 2, 3 ,4, 5, 6}     // Valid

int serNo[][2] = {1, 2, 3 ,4, 5, 6}     // Valid

int serNo[][] = {1, 2, 3 ,4, 5, 6}     // Invalid, we must specify second dimension

int serNo[3][] = {1, 2, 3 ,4, 5, 6}     // Invalid, we must specify second dimension

Assigning value in two dimensional array

Now we will see how to assign value in two dimensional array and reassign again.

// assigning value in two dimensional array
int main(){

int serNo[2][3];

serNo[1][2] = 5;      // Assigning value to array
serNo[2][0] = 9;

printf("Assigning value of index [1][2] is = %d\n", serNo[1][2]);
printf("Assigning value of index [2][0] is = %d\n\n", serNo[2][0]);

serNo[1][2] = 12;      // Re-assigning value to array
serNo[2][0] = 18;

printf("Re-assigning value of index [1][2] is = %d\n", serNo[1][2]);
printf("Re-assigning value of index [2][0] is = %d\n", serNo[2][0]);

return 0;
}

Output

two dimensional array in c

Storing user input inside a two dimensional array

We can use nested loop for taking user input and storing inside a two dimensional array. We can also print them using nested loop. Here we will use for loop for this purpose although you can use any other loop too. A 2D array can store total (row * column) number of inputs. If a 2D array have 2 rows and 3 columns then it can store 2 * 3 = 6 data inside it.

Total input = row * column

For example,

int myArray[3][2] can store 3 * 2 = 6 data inside it.

C program to store user input and print matrix from 2D array

// C program to store user input and print them from 2D array
#include <stdio.h>
int main(){
  int j, k, myArray[3][3];

  for (j = 0; j < 3; j++){

    for (k = 0; k < 3; k++){
      printf("Enter element of index [%d][%d]: ",j,k);
      scanf("%d",&myArray[j][k]);
    }
  }

  printf("\n You have entered the matrix : \n");

  for(j = 0; j < 3; j++){
    printf("\n");

    for (k = 0; k < 3; k++){
            printf("%d\t",myArray[j][k]);
        }
    }
  return 0;
}

Output of print matrix program from 2D array:

2d array in c, print matrix program from 2d array in c