I'm trying to build a function that will multiply 2 matrices of different sizes, using 2D arrays. I'm only getting errors in the function I wrote to multiply the matrices, it's saying the counting variables of the two nested loops are the source of the problem and it won't compile, and it's not letting me run it in debugger mode for some odd reason.
Errors are "expression must have point-to-object type" and "subscript requires array or pointer type". Not sure what this means for the variables that are causing the error, as they are being used as counter variables for the nest loops. Everything else works fine without the multiply function.
Here's my whole code for context. Any advice is appreciated! Thanks.
#include <iostream>
using namespace std;
//Function prototypes
void matrixMultiply(int* a, int* b, int* c, int N, int M, int P);
void displayMatrix(int* a, int N, int M);
int main() {
//Constants
int const N = 2; // Rows of Data
int const M = 3; // Rows/Columns of Data
int const P = 4; // Columns of Data
//Declaration of matrices
int a[N][M] = { { 1, 2, 3 }, { 4, 5, 6 } };
int b[M][P] = { {1, 4, 7, 10}, { 2, 5, 8, 11}, {3, 6, 9, 12} };
int c[N][P];
displayMatrix(*a, N, M);
cout << endl;
displayMatrix(*b, M, P);
cout << endl;
matrixMultiply(*a, *b, *c, M, N, P);
return 0;
}
//Function to Multiply - errors occuring here
void matrixMultiply(int* a, int* b, int* c, int N, int M, int P) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < P; j++) {
c[i][j] = 0; // error underlining j
for (int k = 0; k < M; k++) {
c[i][j] += a[i][k] * b[k][j]; // error underlining j and k
}
}
}
displayMatrix(c, N, P);
}
//Function to display
void displayMatrix(int* a, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << a[i * col + j] << " ";
}
cout << endl;
}
}