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
|
#include <cstdlib>
#include <iostream>
using namespace std;
void matpop(int m, int n, float *matToPop) {
int i, j;
float tempFlt = .0f;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
tempFlt = i * n + j;
matToPop[i * n + j] = tempFlt;
}
}
}
void matmult(int m, int p, int n, float *a, float *b, float *c) {
int i, j, k;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
float tempFlt = .0f;
for (k = 0; k < p; k++) {
tempFlt += a[i * p + k] * b[k * n + j];
}
c[i * n + j] = tempFlt;
}
}
}
void matprint(int m, int n, float *outputMat) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
float tmp = outputMat[i * n + j];
cout << tmp << "\t";
}
cout << "\n";
}
cout << "\n";
}
int main(int argc, char** argv) {
int m, p, n;
cout << "Please enter the number of rows in array A: ";
cin >> m;
cout << "Please enter the number of columns in array A: ";
cin >> p;
cout << "Please enter the number of columns in array B: ";
cin >> n;
float a[m * p];
float b[p * n];
float c[m * n];
matpop(m, p, a);
matpop(p, n, b);
matmult(m, p, n, a, b, c);
cout << "The values for matrix A are:\n";
matprint(m, p, a);
cout << "The values for matrix B are:\n";
matprint(p, n, b);
cout << "The values for matrix C are:\n";
matprint(m, n, c);
}
|