0

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;
    }
}
nineduey
  • 9
  • 1
  • So I need to declare the c array within the multiply function instead of main? – nineduey Mar 17 '20 at 02:22
  • Maybe you are trying to index with double bracket, given parameter type is single-pointer. The compiler doesn't know how much the size of each slot is for second bracket. You're not getting any error in displayMatrix() function, because you are properly indexing single-pointer with one bracket. – MyBug18 Mar 17 '20 at 02:25
  • Ok, so it's not recognizing my passed arrays as 2D. That makes sense since my display function only utilizes a single pair of brackets. – nineduey Mar 17 '20 at 02:28
  • Yes, exactly. And if you want to use 2D array, at least you need to tell compiler about how much slots are in a row, or column. Not providing any of these, compiler would not know how many index should be skipped when a row index has increased. – MyBug18 Mar 17 '20 at 02:31
  • That is what int N, M and P are and those are being passed to the function just fine. Is the syntax incorrect when passing the 2D arrays a & b to the function? – nineduey Mar 17 '20 at 02:37
  • 1
    https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function/17569578#17569578 This answer gives nice explanation. Try read this. – MyBug18 Mar 17 '20 at 02:40

0 Answers0