-2

I'm taking an OOP course at college, we are supposed to overload the +, -, *, +=, -= and some other operators to handle Matrices operations using only structs and operator overloading. I came up with that code, the user enters a 1D array, and then that array is turned into a 2D array or a matrix.

The function createMatrix will take a 1D-array and then define the number of Rows and Columns then put the values in that array into the matrix struct or in an element in the struct to be specific. then I defined +, - and <<.

It is not complete yet, but the program doesn't output anything and I can't find where the bug is at, is it in the '+' operator or the '<<' operator -which handles the output of the matrices.

#include <iostream>
#include <algorithm>
using namespace std;

struct matrix{
    int* data;          //since it is a 1d array which will later be stored in the matrix
    int row, col;
};

void createMatrix(int row, int col, int num[], matrix& mat);    //by reference so that I fetch it by address from memory


matrix operator+ (matrix mat1, matrix mat2){          //Addition
    matrix mat;
    for(int i = 0; i < max(mat1.row*mat1.col, mat2.row*mat2.col); i++){
        mat.data[i] = mat1.data[i] + mat2.data[i];

    }
    return mat;
}

matrix operator- (matrix mat1, matrix mat2){          //Subtraction
    matrix mat;
    for(int i = 0;i < max(mat1.row*mat1.col, mat2.row*mat2.col); i++){
        mat.row = mat1.row - mat2.row;

    }
    return mat;
}

 ostream operator<< (ostream& out, matrix mat){
     for(int i = 0; i < ((mat.row)*(mat.col)); i++){
         if(i % mat.row == 0 ){
             cout<<endl;
         }
         else{out<<mat.data[i]<<" ";}
     }
 }
 int main()
 {
     int row1, col1;
     int row2, col2;

     cout<<"Enter Rows of first matrix: "; cin>>row1;
     cout<<"Enter Cols of first matrix: "; cin>>col1;

     cout<<"Enter Rows of second matrix: "; cin>>row2;
     cout<<"Enter Cols of second matrix: "; cin>>col2;

     int arr1[row1*col1], arr2[row2*col2];
     cout<<"Enter the values you which to add in the first matrix: ";
     for(int i = 0; i < row1*col1; i++){
         cin>>arr1[i];
     }
     cout<<"Enter the values you which to add in the second matrix: ";
     for(int i = 0; i < row2*col2; i++){
         cin>>arr2[i];
     }

     matrix mat1, mat2, mat3;
     createMatrix(row1, col1, arr1, mat1);
     createMatrix(row2, col2, arr2,  mat2);
     mat3 = mat1 + mat2;
    cout<<mat3;

    return 0;
}

void createMatrix(int row, int col, int num[], matrix& mat){
     mat.row = row;
     mat.col = col;
     mat.data = new int [col * row];             //We are trying to make a matrix from a 1d array, so we will stretch the matrix -which is a 2d array
     for(int i = 0; i < col * row; i++){             //in 1-D of size row *col
         mat.data[i] = num[i];                    //Depending on the parameter the data array will be filled dynamically
     }
 }
3
  • May or may not be related (don't know what the bug you're looking for is): The compiler may be trying to warn you that operator<< is missing a return statement. Sep 27, 2018 at 20:36
  • 1
    int arr1[row1*col1], arr2[row2*col2]; defines Variable Length Arrays (VLA). VLAs are not Standard C++. Your compiler may or may not support this behaviour, and if it does, it is a blisteringly easy way to cause a stack overflow. Probably best if you use std::vector instead. Sep 27, 2018 at 20:37
  • 1
    createMatrix looks like the sort of thing that should be returning a matrix, not taking one as a parameter. To get that to work (and because you can't write worthwhile C++ code without doing this), familiarize yourself with the Rules of Three, Five, and Zero. Sep 27, 2018 at 20:40

2 Answers 2

2

Your operator+ will cause a segfault because you have not allocated mat.data using new. You need to do

matrix mat;
mat.data = new int [max(mat1.row*mat1.col, mat2.row*mat2.col)];

Anyway, your matrix addition logic won't work and will cause another segfault. I suggest looking at this page for guidance on how to tackle this. It talks about using two-dimensional matrices which should make the logic easier to follow rather than working with 1D array/vector.

So if you had two 2D arrays, a and b, you could add them like this:

for(i = 0; i < min(a_row, b_row); ++i)
    for(j = 0; j < min(b_col, a_col); ++j)
        sum[i][j] = a[i][j] + b[i][j];
2
  • Sidenote: rather than setting all of the parameters of matrix after the fact (or forgetting to do so), you should consider giving it a constructor to make this problem impossible. Sep 27, 2018 at 20:48
  • I did not know it was better to work with a 1D array so thanks. Sep 27, 2018 at 20:54
1

When you are overloading the operator<< who will return a std::ostream, you always have to return a reference to it. Example:

ostream& operator<< (ostream& out, matrix mat) {
    out << mat.col << mat.data << mat.row;
    return out;
}

Also, you can't just make an array whose size it not known at compile time yet. cin is an I/O operation. So the size is not known at compile time, but at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.