乘以两个3x3矩阵的函数

时间:2017-01-31 06:14:08

标签: c++ matrix

我将3个矩阵传递给a函数。用户输入了矩阵bcabc的乘积。将值分配给const int N = 3; void multiplyMatrix(const double a[][N], const double b[][N], const double c[][N]) { for (int i = 0; i < N; i++) { for (int k = 0; k < N; k++) { c[i][k] = a[i][0] * b[0][k] + a[i][1] * b[1][k] + a[i][2] * b[2][k]; } } } int main() { cout << "Enter matrix1: "; double m1[3][N]; for (int i = 0; i < N; i++) { for (int k = 0; k < N; k++) { cin >> m1[i][k]; } } cout << "Enter matrix2: "; double m2[3][N]; for (int i = 0; i < N; i++) { for (int k = 0; k < N; k++) { cin >> m2[i][k]; } } //have to initiliaze matrix to some value for math to work properly!!! double m3[3][N] = {}; multiplyMatrix(m1, m2, m3); return 0; } 时,我收到错误。

  

表达式必须是可修改的Ivalue。

有人可以向我解释一下吗?

{{1}}

1 个答案:

答案 0 :(得分:0)

我将3个矩阵传递给multiplyMatrix函数。用户输入了矩阵abcab的乘积。将值分配给c时,我收到错误。

  

表达式必须是可修改的Ivalue。

有人可以向我解释一下吗?

const int N = 3;
void multiplyMatrix(double a[3][3], double b[3][3],double c[3][3])
{

    for (int i = 0; i < N; i++) {
        for (int k = 0; k < N; k++) {
            c[i][k] = a[i][0] * b[0][k] + a[i][1] * b[1][k] + a[i][2] * b[2][k];
        }
    }
}

int main()
{

    cout << "Enter matrix1: ";
    double m1[3][N];
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < N; k++) {
            cin >> m1[i][k];
        }
    }

    cout << "Enter matrix2: ";
    double m2[3][N];
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < N; k++) {
            cin >> m2[i][k];
        }
    }

    //have to initiliaze matrix to some value for math to work properly!!!
    double m3[3][N] = {};

    multiplyMatrix(m1, m2, m3);

    return 0;
}

只需删除const就可以了。