简体   繁体   English

用户在 C++ 中输入 3x3 矩阵

[英]User inputs 3x3 Matrix in C++

I have the following code我有以下代码

#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;
}

When the user enters in the elements of the matrix it simply goes to the next line etc...当用户输入矩阵的元素时,它只会转到下一行等......

How can I make it so that when the user inputs the code, it shows the actual matrix and not just a list of 9 elements.我怎样才能做到这样,当用户输入代码时,它会显示实际的矩阵,而不仅仅是 9 个元素的列表。

It depends on the users.这取决于用户。 Users can enter the elements of 3x3 matrix with spaces and a new line (after 3 elements):用户可以输入带有空格和新行的 3x3 矩阵的元素(在 3 个元素之后):

3x3 矩阵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM