-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
70 lines (63 loc) · 1.61 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
#include <stdlib.h>
#include <stdio.h>
float **MultiplyMatrix(int lenght, float **matrix1, float **matrix2)
{
//Allocates space for the answer to the matrix multiplications
float **answer = (float **)calloc(lenght, sizeof(float *));
for (int count = 0; count <= lenght - 1; count++)
{
answer[count] = new float[lenght];
}
//loops through each entrance and does the calculations
for (int P = 0; P <= lenght - 1; P++)
{
for (int i = 0; i <= lenght - 1; i++)
{
for (int L = 0; L <= lenght - 1; L++)
{
answer[P][i] += matrix1[P][i] * matrix2[P][L];
}
}
}
return answer;
}
void printMatrix(float **matrix, int lenght)
{
for (int i = 0; i <= lenght - 1; i++)
{
printf("\n");
for (int L = 0; L <= lenght - 1; L++)
{
printf(" %f ,", matrix[i][L]);
}
}
}
int main()
{
float **test = (float **)calloc(4, sizeof(float *));
for (int count = 0; count <= 3; ++count)
{
test[count] = new float[4]; // these are our columns
}
float **test2 = (float **)calloc(4, sizeof(float *));
for (int count = 0; count <= 3; ++count)
{
test2[count] = new float[4]; // these are our columns
}
for (int i = 0; i <= 3; i++)
{
for (int L = 0; L <= 3; L++)
{
test[i][L] = (float) L;
}
}
for (int i = 0; i <= 3; i++)
{
for (int L = 0; L <= 3; L++)
{
test2[i][L] = (float) L;
}
}
printMatrix(test,4);
printMatrix(MultiplyMatrix(4, test, test2), 4);
}