简体   繁体   中英

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.

It depends on the users. Users can enter the elements of 3x3 matrix with spaces and a new line (after 3 elements):

3x3 矩阵

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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