2D Arrays in C Programming | User-Defined Data Types in C | Data Structures


Welcome Back Programmers, After learning about 1 Dimensional Array , it is very much important to know that C language can also support multi dimensional arrays , which can be either 2 Dimensional or 3 Dimensional or even N-Dimensional .

Knowing about them is also very important to have a good edge over programming. You all have studied Matrices in 11th Class Mathematics and even later on also. We are going to tell you about a Array type same as Matrices but on computer level. This special type is 2-Dimensional Array.

2-Dimensional Array as the name suggests here we have to deal with 2 dimensions at a time, i.e, with rows and columns, we can store data in form of a table in rows and columns , but a table whose borders are invisible to human eyes, i.e only visible to the compiler and the computer . It is also called Array of Arrays. These are used to create a relational Database look-like Data Structure .

Declaring 2-Dimensional Array :

GENERAL FORM :  data_type  array_name[row_size][column_size];

Example :  int arr[10][10];
        char arr[100][20];
        float arr2[12][21];

* Here the value in 1st brackets signify the number of ROWS and the value in 2nd bracket signifies the number of COLUMNS. This same convention is followed every where ,i.e firstly the number of rows will come and secondly the number of columns. YOU CAN USE THIS AS A MEMORY TIP
This 2-D array int arr[10][10] is stored in the form of matrix having 10 rows and 10 columns.
* The total number of elements that a given 2-D array can store = Number of rows multiply by Number of columns.

Initializing a  2-Dimensional Array :


1. Compile-Time Initialization : 

int arr[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

* Here rows=2 and cols=4 , therefore the 2 inner curly braces signify the 2 different rows and the elements in each row signifies each element in the column.

int arr[2][4] = {1, 2, 3, 4, 5, 6, 7, 8};

* We can also initialize array like this without giving any inner brackets for the rows, the compiler automatically puts the required elements ( number of elements = number of columns ,i.e 4 ) in each row and then automatically comes to the next row , and then fills that again with necessary number of elements.

int age[][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; 

* We can also omit the first dimension size ,i.e the number of rows but we can't omit the second dimension size . It automatically sets the number of rows by adjusting the number of elements in each columns. Like here col_size = 4 , i.e whenever 1st four elements would be set in the array, the compiler will automatically create a new row as the number of columns in a row has been exceeded.

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

* Such initializations will cause compile time errors in the program. So avoid such mistakes

2. Run-Time Initialization : 

Like 1D arrays, 2D arrays can also be initialized at runtime using loops for iterating through each element .

#include <stdio.h>

int main()
{
 int a[2][4], i, j;
 printf("Enter the element of a array of size 2 by 4 : ");
 for(i=0; i < 2 ; i++)
 {
    for(j=0; j < 4; j++) 
   {
    scanf("%d", &a[i][j]);
   }
 }
 for(i=0; i < 2; i++) 
 {
    for(j=0; j < 4; j++) 
   {
       printf("%d\t", a[i][j]);
   }
    printf("\n");
 }
 return 0;
}

* Here loop having variable ' i ' iterates over the rows, for i = 0 , we would on the 1st row and then comes the loop having variable ' j ', which iterates over the column elements. For each value of i , the j-loop will iterate over all the elements in that row.

Example : In case of array of 2 by 4 , such indexing will occur , you can see 1st row is having value 0 and then according to the above example the j-loop iterates through every position from 0-3 (<4) . So we get a[0][0] , a[0][1] ,  a[0][2] and a[0][3]. And similarly for 2nd row.

  a[0][0]  a[0][1] a[0][2] a[0][3]
 a[1][0] a[1][1] a[1][2] a[1][3]

 WORKING OF THE ABOVE SAMPLE CODE : 

Here firstly a[0][0] index will be filled then a[0][1], then a[0][2] and then a[0][3] . Then condition for j in the 2nd loop will be falsified and then i will change to i=i+1, that is , ' i ' will become 1, and again the same order will be followed till the last index will be encountered , a[ROW_SIZE - 1][COL_SIZE - 1] , in this case last element will be a[1][3]. 

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post