Why do I have errors about variables that I pass to a function to compute the multiplication of 2 matrices?

Hello,

I am trying to calculate the multiplication of 2 matrices. I have firstly dynamically created the matrices following user input of their sizes. Then when I pass the pointer itself to a function I have made, I have a variety of errors,

ays

argument of type "double **" is incompatible with parameter of type "double *"
void Multiply function, in relation to the way I have indexed the arr

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cassert>

using namespace std;

void Multiply(double*[], double*[], double*[], int, int, int, int);
double** MakeMatrix(int, int);
bool MatrixMultiplicationCheck(int, int);
void DataEntryMatrix(double**, int, int);
void FreeMatrixMemory(int numRows, double** matrix);

double** MakeMatrix(int numRows, int numCols)
{
    double** matrix;
    matrix = new double* [numRows]; 
    for (int i = 0; i < numRows; i++) 
    {
        matrix[i] = new double[numCols];
    } 
    return matrix;
}

void FreeMatrixMemory(int numRows, double** matrix)
{
    for (int i = 0; i < numRows; i++) 
    {
        delete[] matrix[i];
    } 
    delete[] matrix;
}

void DataEntryMatrix(double** x, int numRows, int numCols)
{
    double temp;

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            cout << "What is number you would like to enter?" << endl;
            cin >> temp;
            x[i][j] = temp;
        }
    }
    cout << "The elements of your matirix are " << endl;
    cout << "{ ";

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            cout << x[i][j] << ", ";
        }
    }
    cout << "}" << endl;
}

bool MatrixMultiplicationCheck(int columns1, int rows2)
{
    assert(columns1 != rows2);
    {
        cout << "Matrices cannot be mutiplied" << endl;
        return false;
    }

    return true;
}

void Multiply(double* x[], double* y[], double* z[], int numRows1, int numCols1, int numRows2, int numCols2)
{
    for (int i = 0; i < numCols1; i++)
    {
        for (int j = 0; j < numCols1; j++)
        {
            z[i][j] = 0;
            for (int k = 0; k < numCols1; k++)
            {
                z[i][j] += x[i][k] * y[k][j];
            }

        }
    }
}

int main()
{

    int i,j,k,l = 0;

    cout << "How many rows in your first matrix?" << endl;
    cin >> i;
    cout << "How many columns in your first matrix?" << endl;
    cin >> j;

    cout << "How many rows in your 2nd matrix?" << endl;
    cin >> k;
    cout << "How many columns in your 2nd matrix?" << endl;
    cin >> l;

    cout << "Matrix A has rows = " << i << " and columns = " << j << endl;
    cout << "Matrix B has rows = " << k << " and columns = " << l << endl;

    MatrixMultiplicationCheck(j, k);

    double** A;
    double** B;
    double** C;

    A = MakeMatrix(i, j);
    B = MakeMatrix(k, l);
    C = MakeMatrix(j, k);

    Multiply(*A, *B, *C, i, j, k, l);

    return 0;
}
}
*A turns a ** to a * … its a dereference. int *a; … *a = 3; //sets the value, not the pointer, and is identical to a[0] = 3;
just call it with A, B and C without *s.
Last edited on
Multiply(A, B, C, i, j, k, l);

You will need to remove the extraneous curly brace at the end, too.
Hello,

Eventually I went back to square 1. Here's my solution

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <cassert>

using namespace std;

void DisplayMatrix(double**, int, int);
void EnterElements(double**, int, int);
void Multiply(double**, double**, double**, int, int, int, int);

void DisplayMatrix(double** matrix, int nrows, int ncols)
{
    cout << "The elements of your matrix are" << endl;
    cout << "{";
    for (int i = 0; i < nrows; i++)
    {
        for (int j = 0; j < ncols; j++)
        {
            cout << matrix[i][j] << ", ";
        }
    }
    cout << "}" << endl;
}

void EnterElements(double** matrix, int nrows, int ncols)
{
    cout << "Enter elements for your matrix" << endl;
    for (int i = 0; i < nrows; i++)
    {
        for (int j = 0; j < ncols; j++)
        {
            cin >> matrix[i][j];
            cout << endl;
        }
    }
}

void Multiply(double** matrix1, double** matrix2, double** matrix3, int nrows1, int ncols1, int nrows2, int ncols2)
{
    for (int i = 0; i < nrows1; i++)
    {
        for (int j = 0; j < ncols2; j++)
        {
            matrix3[i][j] = 0.0;
        }
    }

    for (int i = 0; i < nrows1; ++i)
    {
        for (int j = 0; j < ncols2; ++j)
        {
            for (int k = 0; k < ncols1; ++k)
            {
                matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
}

int main()
{
    int numRows1,numCols1, numRows2, numCols2;

    cout << "How many rows in your first matrix?" << endl;
    cin >> numRows1;
    cout << "How many columns in your first matrix?" << endl;
    cin >> numCols1;

    cout << "How many rows in your 2nd matrix?" << endl;
    cin >> numRows2;
    cout << "How many columns in your 2nd matrix?" << endl;
    cin >> numCols2;

    cout << "Matrix A has rows = " << numRows1 << " and columns = " << numCols1 << endl;
    cout << "Matrix B has rows = " << numRows2 << " and columns = " << numCols2 << endl;

    double** A = new double* [numRows1];

    for (int i = 0; i < numRows1; i++) {
        A[i] = new double[numCols1];
    }

    double** B = new double* [numRows2];

    for (int i = 0; i < numRows2; i++) {
        B[i] = new double[numCols2];
    }

    assert(numCols1 == numRows2);
    {
        cout << "The number of columns in the 1st matrix don't match rows in the 2nd." << endl;
    }

    double** C = new double* [numRows2];

    for (int i = 0; i < numRows2; i++) {
        C[i] = new double[numCols1];
    }

    EnterElements(A, numRows1, numCols1);
    EnterElements(B, numRows2, numCols2);

    DisplayMatrix(A, numRows1, numCols1);
    DisplayMatrix(B, numRows2, numCols2);

    Multiply(A, B, C, numRows1, numCols1, numRows2, numCols2);

    DisplayMatrix(C, numCols1, numRows2);

    for (int i = 0; i < numRows1; ++i)
    {
        delete[] A[i];
    }
    delete[] A;

    for (int i = 0; i < numRows2; ++i)
    {
        delete[] B[i];
    }
    delete[] B;

    for (int i = 0; i < numRows2; ++i)
    {
        delete[] C[i];
    }
    delete[] C;

    return 0;
}
Hi,

Following your feedback, I went back to the old code and did this. Thanks!

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cassert>

using namespace std;

void Multiply(double* [], double* [], double* [], int, int, int, int);
void DisplayMatrix(double**, int, int);
double** MakeMatrix(int, int);
bool MatrixMultiplicationCheck(int, int);
void DataEntryMatrix(double**, int, int);
void FreeMatrixMemory(int numRows, double** matrix);

void DisplayMatrix(double** matrix, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << matrix[i][j] << ", ";
        }
    }
}

double** MakeMatrix(int numRows, int numCols)
{
    double** matrix;
    matrix = new double* [numRows];
    for (int i = 0; i < numRows; i++)
    {
        matrix[i] = new double[numCols];
    }
    return matrix;
}

void FreeMatrixMemory(int numRows, double** matrix)
{
    for (int i = 0; i < numRows; i++)
    {
        delete[] matrix[i];
    }
    delete[] matrix;
}

void DataEntryMatrix(double** x, int numRows, int numCols)
{
    double temp;

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            cout << "What is number you would like to enter?" << endl;
            cin >> temp;
            x[i][j] = temp;
        }
    }
    cout << "The elements of your matirix are " << endl;
    cout << "{ ";

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            cout << x[i][j] << ", ";
        }
    }
    cout << "}" << endl;
}

bool MatrixMultiplicationCheck(int columns1, int rows2)
{
    assert(columns1 != rows2);
    {
        cout << "Matrices cannot be mutiplied" << endl;
        return false;
    }

    return true;
}

void Multiply(double* x[], double* y[], double* z[], int numRows1, int numCols1, int numRows2, int numCols2)
{
    for (int i = 0; i < numCols1; i++)
    {
        for (int j = 0; j < numCols1; j++)
        {
            z[i][j] = 0;
            for (int k = 0; k < numCols1; k++)
            {
                z[i][j] += x[i][k] * y[k][j];
            }

        }
    }
}

int main()
{

    int i, j, k, l = 0;

    cout << "How many rows in your first matrix?" << endl;
    cin >> i;
    cout << "How many columns in your first matrix?" << endl;
    cin >> j;

    cout << "How many rows in your 2nd matrix?" << endl;
    cin >> k;
    cout << "How many columns in your 2nd matrix?" << endl;
    cin >> l;

    cout << "Matrix A has rows = " << i << " and columns = " << j << endl;
    cout << "Matrix B has rows = " << k << " and columns = " << l << endl;

    //MatrixMultiplicationCheck(j, k);

    double** A;
    double** B;
    double** C;

    A = MakeMatrix(i, j);
    B = MakeMatrix(k, l);
    C = MakeMatrix(j, k);

    DataEntryMatrix(A, i, j);
    DataEntryMatrix(B, k, l);

    Multiply(A, B, C, i, j, k, l);
    DisplayMatrix(C, j, k);

    FreeMatrixMemory(i, A);
    FreeMatrixMemory(l, B);
    FreeMatrixMemory(j, C);

    return 0;
}
Topic archived. No new replies allowed.