Computer Science, asked by umii61009, 1 month ago

Write a program in C++ which takes two 4 x 4 matrices as input from the user and displays the matrices in tabular form. Then ask user to make a choice:

1. Press 1 for matrix addition
2. Press 2 for matrix subtraction
3. Press 3 for matrix multiplication
4. Press 4 for matrix transpose

Use 2D arrays to save matrix values and use functions to perform addition, subtraction, multiplication and transpose. Refer below examples to understand the concept of matrix operations.

Answers

Answered by m11117shisin
0

Answer:

vhfjgdkskvslxvdkzvxjsb gd

Answered by 2012182
0

Answer:

#include<iostream>

#include<conio.h>

using namespace std;

void add(int a[][4],int b[][4]);

void sub(int a[][4],int b[][4]);

void multiply(int a[][4],int b[][4]);

void transpose(int a[][4],int b[][4]);

int main()

{

   int c,a[4][4],b[4][4],i=0,j=0;

   cout<<"Enter 1st 4x4 matrix values = ";

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

   {

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

       {

           cin>>a[i][j];

       }

   }

   cout<<"\nEnter 2nd 4x4 matrix values= ";

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

   {

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

       {

           cin>>b[i][j];

       }

   }

   cout<<"\nEnter your choice for given below ";

   cout<<"\n1. Matrix Addition";

   cout<<"\n2. Matrix Subtraction";

   cout<<"\n3. Matrix Multiplication";

   cout<<"\n4. Matrix Transpose\n";

   cin>>c;

   switch(c)

   {

       case 1: add(a,b);

               break;

       case 2: sub(a,b);

               break;

       case 3: multiply(a,b);

               break;

       case 4: transpose(a,b);

               break;

       default: cout<<"Wrong input";

   }

   return 0;

}

void add(int a[][4],int b[][4])

{

   int add[4][4],i,j;

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

               {

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

                   {

                     add[i][j]=a[i][j]+b[i][j];

                   }

               }

               cout<<"\nAdding both matrices we get \n";

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

               {

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

                   {

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

                   }

                   cout<<"\n";

               }

}

void sub(int a[][4],int b[][4])

{

   int sub[4][4],i,j;

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

               {

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

                   {

                     sub[i][j]=a[i][j]-b[i][j];

                   }

               }

               cout<<"\nSubtracting second matrix from first matrix we get\n ";

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

               {

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

                   {

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

                   }

                   cout<<"\n";

               }

}

void multiply(int a[][4],int b[][4])

{

   int multiply[4][4],i,j;

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

               {

                   for(j=0;i<4;j++)

                   {

                       multiply[i][j]=a[i][j]*b[j][i];

                   }

               }

               cout<<"\nMultiplying both matrices we get\n";

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

               {

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

                   {

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

                   }

                   cout<<"\n";

               }

}

void transpose(int a[][4],int b[][4])

{

  int transpose1[4][4],transpose2[4][4],i,j;

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

               {

                   for(j=0;i<4;j++)

                   {

                       transpose1[i][j]=a[j][i];

                       transpose2[i][j]=b[i][j];

                   }

               }

               cout<<"\nTranspose of 1st matrix\n";

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

               {

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

                   {

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

                   }

                   cout<<"\n";

               }

               cout<<"\nTranspose of 2nd matrix\n";

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

               {

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

                   {

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

                   }

                   cout<<"\n";

               }

}

Explanation:

Similar questions