-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix adt.cpp
121 lines (104 loc) · 3.05 KB
/
matrix adt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<string.h>
using namespace std;
struct matrixType{
int matDimension;
int matValues[10][10];
};
class MatrixADT{
private:
matrixType resultMatrix;
public:
void intializeResultMatrix(int);
matrixType add(matrixType, matrixType);
matrixType subtract(matrixType,matrixType);
matrixType multiply(matrixType,matrixType);
void printResult();
};
matrixType MatrixADT::add(matrixType M1, matrixType M2)
{
cout<<"addition of matrix is: "<<"\n";
int i,j;
for(i=0;i<M1.matDimension;i++)
for(j=0;j<M2.matDimension;j++)
{
resultMatrix.matValues[i][j]=M1.matValues[i][j]+M2.matValues[i][j];
}
return (M1);
}
matrixType MatrixADT::subtract(matrixType M1, matrixType M2)
{
cout<<"subtraction of matrix is: "<<"\n";
int i,j;
for(i=0;i<M1.matDimension;i++)
for(j=0;j<M2.matDimension;j++)
{
resultMatrix.matValues[i][j]=M1.matValues[i][j]-M2.matValues[i][j];
}
return (M1);
}
matrixType MatrixADT::multiply(matrixType M1, matrixType M2)
{
cout<<"multiplication of matrix is: "<<"\n";
int i,j,k;
for(i=0;i<M1.matDimension;i++)
for(j=0;j<M2.matDimension;j++)
for(k=0;k<M1.matDimension;k++)
{
resultMatrix.matValues[i][j]+=M1.matValues[i][k]*M2.matValues[k][j];
}
return (M1);
}
void MatrixADT::intializeResultMatrix(int dim)
{
int i,j;
resultMatrix.matDimension=dim;
for(i=0;i<dim;i++)
for(j=0;j<dim;j++)
{
resultMatrix.matValues[i][j]=0;
}
}
[]
int main(){
MatrixADT maX;
matrixType M1, M2;
char op;
int dim;
cout<<"enter the dimension of matrix: ";
cin>>dim;
M1.matDimension=M2.matDimension=dim;
int i,j;
cout<<"enter matrix1: "<<"\n";
for(i=0;i<dim;i++)
for(j=0;j<dim;j++)
cin>>M1.matValues[i][j];
cout<<"enter matrix2: "<<"\n";
for(i=0;i<dim;i++)
for(j=0;j<dim;j++)
cin>>M2.matValues[i][j];
maX.intializeResultMatrix(dim);
cout<<"enter your option +,-,* ";
cin>>op;
switch(op)
{
case '+' : M1=maX.add(M1,M2);
break;
case '-' : M1=maX.subtract(M1,M2);
break;
case '*' : M1=maX.multiply(M1,M2);
break;
}
maX.printResult();
return (0);
}
void MatrixADT::printResult()
{
int i,j;
for (i=0;i<resultMatrix.matDimension;i++){
for (j=0; j<resultMatrix.matDimension-1;j++){
cout<<resultMatrix.matValues[i][j]<<" ";
}
cout <<resultMatrix.matValues[i][j]<<"\n";
}
}