Two Dimensional Arrays in C Programming [Complete Guide]

Two Dimensional Arrays in C Programming

We are already familiar with one-dimensional arrays or simple arrays. In one-dimensional arrays data stores in linear form or we consider it that data is stored in linear form. In one-dimensional arrays, there is only one subscript to access values in an array.

But we can also define two-dimensional arrays having two subscripts so also known as the double-subscripted arrays.

We can declare 2-D arrays just as we declare any simple array.

In one-dimensional arrays when we declare an array like
int arr[3];
This form 3 blocks in memory to store integer numbers.

But when we declare a 2-D array just as
int arr[3][3];
This will form 9 blocks in memory to store integer numbers.

You can relate this with a matrix in calculus with three rows and three columns or 3-by-3 matrix.

The first subscript shows the total number of rows and second subscript shows the total number of columns.

You can understand by understating this image
memory representation of two dimentional array
As you know index in arrays start from 0. So the row numbers are 0,1,2 and column numbers are also 0,1,2.

We can access any block by writing the name of the 2-D array and then giving row subscript first and then column subscript.

If I want to access the block with row number 1 and column number also 2. I will write arr[1][2].

If we want to initialize it we can initialize it as
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;

After this initialization, the array will have values in it just as shown
memory representation of two dimentional array with values
We can also initialize two-dimensional arrays like
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
This will store integers in memory just as
memory representation of two dimentional array having 2 rows and two columns
If we miss any number from the list of data items just as we missed 2 from the data the array will automatically save 0 in their location.
For example, if we write
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
this will save values just as
memory representation of two dimentional array having two arrays and two columns
You can see at b[0][1] there is a 0 which is automatically assigned.

Program to get values from the user in the 2-D array:
#include<stdio.h>
int main(){

    int arr[2][3],i,j;

    printf("Enter values in Two Dimensional array:\n\n");
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            printf("arr[%d][%d]: ",i,j);
            scanf("%d",&arr[i][j]);
        }
    }

    printf("\n\nValues in Two Dimensional arrays are:\n\n");
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter values in Two Dimensional array:

arr[0][0]: 5
arr[0][1]: 3
arr[0][2]: 2
arr[1][0]: 7
arr[1][1]: 4
arr[1][2]: 3

Values in Two Dimensional arrays are:

5 3 2

7 4 3

In the program, I have declared a two-dimensional array with 2 rows and 3 columns. As 2*3=6 So I can save 6 values in that array.
I have used two loops the first loop is for rows and second is for columns both starts from zero and ends at size-1.
When the first loop execute the value of i (row) will be 0 and in the second loop the value of i (row) will remain the same and by increasing the value of j (column) we can access each block with zero row and save values in it. Same will happen when i equal to 1.

Comments

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List