/
valgrind-bughunt.cpp
71 lines (60 loc) · 1.46 KB
/
valgrind-bughunt.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
// Author: Dan DaCosta for csci3081, lab 5.
// Debugged by team: TEAM_NAME_HERE
// Team Members: NAME1 NAME2 ...
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//newMat: Allocate a new matrix with dimensions x by y filling cell values
// randomly.
int** newMat(int x,int y) {
int ** a = new int*[x];
for(int i = 0; i < x; i++) {
a[i] = new int[y];
for(int j = 0; j < y; j++) {
a[i][j] = rand() % 13;
}
}
return a;
}
// mulMat: Multiplies A with dimensions x by y by B with dimensions y by z and
// returns a new matrix C with dimensions x by z.
// Pseudocode for matrix multiplication:
// for i from 1 to x
// for j from 1 to z
// C[i,j] = 0
// for k from 1 to y
// C[i,j] += A[i,k]B[k,j]
int** mulMat(int x, int y, int z, int ** A, int ** B) {
int ** R = newMat(x,z);
for(int i = 0; i < x; i++) {
for(int j = 0; j < z; j++) {
R[i][j] = 0;
for(int k = 0; k < y; k++) {
R[i][j] += A[i][k]*B[k][j];
}
}
}
return R;
}
// prnMat: Print the matrix A with dimensions x by y.
void prnMat(int x, int y, int ** A) {
cout<<"Printing "<<x<<" by "<<y<<" matrix:"<<endl;
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
cout<<A[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void execute() {
int ** A = newMat(7,11);
int ** B = newMat(11,13);
int ** C = mulMat(7,11,13,A,B);
prnMat(7,13,C);
}
int main(int ,char **) {
execute();
return 0;
}