SlideShare a Scribd company logo
/* 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

More Related Content

TXT
Los dskn
PDF
Container adapters
PDF
Computer graphics lab report with code in cpp
PPT
Cquestions
PDF
Polymorphism
PDF
c-programming-using-pointers
DOCX
informatics practices practical file
PDF
C++ TUTORIAL 6
Los dskn
Container adapters
Computer graphics lab report with code in cpp
Cquestions
Polymorphism
c-programming-using-pointers
informatics practices practical file
C++ TUTORIAL 6

What's hot (20)

PDF
C++ TUTORIAL 10
PDF
numdoc
PDF
C++ TUTORIAL 9
PDF
The Ring programming language version 1.5.3 book - Part 188 of 194
PDF
C++ TUTORIAL 4
DOCX
Cg my own programs
PDF
Stl algorithm-Basic types
PDF
Data Structure and Algorithm
PDF
Implementing string
PDF
C++ TUTORIAL 7
PDF
C++ TUTORIAL 1
PDF
Understanding storage class using nm
PDF
What We Talk About When We Talk About Unit Testing
PPT
Cursor implementation
PDF
Functional C++
PDF
Microsoft Word Hw#1
PDF
Computer graphics lab manual
DOCX
Computer graphics lab assignment
DOCX
DataStructures notes
PPTX
3. chapter ii
C++ TUTORIAL 10
numdoc
C++ TUTORIAL 9
The Ring programming language version 1.5.3 book - Part 188 of 194
C++ TUTORIAL 4
Cg my own programs
Stl algorithm-Basic types
Data Structure and Algorithm
Implementing string
C++ TUTORIAL 7
C++ TUTORIAL 1
Understanding storage class using nm
What We Talk About When We Talk About Unit Testing
Cursor implementation
Functional C++
Microsoft Word Hw#1
Computer graphics lab manual
Computer graphics lab assignment
DataStructures notes
3. chapter ii
Ad

Viewers also liked (20)

PDF
Stabilus katalogas - UAB „Lintera“
PPT
Modules03 04to03-10
DOCX
PDF
kaborana049005
PDF
M049004
PPTX
Using Social Media to Engage UNH Students
PDF
SP02
PDF
DOCX
4kKkwmp2551
DOC
Women in art
DOCX
1 d array
PDF
SocialMI, progetto di tesi IED per l'Assessorato alle Politiche Sociali del C...
DOCX
Eng2550
PPTX
Mesurer la valeur de vos points d’engagement sur Internet
PPT
Importance du contenu redactionnel pour le referencement : par AxeNet
PDF
Formation génie écologique
PPT
Comment developper une stratégie de contenu ?
PDF
Adw #15 : Les RP à l'heure du 2.0
PPT
Rédaction Web : séduire les internautes et Google
DOCX
Du-terrain-au-dossier-d'Environnement
Stabilus katalogas - UAB „Lintera“
Modules03 04to03-10
kaborana049005
M049004
Using Social Media to Engage UNH Students
SP02
4kKkwmp2551
Women in art
1 d array
SocialMI, progetto di tesi IED per l'Assessorato alle Politiche Sociali del C...
Eng2550
Mesurer la valeur de vos points d’engagement sur Internet
Importance du contenu redactionnel pour le referencement : par AxeNet
Formation génie écologique
Comment developper une stratégie de contenu ?
Adw #15 : Les RP à l'heure du 2.0
Rédaction Web : séduire les internautes et Google
Du-terrain-au-dossier-d'Environnement
Ad

Similar to 2 d matrices (20)

PPTX
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
PPTX
C PROGRAMS - SARASWATHI RAMALINGAM
PDF
C++ TUTORIAL 5
PDF
2D array
PPT
Sparse Matrix and Polynomial
DOC
C lab-programs
DOCX
12th CBSE Practical File
DOCX
PDF
C++ ARRAY WITH EXAMPLES
PPTX
Cse 121 presentation on matrix [autosaved]
PPT
lecture7.ppt
PPT
its arrays ppt for first year students .
PDF
Write Python for Speed
PDF
05_Arrays C plus Programming language22.pdf
PPT
Whats new in_csharp4
PDF
a) In the code, board is initialized by reading an input file. But y.pdf
PPTX
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
PPT
ch03-parameters-objects.ppt
PDF
Cryptography and network security record for cse .pdf
PDF
Data Structure & Algorithms - Matrix Multiplication
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
C PROGRAMS - SARASWATHI RAMALINGAM
C++ TUTORIAL 5
2D array
Sparse Matrix and Polynomial
C lab-programs
12th CBSE Practical File
C++ ARRAY WITH EXAMPLES
Cse 121 presentation on matrix [autosaved]
lecture7.ppt
its arrays ppt for first year students .
Write Python for Speed
05_Arrays C plus Programming language22.pdf
Whats new in_csharp4
a) In the code, board is initialized by reading an input file. But y.pdf
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
ch03-parameters-objects.ppt
Cryptography and network security record for cse .pdf
Data Structure & Algorithms - Matrix Multiplication

Recently uploaded (20)

PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Current and future trends in Computer Vision.pptx
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
communication and presentation skills 01
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Total quality management ppt for engineering students
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
"Array and Linked List in Data Structures with Types, Operations, Implementat...
R24 SURVEYING LAB MANUAL for civil enggi
Current and future trends in Computer Vision.pptx
August 2025 - Top 10 Read Articles in Network Security & Its Applications
III.4.1.2_The_Space_Environment.p pdffdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
Categorization of Factors Affecting Classification Algorithms Selection
communication and presentation skills 01
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Fundamentals of Mechanical Engineering.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Total quality management ppt for engineering students
Exploratory_Data_Analysis_Fundamentals.pdf
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS

2 d matrices

  • 1. /* 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++) {
  • 2. 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; } }
  • 3. 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
  • 4. 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; }
  • 5. 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++)
  • 6. 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++)
  • 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; }
  • 10. 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
  • 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 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
  • 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 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