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,

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?

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;



How to input Two Dimensional Array elements and display

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

Output :






C Program for Two Dimensional Matrix Addition and Subtraction

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 (13 downloads)

Output –