mardi 24 mai 2016

Addition of two matrix using struct

Im trying to sum two matrix using struct, but it doesnt work.

If this code can be optimized please tell me :D

Compile with:

g++ -O2 -Wall program.cpp -o program

Output:

Proy3.cpp:5:16: error: variable ‘Matrix M1’ has initializer but incomplete type struct Matrix M1 { ^ Proy3.cpp:7:3: error: expected primary-expression before ‘int’ int row, column; ^ Proy3.cpp:7:3: error: expected ‘}’ before ‘int’ Proy3.cpp:7:3: error: expected ‘,’ or ‘;’ before ‘int’ Proy3.cpp:9:2: error: expected declaration before ‘}’ token };

Code:

# include < cstdio >

# include < iostream >

struct Matrix M1 {

    int row, column;
            int x[20][20];
};

struct M2 {

    struct Matrix M1;
        int y[20][20];

};

using namespace std;

int main() {

cout << "Insert size rows: Mat[a]";
cin >> &M1.row);

cout << "Insert size of columns Mat[a]";
cin >> &M1.column;


cout << "Insert size of rows Mat[b]";
cin >> &M2.M1.row;

cout << "Insert size of columns Mat[b]";
cin >> &M2.M1.column;

int i, j;

   // Matrix x

    for(i = 0; i <= M1.row; i++)
{
        for(j = 0; j <= M1.column; j++)
        {
        cout << "Insert number for matrix X : \n";
        cin >> &M1.x[i][j]
        }
}

       // Matrix y

    for(i = 0; i <= M2.M1.row; i++)
{
        for(j = 0; j <= M2.M1.column; j++)
        {
        cout << "Insert number for matrix Y : \n";
        cin << &M2.M1.y[i][j];
        }
}

// Matrix X + Matrix Y

for(i = 0; i <= M1.row; i++)
{
    for(j = 0; j < M1.column; j++)
    {
        cout <<"The sum of " << M1.x[i][j] << " + " <<  M2.M1.y[i][j] << " = " << M1.x[i][j] +  M2.M1.y[i][j] << endl;
    }
}
return 0;

}

Aucun commentaire:

Enregistrer un commentaire