-
Notifications
You must be signed in to change notification settings - Fork 93
/
Matrix.cpp
175 lines (173 loc) · 3.25 KB
/
Matrix.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// A template class for Matrix program
#include<iostream>
using namespace std;
template<class T>
class matrix
{
int row, col;
T **mat;
public:
matrix(int, int, int);
matrix(const matrix<T> &);
matrix<T> operator *(matrix<T> obj2);
void display();
~matrix();
};
template<class T>
matrix<T>::matrix(int a, int b, int flag):row(a), col(b)
{
if(flag)
{
cout << endl << "Enter values for the matrix: " << endl;
mat = new T *[row];
for(int i = 0; i < row; i++)
{
mat[i] = new T [col];
for(int j = 0; j < col; j++)
{
cout << "\nEnter value ( " << i+1 << ", " << j+1 << " )\n";
cin >> mat[i][j];
}
}
}
else
{
mat = new T *[row];
for(int i = 0; i < row; i++)
{
mat[i] = new T [col];
}
}
};
template<class T>
matrix<T>::matrix(const matrix<T> &obj)
{
row = obj.row;
col = obj.col;
mat = new T*[row];
for(int i = 0; i < row; i++)
{
mat[i] = new T [col];
}
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
mat[i][j] = obj.mat[i][j];
}
}
};
template<class T>
matrix<T> matrix<T>::operator *(matrix<T> matB)
{
matrix<T> multi(row, matB.col, 0);
for(int i = 0; i < row; i++)
{
for (int j = 0; j < matB.col; j++)
{
multi.mat[i][j] = 0;
}
}
for(int i = 0; i < row; i++)
{
for (int j = 0; j < matB.col; j++)
{
for(int k = 0; k < col; k++)
{
multi.mat[i][j] += mat[i][k] * matB.mat[k][j];
}
}
}
return multi;
};
template<class T>
void matrix<T>::display()
{
for(int i = 0; i < row; i++)
{
cout << endl;
for(int j = 0; j < col; j++)
{
cout << mat[i][j] << " ";
}
}
cout << endl;
};
template<class T>
matrix<T>::~matrix()
{
cout << "dest" << endl;
for(int i = 0; i < row; i++)
{
delete[] mat[i];
}
delete[] mat;
};
int main()
{
int row1, col1, row2, col2, choice = 0, isCompatible = 0;
while (isCompatible != 1)
{
cout << "\nEnter dimensions for matrix 1: ";
cin >> row1 >> col1;
cout << "\nEnter dimensions for matrix 2: ";
cin >> row2 >> col2;
if (col1 == row2) isCompatible = 1;
}
while (choice != 4)
{
cout << "\nUse\n1 -> Integer Matrix\n2 -> Float Matrix\n3 -> Enter new dimensions\n4 -> Exit\nEnter Your Choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
matrix<int> intMatA(row1, col1, 1);
matrix<int> intMatB(row2, col2, 1);
matrix<int> intMatC = intMatA * intMatB;
cout << "\nMatrix 1:\n";
intMatA.display();
cout << "\nMatrix 2:\n";
intMatB.display();
cout << "\nResultant Matrix:\n";
intMatC.display();
break;
}
case 2:
{
matrix<float> intMatA(row1, col1, 1);
matrix<float> intMatB(row2, col2, 1);
matrix<float> intMatC = intMatA * intMatB;
cout << "\nMatrix 1:\n";
intMatA.display();
cout << "\nMatrix 2:\n";
intMatB.display();
cout << "\nResultant Matrix:\n";
intMatC.display();
break;
}
case 3:
{
isCompatible = 0;
while (isCompatible != 1)
{
cout << "\nEnter dimensions for matrix 1: ";
cin >> row1 >> col1;
cout << "\nEnter dimensions for matrix 2: ";
cin >> row2 >> col2;
if (col1 == row2) isCompatible = 1;
else cout << "\nInvalid dimensions.\nPlease input dimensions again.\n";
}
break;
}
case 4:
{
return 0;
break;
}
default:
cout << "\nInvalid Choice.\n";
break;
}
}
}