I know using vectors is much easier but it just crossed my mind if i wanted to use C-style arrays to do generic matrix multiplication, how would that work. I searched online and got some help by using templates but the array won't return from the function.
// Example program
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t row1, size_t col1, size_t row2, size_t col2>
typename multiply(T(&a)[row1][col1], T(&b)[row2][col2]) {
if (row1 != col2) {
cout << "Error! Rows of first matrix is not the same as Columns of second "
"matrix, therefore Multiplication is NOT POSSIBLE!";
return 0;
} else {
T c[row1][col2];
for (size_t i = 0; i < row1; ++i) {
for (size_t j = 0; j < col2; ++j) {
c[i][j] = 0;
for (size_t k = 0; k < col1; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
}
int main() {
// C-style array
int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
int my_array3[2][2];
int c[2][2] = multiply(my_array, my_array2);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
cout << c[i][j] << " ";
}
cout << endl;
}
return 0;
}
So any idea how I can make this code work?