C Program How to Input & display 2D Array with Example- Addition of matrix

In C programming, Multidimensional arrays are considered as array of arrays. A two-dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of 2D Arrays. In C programming an array can have two, three, or even ten or more dimensions. However the complexity also increases as the number of dimension increases. The most used multidimensional array is the Two-Dimensional Array. Two dimensional (2D) array is organized as a matrix with a number of rows and columns.

For example,
int scores[10][5];

Here, scores is a  variable of integer data type represents two-dimensional 2D array. This array can hold 50 elements, i.e it has 10 rows and 5 columns. As first index is row number 10, which represents the count of students and second index is column number 5, which represents count of subjects.

Rows and Columns in Two dimensional array
Rows and Columns in Two dimensional array

How to initialize a multidimensional array?

int marks[3][3] = {
                  {22,33,44},
                  {43,45,66},
                  {67,78,59}
                  };

Here consider array of 3 rows and 3 columns, total elements declared are 3*3 i.e 9. Array elements holds the initialized values as follows,

marks[0][0]=22; marks[0][1]=33;marks[0][2]=44;

marks[1][0]=43;marks[1][1]=45;marks[1][2]=66;

marks[2][0]=67;marks[2][1]=78;marks[2][2]=59;

How to input Two Dimensional Array elements and display

// C program to input 2D array elements and Display
#include <stdio.h>

int main()
{
    int marks[5][4];
    int r,c;
    for (r = 0; r < 5; r++) 
    {
        for(c = 0; c < 4; c++) 
        {
            printf("\nENTER VALUE AT marks[%][%d] :", r, c);
            scanf("%d", &marks[r][c]);
        }
    }

    printf("\n2D Array  Values are: \n\n");
    for (r = 0; r < 5; r++) 
    {
        printf("\nROW %d",r);
        for(c = 0; c < 4; c++)
        {
            printf("%d ",marks[r][c]);
        }
        printf("\n------------------------------");
    }
    return 0;
}

Download Code: How to Input and display 2D array elements (289 downloads)

Output :

 

 

 

 

 

C Program for Two Dimensional Matrix Addition and Subtraction

//C Program for Two Dimensional Matrix Addition and Subtraction


#include <stdio.h>
#define ROWS 3
#define COLS 3

int main()
{

    
    int mat1[ROWS][COLS],mat2[ROWS][COLS],mat3[ROWS][COLS];
    int r,c;
    for (r = 0; r < ROWS; r++) 
    {
        for(c = 0; c < COLS; c++) 
        {
            printf("\nENTER VALUE AT mat1[%d][%d] :", r, c);
            scanf("%d", &mat1[r][c]);
        }
    }

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

    for (r = 0; r < ROWS; r++) 
    {
        for(c = 0; c < COLS; c++) 
        {
            printf("\nENTER VALUE AT mat2[%d][%d] :", r, c);
            scanf("%d", &mat2[r][c]);
        }
    }

    printf("\n--------- MATRIX-1: \n");
    for (r = 0; r < ROWS; r++) 
    {
        for(c = 0; c < COLS; c++)
        {
            printf("%d ",mat1[r][c]);
        }
	printf("\n");
    }

    
    printf("\n--------- MATRIX-2: \n");
    for (r = 0; r < ROWS; r++) 
    {
        for(c = 0; c < COLS; c++)
        {
            printf("%d ",mat2[r][c]);
        }
	printf("\n");
    }

    printf("\n--------- ADDITION OF MATRICES: \n");
    for (r = 0; r < ROWS; r++) 
    {
        for(c = 0; c < COLS; c++)
        {
	    mat3[r][c]=mat1[r][c]+mat2[r][c];
            printf("%d ",mat3[r][c]);
        }
        printf("\n");
    }

return 0;
}

In above program, ROWS and COLS are macros assigns a constant values. mat1 ,mat2 ,mat3 are the 2D array variables of integer data type. it works as following steps.

  1. Input values for mat1
  2. Input values for mat2
  3. display values of mat1 & mat2
  4. Perform addition by instruction  mat3[r][c]=mat1[r][c]+mat2[r][c];
  5. display matrix mat3 which holds addition.

Download –  matrix-addition-operation.c (124 downloads)

Output –

output-matrix-addition
output-matrix-addition

 

C PROGRAM MATRIX MULTIPLICATION CODE WITH EXAMPLE