用户在C ++中输入3x3 Matrix

时间:2017-10-07 14:49:35

标签: c++ arrays calculator matrix-multiplication

我有以下代码

#include <iostream>

using namespace std;

int main()
{
    double v[3];
    double M[3][3];
    int i,j;

    cout << "Enter in the components of vector v:\n";

    for(i=0; i<3; i++)
    {
        cout << "Component " << i+1 << ": ";
        cin >> v[i];
    }

    cout << "Enter in the elements of matrix M:\n";

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            cin >> M[i][j];
        }
    }

    double Mv[3];

    Mv[0] = (M[0][0] * v[0]) + (M[0][1] * v[1]) + (M[0][2] * v[2]);
    Mv[1] = (M[1][0] * v[0]) + (M[1][1] * v[1]) + (M[1][2] * v[2]);
    Mv[2] = (M[2][0] * v[0]) + (M[2][1] * v[1]) + (M[2][2] * v[2]);

    cout << "The product of Mv is: " << Mv[3] << endl;
    return 0;
}

当用户输入矩阵的元素时,它只是转到下一行等...

如何使用当用户输入代码时,它显示实际的矩阵,而不仅仅是9个元素的列表。

1 个答案:

答案 0 :(得分:1)

这取决于用户。用户可以输入带有空格和新行的3x3矩阵元素(在3个元素之后):

3x3 matrix