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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#include <iostream>
#include <stdlib.h>
using namespace std;
class MyMatrix
{
public:
MyMatrix(int dimension);
MyMatrix(int rows, int columns);
~MyMatrix();
void print();
int get_rows();
int get_columns();
double get_element(int row, int column);
void set_element(int row, int column, double value);
MyMatrix operator + (MyMatrix A);
MyMatrix operator * (MyMatrix A);
void operator = (MyMatrix A);
private:
double* m;
int row_d;
int col_d;
int d;
};
MyMatrix::MyMatrix(int dimension)
{
row_d = dimension;
col_d = dimension;
d = dimension*dimension;
m = new double[d];
unsigned int k=0;
for(unsigned int i=0; i<row_d; i++) {
for(unsigned int j=0; j<col_d; j++) {
if(i==j) {
m[k] = 1;
} else {
m[k] = 0;
}
k++;
}
}
}
MyMatrix::MyMatrix(int rows, int columns)
{
row_d = rows;
col_d = columns;
d = rows*columns;
m = new double[d];
srand(time(NULL));
for(unsigned int i=0; i<d; i++) {
m[i] = 1+(rand()%(100-1+1));
}
}
MyMatrix::~MyMatrix()
{
delete[] m;
}
void MyMatrix::print()
{
unsigned int k=0;
for(unsigned int i=0; i<row_d; i++) {
for(unsigned int j=0; j<col_d; j++) {
cout << m[k] << " ";
k++;
}
cout << endl;
}
}
int MyMatrix::get_rows()
{
return row_d;
}
int MyMatrix::get_columns()
{
return col_d;
}
double MyMatrix::get_element(int row, int column)
{
// first element is 1,1!
if((row*column)<=0) {
return -1;
}
return m[((row-1)*col_d)+(column-1)];
}
void
MyMatrix::set_element(int row, int column, double value)
{
if((row*column)>0) {
m[((row-1)*col_d)+(column-1)] = value;
}
}
MyMatrix
MyMatrix::operator + (MyMatrix A)
{
MyMatrix* res = new MyMatrix(row_d, col_d);
unsigned int k=0;
for(unsigned int i=0; i<row_d; i++) {
for(unsigned int j=0; j<col_d; j++) {
res->m[k] = m[k]+A.get_element(i+1, j+1);
k++;
}
}
return *res;
}
MyMatrix MyMatrix::operator * (MyMatrix A)
{
MyMatrix* res = new MyMatrix(row_d, col_d);
unsigned int k=0;
for(unsigned int i=0; i<row_d; i++) {
for(unsigned int j=0; j<col_d; j++) {
res->m[k] = m[k]*A.get_element(i+1, j+1);
k++;
}
}
return *res;
}
void MyMatrix::operator = (MyMatrix A)
{
unsigned int k=0;
for(unsigned int i=0; i<A.get_rows(); i++) {
for(unsigned int j=0; j<A.get_columns(); j++) {
m[k] = A.get_element(i+1, j+1);
k++;
}
}
}
int main (int argc, char* const argv[]) {
cout << "9x9 identity matrix:" << endl;
MyMatrix* m = new MyMatrix(9);
m->print();
cout << " rows: " << m->get_rows() << ", ";
cout << "columns: " << m->get_columns() << endl;
delete m;
cout << endl << "2x5 matrix:" << endl;
MyMatrix n(2,5);
n.print();
cout << " rows: " << n.get_rows() << ", ";
cout << "columns: " << n.get_columns() << endl;
cout << "(1,3) = " << n.get_element(1,3) << endl;
cout << "last: (2,5) = " << n.get_element(2,5) << endl;
cout << "second to last element = 100: " << endl;
n.set_element(2, 4, 100);
n.print();
cout << endl << "addition:" << endl;
cout << "matrix 1:" << endl;
MyMatrix* a1 = new MyMatrix(3,3);
a1->print();
cout << "matrix 2:" << endl;
MyMatrix* a2 = new MyMatrix(3,3);
a2->print();
cout << "result:" << endl;
MyMatrix* e1 = new MyMatrix(3,3);
*e1 = *a1+*a2;
e1->print();
delete e1;
cout << endl << "multiplication" << endl;
cout << "matrix 1:" << endl;
MyMatrix* m1 = new MyMatrix(3,3);
a1->print();
cout << "matrix 2:" << endl;
MyMatrix* m2 = new MyMatrix(3,3);
m2->print();
cout << "result:" << endl;
MyMatrix* e2 = new MyMatrix(3,3);
*e2 = *m1 * *m2;
e2->print();
delete e2;
return 0;
}
|