-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathfunction.cpp
70 lines (56 loc) · 1.57 KB
/
function.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
#include <iostream>
#include <float.h>
struct Matrix
{
size_t rows;
size_t cols;
float * pData;
};
float matrix_max(struct Matrix mat)
{
float max = FLT_MIN;
//find max value of mat
for(size_t r = 0; r < mat.rows; r++)
for (size_t c = 0; c < mat.cols; c++)
{
float val = mat.pData[ r * mat.cols + c];
max = ( max > val ? max : val);
}
return max;
}
Matrix * create_matrix(size_t rows, size_t cols)
{
Matrix * p = new Matrix{rows, cols};
p->pData = new float[p->rows * p->cols]{1.f, 2.f, 3.f};
//you should check if the memory is allocated successfully
return p;
}
bool matrix_add(const Matrix & matA, const Matrix & matB, Matrix & matC)
{
// check the dimensions of the three matrices
// re-create matC if needed
// do: matC = matA + matB
// return true if everything is right
return true;
}
int main()
{
using namespace std;
Matrix matA = {3,4};
matA.pData = new float[matA.rows * matA.cols]{1.f, 2.f, 3.f};
Matrix matB = {4,8};
matB.pData = new float[matB.rows * matB.cols]{10.f, 20.f, 30.f};
Matrix matC = {4, 2};
matC.pData = new float[matC.rows * matC.cols]{100.f, 200.f, 300.f};
// some operations on the matrices
float maxa = matrix_max(matA);
float maxb = matrix_max(matB);
float maxc = matrix_max(matC);
cout << "max(matA) = " << maxa << endl;
cout << "max(matB) = " << maxb << endl;
cout << "max(matC) = " << maxc << endl;
delete [] matA.pData;
delete [] matB.pData;
delete [] matC.pData;
return 0;
}