Write a program in C++ that takes two 4 x 4 matrices as input from the user and displays the matrices in tabular form. Then ask the 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.
#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 ";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter 2nd 4x4 matrix ";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cin>>b[i][j];
}
}
cout<<"\nEnter your choice ";
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,t=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
multiply[i][j]=0;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{multiply[i][j]+=a[i][k]*b[k][j];
}
}
t=0;
}
cout<<"Multiplying 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;j<4;j++)
{
transpose1[i][j]=a[j][i];
transpose2[i][j]=b[j][i];
}
}
cout<<"Transpose of 1st matrix\n";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<transpose1[i][j]<<" ";
}
cout<<"\n";
}
cout<<"Transpose of 2nd matrix\n";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<transpose2[i][j]<<" ";
}
cout<<"\n";
}
}
Comments
Leave a comment