# c multi dimensional array

A multidimensional array is an array containing two, three, four, five, or more arrays. The increasing number of dimensions, increases the code complexity for the developers. The most popular arrays are single, two and three dimensional arrays.

Two dimensional Arrays:

A C two-dimensional array can be defined as an array of arrays that needs two indices for every element.

Three dimensional Arrays:

A C three-dimensional array can be defined as an array of arrays of arrays that needs three indices for every element.

Example: Accessing the elements of a two dimensional array.

#include <stdio.h> #include<stdlib.h>   void main() {   int a,b,c,r1,c1,r2,c2,i,j;   printf("Enter the number of rows in first matrix: "); scanf("%d",&r1);   printf("Enter the number of columns in first matrix: "); scanf("%d",&c1);   printf("Enter the first matrix: \n");   for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } }   printf("Enter the number of rows in second matrix: "); scanf("%d",&r2);   printf("Enter the number of columns in second matrix: "); scanf("%d",&c2);   printf("Enter the second matrix: \n");   for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("%d",&b[i][j]); } }   if ((r1==r2) & (c1==c2)) {   for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j] = 0; c[i][j] += a[i][j] * b[i][j]; } }   printf ("Matrix after Addition:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf ("%d",c[i][j]); printf ("\n"); } } }   else printf ("Addition not possible."); }

Output

Enter the number of rows in first matrix: 2 Enter the number of columns in first matrix: 2 Enter the first matrix: 1 2 3 4 Enter the number of rows in second matrix: 2 Enter the number of columns in second matrix: 2 Enter the second matrix: 5 4 3 2 Matrix after Addition: 6 6 6 6