Matrix Addition, Subtraction, Multiplication

All Operations in Matrix Programs :-

  • Matrix Addition
  • Matrix Subtraction
  • Matrix Multiplication

[cpp]
#include<iostream.h>
int a[3][3],b[3][3],ad[3][3];
void add();
void sub();
void mul();
void main()
{
int ch,i,j;
cout<<"*********************************************************\n";
do
{
cout<<"Press 1 for matrix addition\nPress 2 for matrix substration\
nPress 3 for matrix multiplication\nPress4 for matrix transpose\n
Press 5 for exit\n";
cin>>ch;
switch (ch)
{
case 1:
for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>a[i][j];
}
cout<<"\n";
}

for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>b[i][j];
}
cout<<"\n";
}
add();
cout<<"The array after addition is : \n";
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
cout<<ad[i][j]<<"\t";
}
cout<<"\n";
}
break;

case 2:
for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>a[i][j];
}
cout<<"\n";
}

for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>b[i][j];
}
cout<<"\n";
}
sub();
cout<<"The array after addition is : \n";
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
cout<<ad[i][j]<<"\t";
}
cout<<"\n";
}
break;

case 3:
for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>a[i][j];
}
cout<<"\n";
}

for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>b[i][j];
}
cout<<"\n";
}
mul();
cout<<"The array after addition is : \n";
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
cout<<ad[i][j]<<"\t";
}
cout<<"\n";
}
break;

case 4:
for(i=0; i<=2; i++)
{
cout<<"Enter the element for "<<i+1<<" row : \n";
for(j=0; j<=2; j++)
{
cout<<"Enter the element for "<<j+1<<" column : ";
cin>>a[i][j];
}
cout<<"\n";
}
cout<<"The array is : \n";
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
cout<<a[j][i]<<"\t";
}
cout<<"\n";
}
break;

}
}
while(ch!=5);
cout<<"*********************************************************\n";
}

void add()
{
int i,j;
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
ad[i][j]=a[i][j]+b[i][j];
}
}
}

void sub()
{
int i,j;
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
ad[i][j]=a[i][j]-b[i][j];
}
}
}

void mul()
{
int i,j;
int m;
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
m=0;
for(int n=0; n<=2; n++)
{
m+=a[i][n]*b[n][j];
}
ad[i][j]=m;
}
}
}
[/cpp]

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.