WEB DEVELOPER SITE

# perform various Matrix operation on a given 2-D Array C++ Tutorial

Following is the program to perform various Matrix operation on a given 2-D Array. .

```#include<iostream.h>
#include<conio.hgt;
int main(){
clrscr();
int a,b,c,i,j,m,n,o,p,l=1,r;
cout<<"Enter case:\n";
cin>>r;
switch(r){
cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
cingt;gt;m;
cout<<"* \n";
cingt;gt;n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (m==o&&n==p){
}
else{
l=0;
}

if (l){
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++){
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++){
cout<<endl;
for (j=1;j<=n;j++)
cout<>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++){
cout<>m;
cout<<"* \n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (m==o&&n==p){
cout<<"Subtracion possible "<<endl;
}
else{
cout<<"Subtraction not possible ";
l=0;
}

if (l){
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++){
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++){
cout<<endl;
for (j=1;j<=n;j++)
cout<>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++){
cout<>m;
cout<<"* \n";
cin>>n;
cout<<"Enter the order of matrix b (must be o*p): "<<endl;
cin>>o;
cout<<"* \n";
cin>>p;
if (n==o){
cout<<"Multiplication possible "<<endl;
}
else{
cout<<"Multiplication not possible ";
l=0;
}

if (l){
cout<<"\n\nEnter the elements of matrix 1: "<<endl;
for (i=1;i<=m;i++){
for (j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Elements of matrix 1 are: ";
for (i=1;i<=m;i++){
cout<>b[i][j];
}
cout<<"Elements of matrix 2 are: ";
for (i=1;i<=o;i++){
cout<<endl;
for (j=1;j<=p;j++)
cout<<b[i][j]<<" ";
}
cout<<"\n\n\nMultiplication:\nc=aXb=";
for (i=1;i<=m;i++){
for (j=1;j<=p;j++){
c[i][j]=0;
for (int k=1;k<=n;k++){
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for (i=1;i<=m;i++){
cout<<endl;
for (j=1;j<=p;j++)
cout<<c[i][j]<<" ";
}
}
break;
default:
cout<<"Wrong choice";
}
getch();
return 0;
}
```

### Output

```Enter case:
3
Enter the order of matrix a (must be m*n):
2
*
3
Enter the order of matrix b (must be o*p):
3
*
4
Multiplication possible
Enter the elements of matrix 1:
1 2 3 4 5 6
Elements of matrix 1 are:
1 2 3
4 5 6
Enter the elements of matrix 2:
1 2 3 4 5 6 7 8 9 10 11 12
Elements of matrix 2 are:
1 2 3 4
5 6 7 8
9 10 11 12
Multiplication:
c=aXb=
38 44 50 56
83 98 113 128

```