CodesJava

Easy learning with example program codes

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[10][10],b[10][10],c[10][10],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
Posted in C   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki