c++ - Addition of two matrix using struct -


i'm trying sum 2 matrix using struct, doesn't work.

if code can optimized please tell me :d

compile with:

g++ -o2 -wall program.cpp -o program 

output:

in file included /usr/include/c++/4.8/iostream:39:0, proy3.cpp:2: /usr/include/c++/4.8/ostream:548:5: note: template std::basic_ostream& std::operator<<(std::basic_ostream&, const unsigned char*) operator<<(basic_ostream& __out, const unsigned char* __s) ^ /usr/include/c++/4.8/ostream:548:5: note: template argument deduction/substitution failed: proy3.cpp:51:30: note: ‘std::istream {aka std::basic_istream}’ not derived ‘std::basic_ostream’ cin << &m2.x[i][j];

code:

# include < cstdio > # include < iostream >   typedef struct matrix {     int row, column;             int x[20][20]; };  matrix m1,m2; 

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.row;  cout << "insert size of columns mat[b]"; cin >> m2.column;  int i, j;     // matrix x      for(i = 0; <= m1.row; i++) {         for(j = 0; j <= m1.column; j++)         {         cout << "insert number matrix x : \n";         cin >> m1.x[i][j]         } }         // matrix y      for(i = 0; <= m2.row; i++) {         for(j = 0; j <= m2.column; j++)         {         cout << "insert number matrix y : \n";         cin << m2.x[i][j];         } }  // matrix x + matrix y  for(i = 0; <= m1.row; i++) {     for(j = 0; j < m1.column; j++)     {         cout <<"the sum of " << m1.x[i][j] << " + " <<  m2.x[i][j] << " = " << m1.x[i][j] +  m2.x[i][j] << endl;     } } return 0; 

}

  for(i = 0; <= m2.m1.row; i++)  {     for(j = 0; j <= m2.m1.column; j++)     {     cout << "insert number matrix y : \n";     cin << &m2.m1.y[i][j];        }   } 

there no element m2.m1.y trying access. why declaring m1 inside m2.you have 1 structure , have 2 instances of it.something like

struct matrix {     int row,column;     int x[20][20]; }; struct matrix m1,m2; 

now can input 2 matrices.

also have use cin>>a instead of cin>>&a.

also cin << &m2.x[i][j]; should be

 cin >> m2.x[i][j];     ^^^^ 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -