Friday, February 15, 2019

Sum of two Matrices - in C - Language Using Ubuntu

File Name : summat.c

#include <stdio.h>
void main()
{
 int r1,c1,r2,c2,c,d,f[10][10],s[10][10],sum[10][10];
 printf("Enter the number of rows and columns of First Matrix\n");
 scanf("%d%d",&r1,&c1);
 printf("Enter the number of rows and columns of Second Matrix\n");
 scanf("%d%d",&r2,&c2);
 if(r1==r2 && c1==c2)
 {
 printf("Enter the elements of First Matrix\n");
 for(c=0;c<r1;c++)
    for(d=0;d<c1;d++)
       scanf("%d",&f[c][d]);
 printf("Enter the elements of Second Matrix\n");
 for(c=0;c<r2;c++)
    for(d=0;d<c2;d++)
        scanf("%d",&s[c][d]);
 printf("Sum of Two Matrices:\n");
 for(c=0;c<r1;c++)  //we may use r1,c1 combinatio or r2,c2.
 {
  for(d=0;d<c1;d++)
     {
       sum[c][d]=f[c][d]+s[c][d];
       printf("%d\t", sum[c][d]);
     }
  printf("\n");
  }
 }
 else
 {
  printf("Matrix addition is not possible...\n");
 }
}

Output1 :
Enter the number of rows and columns of First Matrix
2
2
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the elements of Second Matrix
1
2
3
4
Sum of Two Matrices:
2    4   
6    8   

Output 2:
Enter the number of rows and columns of First Matrix
2
4
Enter the number of rows and columns of Second Matrix
4
6
Matrix addition is not possible...

No comments:

Post a Comment

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog