Sunday, 7 February 2021

WAP in C++ to find the sum of two compatible matrices.

 //To find the sum of two compatible matrices

#include<iostream>

using namespace std;

main()

{

    int r1,c1,r2,c2,i,j,sum[50][50];

    cout<<"Enter the order of the first matrix\n";

    cin>>r1>>c1;

    int arr1[r1][c1];

    cout<<"Enter the elements of the first matrix\n";

    for(i=0;i<r1;i++)

    {

        for(j=0;j<c1;j++)

        cin>>arr1[i][j];

    }

    

    cout<<"Enter the order of the second matrix\n";

    cin>>r2>>c2;

    int arr2[r2][c2];

    cout<<"Enter the elements of the second matrix\n";

    

    for(i=0;i<r2;i++)

    {

        for(j=0;j<c2;j++)

        cin>>arr2[i][j];

    }

    

    if(r1==r2&&c1==c2)

    {

    for(i=0;i<r1;i++)

    {

      for(j=0;j<c1;j++)

      sum[i][j]=arr1[i][j]+arr2[i][j];

    }

    cout<<"The resultant matrix is:\n";

    for(i=0;i<r1;i++)

    {

        for(j=0;j<c1;j++)

        cout<<sum[i][j]<<" ";

        cout<<"\n";

    }

    }

    else 

    cout<<"Matrices are not compatible …";

}

No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...