Two-Dimensional Array in Data Structure| 2-D Array Definition

Two-Dimensional-Array: Array is a collection of data types . It is used to store a group of data simultaneously. It can be store data of same data types means an integer array can be store only integer value, character array can store only character value.


A two-dimensional array is a data structure that can hold a collection of values arranged in rows and columns.

We can not fetch data from array directly therefore we use index point.

Array indexing of array always start with 0. Index value is always an integer number. Array may be of any data type like int, char and float.

Two-Dimensional Array| 2-D Array Short With Examples
Two-Dimensional Array| 2-D Array Short With Examples

    Two-Dimensional Array in C Syntax

    Data_Type Array_Name[row][column]= {Array Elements};

     

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


    Here, data_type is the type of data that the array will hold (such as int, float, or char), array_name is the name given to the array, row_size is the number of rows, and column_size is the number of columns.

    Two Dimensional Arrays in C Example Program with output

    #include<stdio.h>

    #include<conio.h>

    void main()

    {

    int a[3][3]={{12,14,15},{44,56,33},{65,88,77}};

    printf("Value at is[0][0]:%d\n",a[0][0]);

    printf("Value at is[0][1]:%d\n",a[0][1]);

    printf("Value at is[0][2]:%d\n",a[0][2]);

    printf("Value at is[1][0]:%d\n",a[1][0]);

    printf("Value at is[1][1]:%d\n",a[1][1]);

    printf("Value at is[1][2]:%d\n",a[1][2]);

    printf("Value at is[2][0]:%d\n",a[2][0]);

    printf("Value at is[2][1]:%d\n",a[2][1]);

    printf("Value at is[2][2]:%d\n",a[2][2]);

    getch();

    }


    **********OUTPUT**********

    Value at is[0][0]:12

    Value at is[0][1]:14

    Value at is[0][2]:15

    Value at is[1][0]:44

    Value at is[1][1]:56

    Value at is[1][2]:33

    Value at is[2][0]:65

    Value at is[2][1]:88

    Value at is[2][2]:77


    Two Dimensional Array Printing Element Using Loop

    #include<stdio.h>

    #include<conio.h>

    void main()

    {

    int a[3][3]={{12,14,15},{44,56,33},{65,88,77}};

    int i,j;

            for(i=0;i<3;i++)

             {

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

                   {

                     printf("%d ",a[i][j]);

                    }

                     printf("\n");

            } 

    getch();

    }


    **********OUTPUT**********

    12 14 15

    44 56 33

    65 88 77


    Two Dimensional Array Printing Element User Input Using Loop

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

    int a[3][3];

    for(int i=0;i<3;i++)

    {

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

    {

    printf("Enter Array Element[%d][%d]:",i,j);

    scanf("%d",&a[i][j]);

    }

    }

    printf("Printing Array Element\n");

    for(int i=0;i<3;i++)

    {

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

    {

    printf("%d ",a[i][j]);

    }

    printf("\n");

    }

    getch();

    }


    **********OUTPUT**********

    Printing Array Element

    12 14 15

    44 56 33

    65 88 77


    Two Dimensional Array Elements Short

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

    int a[3][3],r,c,t;

    printf("Enter Row:");

    scanf("%d",&r);

    printf("Enter Column:");

    scanf("%d",&c);

    printf("-----------------------------------------\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf("Enter Array Element Row and Column[%d][%d]:",i,j);

    scanf("%d",&a[i][j]);

    }

    }

    printf("-----------------------------------------\n");

    printf("2-D Array Matrix\n");

    printf("-----------------------------------------\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf(" %d ",a[i][j]);

    }

    printf("\n");

    }

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    for(int i1=0;i1<r;i1++)

    {

    for(int j1=0;j1<c;j1++)

    {

    if(a[i][j]<a[i1][j1])

    {

    t=a[i][j];

    a[i][j]=a[i1][j1];

    a[i1][j1]=t;

    }

    }

    }

    }

    }

    printf("-----------------------------------------\n");

    printf(" Your 2-D Array Shorted\n");

    printf("-----------------------------------------\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf(" %d ",a[i][j]);

    }

    printf("\n");

    }

    printf("-----------------------------------------\n");

    getch();

    }



    **********OUTPUT**********

    Enter Row:3

    Enter Column:3

    -----------------------------------------

    Enter Array Element Row and Column[0][0]:12

    Enter Array Element Row and Column[0][1]:32

    Enter Array Element Row and Column[0][2]:55

    Enter Array Element Row and Column[1][0]:11

    Enter Array Element Row and Column[1][1]:13

    Enter Array Element Row and Column[1][2]:18

    Enter Array Element Row and Column[2][0]:15

    Enter Array Element Row and Column[2][1]:14

    Enter Array Element Row and Column[2][2]:10

    -----------------------------------------

    2-D Array Matrix

    -----------------------------------------

     12 32 55

     11 13 18

     15 14 10

    -----------------------------------------

     Your 2-D Array Shorted

    -----------------------------------------

     10 11 12

     13 14 15

     18 32 55



    Two Dimensional Array Transpose Matrix

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

    int a[3][3],n;

    printf("Enter Matrix Element:");

    scanf("%d",&n);

    for(int i=0;i<n;i++)

    {

    for(int j=0;j<n;j++)

    {

    printf("Enter Array Element[%d][%d]:",i,j);

    scanf("%d",&a[i][j]);

    }

    }

    for(int i=0;i<n;i++)

    {

    for(int j=0;j<n;j++)

    {

    printf(" %d ",a[i][j]);

    }

    printf("\n");

    }

    printf("Transpose Matrix\n");

    for(int i=0;i<n;i++)

    {

    for(int j=0;j<n;j++)

    {

    printf(" %d ",a[j][i]);

    }

    printf("\n");

    }

    getch();

    }


    **********OUTPUT**********

    Enter Matrix Element:3

    Enter Array Element[0][0]:1

    Enter Array Element[0][1]:2

    Enter Array Element[0][2]:3

    Enter Array Element[1][0]:4

    Enter Array Element[1][1]:5

    Enter Array Element[1][2]:6

    Enter Array Element[2][0]:7

    Enter Array Element[2][1]:8

    Enter Array Element[2][2]:9


     1 2 3

     4 5 6

     7 8 9

    Transpose Matrix

     1 4 7

     2 5 8

     3 6 9


    Two Dimensional Array Addition of Matrix

    #include<stdio.h>

    #include<conio.h>

    void main()

    {

    int a1[3][3];

    int a2[3][3];

    int a3[3][3];

    int r,c;

    printf("Enter First Matrix\n");

    printf("Enter Row:");

    scanf("%d",r);

    printf("Enter Column:");

    scanf("%d",c);

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf("Enter Array Element row and Column[%d][%d]:",i,j);

    scanf("%d",&a1[i][j]);

    }

    }


    printf("First Matrix Printing...\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf("%d ",a1[i][j]);

    }

    printf("\n");

    }

    printf("Enter Second Matrix\n");

    printf("Enter Row:");

    scanf("%d",r);

    printf("Enter Column:");

    scanf("%d",c);

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf("Enter Array Element row and Column[%d][%d]:",i,j);

    scanf("%d",&a2[i][j]);

    }

    }

    printf("Second Matrix Printing...\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    printf("%d ",a2[i][j]);

    }

    printf("\n");

    }

    printf("Adding Matrix Array Element\n");

    for(int i=0;i<r;i++)

    {

    for(int j=0;j<c;j++)

    {

    a3[i][j]=a1[i][j]+a2[i][j];

    printf("%d ",a3[i][j]);

    }

    printf("\n");

    }

    getch();

    }


    **********OUTPUT**********

    Enter First Matrix

    Enter Row:2

    Enter Column:2

    Enter Array Element row and Column00:1

    Enter Array Element row and Column01:4

    Enter Array Element row and Column10:5

    Enter Array Element row and Column11:4


    First Matrix Printing...

     1 4

     5 4


    Enter Second Matrix

    Enter Row:2

    Enter Column:2

    Enter Array Element row and Column00:8

    Enter Array Element row and Column01:7

    Enter Array Element row and Column10:6

    Enter Array Element row and Column11:5


    Second Matrix Printing...

     8 7

     6 5


    Adding Matrix Array Element

     9 11

     11 9