/* Wap in c++ to perform basic operations on 2-D matrices using UDF */
#include<iostream.h>
#include<process.h>
#include<conio.h>
inta[80][80],b[80][80]; // 2-D matrices
inti=0, j=0;
intm, n, p,q; // Matrix 1 : m= no. of Rows, n=no. of Columns
// Matrix 2 : p= no. of Rows, q=no. of Columns
intmc, oc, sum=0;
// FunctionDeclarations
voidadd(inta[80][80],intb[80][80]);
voidsubtract(inta[80][80],intb[80][80]);
voidmultiply(inta[80][80],intb[80][80]);
voidmultiply1(intb[80][80],inta[80][80]);
voidequivalency( inta[80][80],intb[80][80]);
voidupper_tri(inta[80][80]);
voidlower_tri(inta[80][80]);
voiddiagonal_1_a(inta[80][80]);
voidrow_sum_a(inta[80][80]);
voidcolumn_sum_a(inta[80][80]);
voidmain() //mainbody
{clrscr();
int ch,op;char choice;
l: cout<<"nnDescribe the Sizeof matrix 1: ";
cout<<"nnntEnterthe numberof rows:";
cin>>m;
cout<<"nntEnterthe numberof columns:";
cin>>n;
cout<<"nnEnterthe elementsof matrix 1: nn";
for(i=0;i<m; i++)
for(j=0;j<n; j++)
cin>>a[i][j];
cout<<"nnnnDescribe the sizeof matrix 2 : ";
cout<<"nnntEnterthe numberof rows:";
cin>>p;
cout<<"nntEnterthe numberof columns:";
cin>>q;
cout<<"nnEnterthe elementsof matrix 2: nn";
for(i=0;i<p; i++)
for(j=0;j<q; j++)
cin>>b[i][j];
for(i=0;i<80; i++)
cout<<"-";
cout<<"nnMatrix 1 is : nn";
for(i=0;i<m; i++)
{
for(j=0;j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"nn";
}
cout<<"nnMatrix 2 is : nn";
for(i=0;i<p; i++)
{
for(j=0;j<q; j++)
{
cout<<b[i][j]<<" ";
}
cout<<"nn";
}
do{
cout<<"nnChoose fromthe followingmenu:";
cout<<"nn1.Add two2-D arrays ";
cout<<"nn2.Subtract two2-D arrays ";
cout<<"nn3.Multiplytwo2-D arrays " ;
cout<<"nn4.Checkequivalencyof two2-D matrices.";
cout<<"nn5.Upper triangularmatrix ";
cout<<"nn6.LowerTriangularmatrix ";
cout<<"nn7.Displayandfindsumof diagonal elements";
cout<<"nn8.Displayandfindrowwise sumof 2-D array ";
cout<<"nn9.Displayandfindcolumnwise sumof 2-D array ";
cout<<"nn10. Exit";
cout<<"nnnnEnteryourchoice :";
cin>>ch;
switch(ch)
{
case 1 : if(m==p)
if(n==q)
add(a,b);
else
{
cout<<"nnNumberof columnsof matricesare notsame. nEnterthe matricesagain.";
goto l;
}
else
{
cout<<"nnNumberof rowsof matricesare not same. nEnterthe matricesagain.";
goto l;
}
break;
case 2 : if(m==p)
if(n==q)
{
kk:
cout<<"nnt1.SubtractMatrix 2 fromMatrix 1 ";
cout<<"nnt1.SubtractMatrix 1 fromMatrix 2 ";
cout<<"nntEnteryourchoice :";cin>>mc;
switch(mc)
{
case 1 : subtract(a,b);break;
case 2 : subtract(b,a);break;
default:cout<<"nnPleaseenterdesiredkeyword.";gotokk;
}
}
else
{
cout<<"nnNumberof columnsof matricesare notsame. nEnterthe matricesagain.";
goto l;
}
else
{
cout<<"nnNumberof rowsof matricesare not same. nEnterthe matricesagain.";
goto l;
}
break;
case 3 : cout<<"nnt(i). Multiplymatrix 1withmatrix 2 ";
cout<<"nnt(ii).Multiplymatrix 2withmatrix 1 ";
cout<<"nntEnteryourchoice : "; cin>>op;
switch(op)
{
case 1 : if(n==p)
multiply(a,b);
else
{
cout<<"nnMultiplicaionnotpossible,asno.of columnsof firstmatrix != no.of rowsof
secondmatrix. nEnterthe matricesagain.";
gotol;
}
break;
case 2 : if(m==q)
multiply1(b,a);
else
{
cout<<"nnMultiplicaion notpossible,asno.of columnsof firstmatrix != no. of rows of
secondmatrix. nEnterthe matricesagain.";
goto l;
}
break;
}
break;
case 4: if(m==p)
if(n==q)
equivalency(a,b);
else
cout<<"nnMatricesare not equivalent.";
else
cout<<"nnMatricesare notequivalent.";
break;
case 5:
cout<<"nnt(i).DisplayUppertriangularof matrix 1";
cout<<"nnt(ii).DisplayUppertriangularof matrix 2";
cout<<"nntPlease enteryourchoice :"; cin>>mc;
switch(mc)
{
case 1 : if(m==n)
upper_tri(a);
else
cout<<"nnUpperTriangle cannotbe displayed.Matrix shouldbe square.";
break;
case 2 : if(p==q)
upper_tri(b);
else
cout<<"nnUpperTriangle cannotbe displayed.Matrix shouldbe square.";
break;
}
break;
case 6: cout<<"nnt(i).DisplayLowertriangularmatrix 1";
cout<<"nnt(ii).DisplayLowertriangularmatrix 2 ";
cout<<"nntEnteryourchoice : "; cin>>mc;
switch(mc)
{
case 1 : if(m==n)
lower_tri(a);
else
cout<<"nnLowerTriangle cannotbe displayed.Matrix shouldbe square.";
break;
case 2 : if(p==q)
lower_tri(b);
else
cout<<"nnLowerTriangle cannotbe displayed.Matrix shouldbe square.";
break;
}
break;
case 7:
cout<<"nnt(i).Diagonalsof matrix 1";
cout<<"nnt(ii).Diagonalsof matrix 2";
cout<<"nntEnteryourchoice : "; cin>>oc;
switch(oc)
{
case 1 : cout<<"nntt1.Findsumof diagonal (top-lefttobottom-right) ";
cout<<"nntt2.Findsumof diagonal (bottom-lefttotop-right) ";
cout<<"nnttEnteryourchoice :"; cin>>mc;
diagonal_1_a(a);
break;
case 2 : cout<<"nntt1.Findsumof diagonal (top-lefttobottom-right) ";
cout<<"nntt2.Findsumof diagonal (bottom-lefttotop-right) ";
cout<<"nnttEnteryourchoice :"; cin>>mc;
diagonal_1_a(b);
break;
}
break;
case 8:
cout<<"nnt1.Displayandfindrow wise sumof a matrix 1 : ";
cout<<"nnt2.Displayandfindrow wise sumof a matrix 2 : ";
cout<<"nntEnteryourchoice :"; cin>>oc;
switch(oc)
{
case 1 : row_sum_a(a); break;
case 2 : row_sum_a(b);break;
}
break;
case 9: cout<<"nnt1.Displayandfindcolumnwise sumof a matrix 1 : ";
cout<<"nnt2.Displayandfindcolumnwise sumof a matrix 2 : ";
cout<<"nntEnteryourchoice :"; cin>>oc;
switch(oc)
{
case 1 : column_sum_a(a);break;
case 2 : column_sum_a(b);break;
}
break;
case 10 : exit(10);
}
cout<<"nnnWanttochoose again=> "; cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
}
//----------------------------------------------------------- FunctionDefinitions ---------------------------------------------------------
voidadd(inta[80][80],intb[80][80])
{
intc[80][80];
for(i=0;i<m;i++)
for(j=0;j<n; j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"nnnResultantmatrix byadditionis: nn";
for(i=0;i<m;i++)
{
for(j=0;j<n; j++)
{
cout<<c[i][j]<<"";
}
cout<<"n";
}
}
voidsubtract(inta[80][80],intb[80][80])
{intl,u;
if (mc==1)
{
l=m;
u=n;
}
else
l=p;
u=q;
intc[80][80];
for(i=0;i<l;i++)
for(j=0;j<u; j++)
c[i][j]=a[i][j]-b[i][j];
cout<<"nnnResultantmatrix by Subtraction is:nn";
for(i=0;i<m;i++)
{
for(j=0;j<n; j++)
{
cout<<c[i][j]<<"";
}
cout<<"n";
}
}
voidmultiply(inta[80][80],intb[80][80])
{
intc[80][80];
j=0;
intk=0;
cout<<”nnResultantof Matrix Multiplication:“;
for(i=0;i<m;i++)
{ cout<<"n";
for(j=0; j<q;j++)
{
c[i][j]=0;
for(k=0;k<n; k++)
{ c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
cout<<c[i][j]<<"";
}
}
}
voidmultiply1(intb[80][80],inta[80][80])
{
intc[80][80];
j=0;
intk=0;
intsum=0;
for(i=0;i<n;i++)
{
for(k=0; k<n; k++)
{
for(j=0;j<n; j++)
{
sum=sum+b[i][j]*a[j][k];
}
c[i][k]=sum;
sum=0;
}
}
cout<<”nnResultantof Matrix Multiplication:“;
cout<<"nnnn";
for(i=0;i<p;i++)
{
for(j=0; j<n;j++)
cout<<c[i][j]<<"";
cout<<"n";
}
}
voidequivalency( inta[80][80],intb[80][80])
{intcount=0;
for(i=0;i<m; i++)
{
for(j=0;j<n; j++)
{
if(a[i][j]==b[i][j])
++count;
}
}
if(count==(m*n))
cout<<"nnThe matricesare equivalent.";
else
cout<<"nnThe matricesare not equivalent.";
}
voidupper_tri(inta[80][80])
{ints;
if(mc==1)
s=m;
else
s=p;
cout<<”nnUppertriangle of matrix is: “;
for(i=0;i<s; i++)
{
for(intl=0; l<=(i+1)*2; l++)
cout<<" ";
for(j=i;j<s;j++)
{
cout<<a[i][j]<<"";
}
cout<<"n";
}
}
voidlower_tri(inta[80][80])
{intz;
if(mc==1)
z=m;
else
z=p;
cout<<”nnLowertriangle of matrix is: “;
for(i=0;i<z; i++)
{
for(j=0;j<=i; j++)
{
cout<<a[i][j]<<"";
}
cout<<"n";
}
}
voiddiagonal_1_a(inta[80][80])
{intui;
if(oc==1)
ui=m;
else
ui=p;
if(mc==2)
{
sum=0;
cout<<"nnDiagonal Elementsare : nn";
for(i=0;i<ui;i++)
{
cout<<a[i][ui-(i+1)]<<"n";
sum+=a[i][ui-(i+1)];
}
cout<<"nnSumof diagonal elementsis:"<<sum;
}
else if(mc==1)
{ sum=0;
cout<<"nnDiagonal Elementsare : nn";
for(i=0;i<ui;i++)
{
cout<<a[i][i]<<"n";
sum+=a[i][i];
}
cout<<"nnSumof diagonal elementsis:"<<sum;
}
}
voidrow_sum_a(inta[80][80])
{intv,u;
if(oc==1)
{
v=m;
u=m;
}
else
{
v=p;
u=q;
}
for(i=0;i<v; i++)
{ sum=0;
for(j=0; j<u;j++)
{
sum=sum+a[i][j];
}
cout<<"nnSumof Row "<<i+1<<" = "<<sum;
}
}
voidcolumn_sum_a(inta[80][80])
{intv,u;
if(oc==1)
{
v=m;
u=m;
}
else
{
v=p;
u=q;
}
for(j=0; j<v;j++)
{ sum=0;
for(i=0;i<u; i++)
{
sum=sum+a[i][j];
}
cout<<"nnSumof Column"<<j+1<<" = "<<sum;
}
}
Output:
Describe the Size of matrix 1 :
Enter the numberof rows: 3
Enter the numberof columns: 3
Enter the elementsof matrix 1 :
1
2
3
4
5
6
7
8
9
Describe the size of matrix 2 :
Enter the numberof rows: 3
Enter the numberof columns: 3
Enter the elementsof matrix 2 :
3
2
1
1
2
3
2
0
1
-----------------------------------------------------------------------------------------------------------------------------------------------------
Matrix 1 is :
1 2 3
4 5 6
7 8 9
Matrix 2 is :
3 2 1
1 2 3
2 0 1
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 1
Resultantmatrix byadditionis:
4 4 4
5 7 9
9 8 10
Want to choose again=> y
Choose fromthe followingmenu :
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 2
1. Subtract Matrix 2 fromMatrix 1
2. Subtract Matrix 1 fromMatrix 2
Enter yourchoice : 1
Resultantmatrix bySubtractionis:
-2 0 2
3 3 3
5 8 8
Want to choose again=> Y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 3
(i). Multiplymatrix 1withmatrix 2
(ii).Multiplymatrix2withmatrix 1
Enter yourchoice : 1
Resultantof matix Multiplication:
11 6 10
29 18 25
47 30 40
Want to choose again=>y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sum of 2-D array
10. Exit
Enter yourchoice : 4
The matricesare not equivalent.
Want to choose again=>Y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two 2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 5
(i).DisplayUppertriangularof matrix 1
(ii).DisplayUppertriangularof matrix 2
Please enteryourchoice : 1
Upper triangle of matrix is:
1 2 3
5 6
9
Want to choose again=> Y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfind row wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 6
(i).DisplayLowertriangularmatrix 1
(ii).DisplayLowertriangularmatrix 2
Enter yourchoice : 2
LowerTriangle of matrix is:
3
1 2
2 0 1
Want to choose again=> Y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 7
(i).Diagonalsof matrix 1
(ii).Diagonalsof matrix 2
Enter yourchoice : 1
1. Findsumof diagonal (top-lefttobottom-right)
2. Findsumof diagonal (bottom-lefttotop-right)
Enter yourchoice : 1
Diagonal Elementsare :
1
5
9
Sumof diagonal elementsis:15
Want to choose again=> y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Displayandfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 8
1. Displayandfindrowwise sumof a matrix 1 :
2. Displayandfindrowwise sumof a matrix 2 :
Enteryour choice : 1
Sumof Row 1 = 6
Sumof Row 2 = 15
Sumof Row 3 = 24
Want to choose again=> Y
Choose fromthe followingmenu:
1. Add two2-D arrays
2. Subtract two 2-D arrays
3. Multiplytwo2-D arrays
4. Checkequivalencyof two2-Dmatrices.
5. Upper triangularmatrix
6. Lower Triangularmatrix
7. Displayandfindsumof diagonal elements
8. Displayandfindrow wise sumof 2-D array
9. Display andfindcolumnwise sumof 2-D array
10. Exit
Enter yourchoice : 9
1. Displayandfindcolumnwise sumof a matrix 1 :
2. Displayandfindcolumnwise sumof a matrix 2 :
Enteryour choice : 2
Sumof Column1 = 6
Sumof Column2 = 4
Sumof Column3 = 5
Want to choose again=> N

2 d matrices

  • 1.
    /* Wap inc++ to perform basic operations on 2-D matrices using UDF */ #include<iostream.h> #include<process.h> #include<conio.h> inta[80][80],b[80][80]; // 2-D matrices inti=0, j=0; intm, n, p,q; // Matrix 1 : m= no. of Rows, n=no. of Columns // Matrix 2 : p= no. of Rows, q=no. of Columns intmc, oc, sum=0; // FunctionDeclarations voidadd(inta[80][80],intb[80][80]); voidsubtract(inta[80][80],intb[80][80]); voidmultiply(inta[80][80],intb[80][80]); voidmultiply1(intb[80][80],inta[80][80]); voidequivalency( inta[80][80],intb[80][80]); voidupper_tri(inta[80][80]); voidlower_tri(inta[80][80]); voiddiagonal_1_a(inta[80][80]); voidrow_sum_a(inta[80][80]); voidcolumn_sum_a(inta[80][80]); voidmain() //mainbody {clrscr(); int ch,op;char choice; l: cout<<"nnDescribe the Sizeof matrix 1: "; cout<<"nnntEnterthe numberof rows:"; cin>>m; cout<<"nntEnterthe numberof columns:"; cin>>n; cout<<"nnEnterthe elementsof matrix 1: nn"; for(i=0;i<m; i++) for(j=0;j<n; j++) cin>>a[i][j]; cout<<"nnnnDescribe the sizeof matrix 2 : "; cout<<"nnntEnterthe numberof rows:"; cin>>p; cout<<"nntEnterthe numberof columns:"; cin>>q; cout<<"nnEnterthe elementsof matrix 2: nn"; for(i=0;i<p; i++) for(j=0;j<q; j++) cin>>b[i][j]; for(i=0;i<80; i++) cout<<"-"; cout<<"nnMatrix 1 is : nn"; for(i=0;i<m; i++) { for(j=0;j<n; j++) {
  • 2.
    cout<<a[i][j]<<" "; } cout<<"nn"; } cout<<"nnMatrix 2is : nn"; for(i=0;i<p; i++) { for(j=0;j<q; j++) { cout<<b[i][j]<<" "; } cout<<"nn"; } do{ cout<<"nnChoose fromthe followingmenu:"; cout<<"nn1.Add two2-D arrays "; cout<<"nn2.Subtract two2-D arrays "; cout<<"nn3.Multiplytwo2-D arrays " ; cout<<"nn4.Checkequivalencyof two2-D matrices."; cout<<"nn5.Upper triangularmatrix "; cout<<"nn6.LowerTriangularmatrix "; cout<<"nn7.Displayandfindsumof diagonal elements"; cout<<"nn8.Displayandfindrowwise sumof 2-D array "; cout<<"nn9.Displayandfindcolumnwise sumof 2-D array "; cout<<"nn10. Exit"; cout<<"nnnnEnteryourchoice :"; cin>>ch; switch(ch) { case 1 : if(m==p) if(n==q) add(a,b); else { cout<<"nnNumberof columnsof matricesare notsame. nEnterthe matricesagain."; goto l; } else { cout<<"nnNumberof rowsof matricesare not same. nEnterthe matricesagain."; goto l; } break; case 2 : if(m==p) if(n==q) { kk: cout<<"nnt1.SubtractMatrix 2 fromMatrix 1 "; cout<<"nnt1.SubtractMatrix 1 fromMatrix 2 "; cout<<"nntEnteryourchoice :";cin>>mc; switch(mc) { case 1 : subtract(a,b);break; case 2 : subtract(b,a);break; default:cout<<"nnPleaseenterdesiredkeyword.";gotokk; } }
  • 3.
    else { cout<<"nnNumberof columnsof matricesarenotsame. nEnterthe matricesagain."; goto l; } else { cout<<"nnNumberof rowsof matricesare not same. nEnterthe matricesagain."; goto l; } break; case 3 : cout<<"nnt(i). Multiplymatrix 1withmatrix 2 "; cout<<"nnt(ii).Multiplymatrix 2withmatrix 1 "; cout<<"nntEnteryourchoice : "; cin>>op; switch(op) { case 1 : if(n==p) multiply(a,b); else { cout<<"nnMultiplicaionnotpossible,asno.of columnsof firstmatrix != no.of rowsof secondmatrix. nEnterthe matricesagain."; gotol; } break; case 2 : if(m==q) multiply1(b,a); else { cout<<"nnMultiplicaion notpossible,asno.of columnsof firstmatrix != no. of rows of secondmatrix. nEnterthe matricesagain."; goto l; } break; } break; case 4: if(m==p) if(n==q) equivalency(a,b); else cout<<"nnMatricesare not equivalent."; else cout<<"nnMatricesare notequivalent."; break; case 5: cout<<"nnt(i).DisplayUppertriangularof matrix 1"; cout<<"nnt(ii).DisplayUppertriangularof matrix 2"; cout<<"nntPlease enteryourchoice :"; cin>>mc; switch(mc) { case 1 : if(m==n) upper_tri(a); else
  • 4.
    cout<<"nnUpperTriangle cannotbe displayed.Matrixshouldbe square."; break; case 2 : if(p==q) upper_tri(b); else cout<<"nnUpperTriangle cannotbe displayed.Matrix shouldbe square."; break; } break; case 6: cout<<"nnt(i).DisplayLowertriangularmatrix 1"; cout<<"nnt(ii).DisplayLowertriangularmatrix 2 "; cout<<"nntEnteryourchoice : "; cin>>mc; switch(mc) { case 1 : if(m==n) lower_tri(a); else cout<<"nnLowerTriangle cannotbe displayed.Matrix shouldbe square."; break; case 2 : if(p==q) lower_tri(b); else cout<<"nnLowerTriangle cannotbe displayed.Matrix shouldbe square."; break; } break; case 7: cout<<"nnt(i).Diagonalsof matrix 1"; cout<<"nnt(ii).Diagonalsof matrix 2"; cout<<"nntEnteryourchoice : "; cin>>oc; switch(oc) { case 1 : cout<<"nntt1.Findsumof diagonal (top-lefttobottom-right) "; cout<<"nntt2.Findsumof diagonal (bottom-lefttotop-right) "; cout<<"nnttEnteryourchoice :"; cin>>mc; diagonal_1_a(a); break; case 2 : cout<<"nntt1.Findsumof diagonal (top-lefttobottom-right) "; cout<<"nntt2.Findsumof diagonal (bottom-lefttotop-right) "; cout<<"nnttEnteryourchoice :"; cin>>mc; diagonal_1_a(b); break; } break; case 8: cout<<"nnt1.Displayandfindrow wise sumof a matrix 1 : "; cout<<"nnt2.Displayandfindrow wise sumof a matrix 2 : "; cout<<"nntEnteryourchoice :"; cin>>oc; switch(oc) { case 1 : row_sum_a(a); break; case 2 : row_sum_a(b);break; }
  • 5.
    break; case 9: cout<<"nnt1.Displayandfindcolumnwisesumof a matrix 1 : "; cout<<"nnt2.Displayandfindcolumnwise sumof a matrix 2 : "; cout<<"nntEnteryourchoice :"; cin>>oc; switch(oc) { case 1 : column_sum_a(a);break; case 2 : column_sum_a(b);break; } break; case 10 : exit(10); } cout<<"nnnWanttochoose again=> "; cin>>choice; }while(choice=='y'||choice=='Y'); getch(); } //----------------------------------------------------------- FunctionDefinitions --------------------------------------------------------- voidadd(inta[80][80],intb[80][80]) { intc[80][80]; for(i=0;i<m;i++) for(j=0;j<n; j++) c[i][j]=a[i][j]+b[i][j]; cout<<"nnnResultantmatrix byadditionis: nn"; for(i=0;i<m;i++) { for(j=0;j<n; j++) { cout<<c[i][j]<<""; } cout<<"n"; } } voidsubtract(inta[80][80],intb[80][80]) {intl,u; if (mc==1) { l=m; u=n; } else l=p; u=q; intc[80][80]; for(i=0;i<l;i++) for(j=0;j<u; j++)
  • 6.
    c[i][j]=a[i][j]-b[i][j]; cout<<"nnnResultantmatrix by Subtractionis:nn"; for(i=0;i<m;i++) { for(j=0;j<n; j++) { cout<<c[i][j]<<""; } cout<<"n"; } } voidmultiply(inta[80][80],intb[80][80]) { intc[80][80]; j=0; intk=0; cout<<”nnResultantof Matrix Multiplication:“; for(i=0;i<m;i++) { cout<<"n"; for(j=0; j<q;j++) { c[i][j]=0; for(k=0;k<n; k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } cout<<c[i][j]<<""; } } } voidmultiply1(intb[80][80],inta[80][80]) { intc[80][80]; j=0; intk=0; intsum=0; for(i=0;i<n;i++) { for(k=0; k<n; k++) { for(j=0;j<n; j++) { sum=sum+b[i][j]*a[j][k]; } c[i][k]=sum; sum=0; } } cout<<”nnResultantof Matrix Multiplication:“; cout<<"nnnn"; for(i=0;i<p;i++) { for(j=0; j<n;j++)
  • 7.
    cout<<c[i][j]<<""; cout<<"n"; } } voidequivalency( inta[80][80],intb[80][80]) {intcount=0; for(i=0;i<m; i++) { for(j=0;j<n;j++) { if(a[i][j]==b[i][j]) ++count; } } if(count==(m*n)) cout<<"nnThe matricesare equivalent."; else cout<<"nnThe matricesare not equivalent."; } voidupper_tri(inta[80][80]) {ints; if(mc==1) s=m; else s=p; cout<<”nnUppertriangle of matrix is: “; for(i=0;i<s; i++) { for(intl=0; l<=(i+1)*2; l++) cout<<" "; for(j=i;j<s;j++) { cout<<a[i][j]<<""; } cout<<"n"; } } voidlower_tri(inta[80][80]) {intz; if(mc==1) z=m; else z=p; cout<<”nnLowertriangle of matrix is: “; for(i=0;i<z; i++) { for(j=0;j<=i; j++) { cout<<a[i][j]<<""; }
  • 8.
    cout<<"n"; } } voiddiagonal_1_a(inta[80][80]) {intui; if(oc==1) ui=m; else ui=p; if(mc==2) { sum=0; cout<<"nnDiagonal Elementsare :nn"; for(i=0;i<ui;i++) { cout<<a[i][ui-(i+1)]<<"n"; sum+=a[i][ui-(i+1)]; } cout<<"nnSumof diagonal elementsis:"<<sum; } else if(mc==1) { sum=0; cout<<"nnDiagonal Elementsare : nn"; for(i=0;i<ui;i++) { cout<<a[i][i]<<"n"; sum+=a[i][i]; } cout<<"nnSumof diagonal elementsis:"<<sum; } } voidrow_sum_a(inta[80][80]) {intv,u; if(oc==1) { v=m; u=m; } else { v=p; u=q; } for(i=0;i<v; i++) { sum=0; for(j=0; j<u;j++) { sum=sum+a[i][j]; } cout<<"nnSumof Row "<<i+1<<" = "<<sum; }
  • 9.
  • 10.
    Output: Describe the Sizeof matrix 1 : Enter the numberof rows: 3 Enter the numberof columns: 3 Enter the elementsof matrix 1 : 1 2 3 4 5 6 7 8 9 Describe the size of matrix 2 : Enter the numberof rows: 3 Enter the numberof columns: 3 Enter the elementsof matrix 2 : 3 2 1 1 2 3 2 0 1 ----------------------------------------------------------------------------------------------------------------------------------------------------- Matrix 1 is : 1 2 3 4 5 6 7 8 9
  • 11.
    Matrix 2 is: 3 2 1 1 2 3 2 0 1 Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 1 Resultantmatrix byadditionis: 4 4 4 5 7 9 9 8 10 Want to choose again=> y Choose fromthe followingmenu : 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix
  • 12.
    6. Lower Triangularmatrix 7.Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 2 1. Subtract Matrix 2 fromMatrix 1 2. Subtract Matrix 1 fromMatrix 2 Enter yourchoice : 1 Resultantmatrix bySubtractionis: -2 0 2 3 3 3 5 8 8 Want to choose again=> Y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit
  • 13.
    Enter yourchoice :3 (i). Multiplymatrix 1withmatrix 2 (ii).Multiplymatrix2withmatrix 1 Enter yourchoice : 1 Resultantof matix Multiplication: 11 6 10 29 18 25 47 30 40 Want to choose again=>y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sum of 2-D array 10. Exit Enter yourchoice : 4 The matricesare not equivalent. Want to choose again=>Y
  • 14.
    Choose fromthe followingmenu: 1.Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two 2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 5 (i).DisplayUppertriangularof matrix 1 (ii).DisplayUppertriangularof matrix 2 Please enteryourchoice : 1 Upper triangle of matrix is: 1 2 3 5 6 9 Want to choose again=> Y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements
  • 15.
    8. Displayandfind rowwise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 6 (i).DisplayLowertriangularmatrix 1 (ii).DisplayLowertriangularmatrix 2 Enter yourchoice : 2 LowerTriangle of matrix is: 3 1 2 2 0 1 Want to choose again=> Y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 7 (i).Diagonalsof matrix 1 (ii).Diagonalsof matrix 2 Enter yourchoice : 1
  • 16.
    1. Findsumof diagonal(top-lefttobottom-right) 2. Findsumof diagonal (bottom-lefttotop-right) Enter yourchoice : 1 Diagonal Elementsare : 1 5 9 Sumof diagonal elementsis:15 Want to choose again=> y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Displayandfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 8 1. Displayandfindrowwise sumof a matrix 1 : 2. Displayandfindrowwise sumof a matrix 2 : Enteryour choice : 1 Sumof Row 1 = 6 Sumof Row 2 = 15 Sumof Row 3 = 24
  • 17.
    Want to chooseagain=> Y Choose fromthe followingmenu: 1. Add two2-D arrays 2. Subtract two 2-D arrays 3. Multiplytwo2-D arrays 4. Checkequivalencyof two2-Dmatrices. 5. Upper triangularmatrix 6. Lower Triangularmatrix 7. Displayandfindsumof diagonal elements 8. Displayandfindrow wise sumof 2-D array 9. Display andfindcolumnwise sumof 2-D array 10. Exit Enter yourchoice : 9 1. Displayandfindcolumnwise sumof a matrix 1 : 2. Displayandfindcolumnwise sumof a matrix 2 : Enteryour choice : 2 Sumof Column1 = 6 Sumof Column2 = 4 Sumof Column3 = 5 Want to choose again=> N