Addition of two matrices in C

Thursday, December 31, 2024


#include
#include
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
/* m - Number of rows
   n - Number of Columns   */
clrscr();
printf("\nEnter the number of Rows of Mat1 : ");
scanf ("%d",&m1);
printf("\nEnter the number of Columns of Mat1 : ");
scanf ("%d",&n1);
/* Accept the Elements in m x n Matrix */
for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("Enter the Element a[%d][%d] : ",i,j);
    scanf("%d",&a[i][j]);
    }
// ------------------------------------------
printf("\nEnter the number of Rows of Mat2 : ");
scanf ("%d",&m2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf ("%d",&n2);
/* Before accepting the Elements Check if no of
   rows and columns of both matrices is equal */
if ( m1 != m2 || n1 != n2 )
 {
 printf("\nOrder of two matrices is not same ");
 exit(0);
 }
 // ------  Terminate Program if Orders are unequal
 // ------  exit(0) :  0 for normal Termination
/* Accept the Elements in m x n Matrix */
for( i = 0 ; i < m2 ; i++ )
    for( j = 0 ; j < n2 ; j++ )
    {
    printf("Enter the Element b[%d][%d] : ",i,j);
    scanf("%d",&b[i][j]);
    }
// ------------------------------------------
/* Addition of two matrices */
for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    c[i][j] = a[i][j] + b[i][j] ;
    }
/*  Print out the Resultant Matrix */
printf("\nThe Addition of two Matrices is : \n");
for( i = 0 ; i < m1 ; i++ )
 {
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("%d\t",c[i][j]);
    }
  printf("\n");
 }
getch();
}



Output

Enter the number of Rows of Mat1 : 3
Enter the number of Columns of Mat1 : 3
Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1
Enter the number of Rows of Mat2 : 3
Enter the number of Columns of Mat2 : 3
Enter the Element b[0][0] : 1
Enter the Element b[0][1] : 2
Enter the Element b[0][2] : 3
Enter the Element b[1][0] : 2
Enter the Element b[1][1] : 1
Enter the Element b[1][2] : 1
Enter the Element b[2][0] : 1
Enter the Element b[2][1] : 2
Enter the Element b[2][2] : 1
The Addition of two Matrices is :
2       4       6
4       2       2
2       4       2

Note : 2-D array needs two nested for loops




Keep in mind :
  1. One Matrix can be added with another only if the order of both matrices is Equal 
  2. No of rows of MAT-1 = No of rows of MAT-2
  3. No of col of MAT-1 = No of col of MAT-2
  4. During addition a[0][0] is added with b[0][0] and result is stored in c[0][0]

Special Note :


We required two 'for loops' (nested) for following Perpose :

  1. Accepting Matrix
  2. Displaying Matrix
  3. Manipulating Matrix
  4. Performing Different Operations on Matrix



Bookmark & Share

4 comments:

Anonymous said...

gud site
very helpful

Anonymous said...

this site is quite helpful for beginners with all basic programs,and then gradually to some tougher level
thus you can learn step by step.

AbhijeetGade said...

just explain how the nested for loops are get executed...if diagramatically explained..then quite better to understand the concept...

Admin said...

@AbhijitGade
You can Goto This Link for Detailed Explanation
https://www.c4learn.com/2009/12/nested-for-loop-in-c-nested-for-loop.html

Total Visits

  © Blogger templates Newspaper by Ourblogtemplates.com 2008

Back to TOP