Home > Back-end >  C Program with matrix class ending abruptly
C Program with matrix class ending abruptly

Time:06-05

I am writing a matrix class where I need to perform some matrix calculations in the main program. I am not sure why the program is ending abruptly when user chooses a matrix of size more than 2x2 matrix. The std::cin works fine until two rows but program ends after the loop reaches third row. Only part of the code is shown below which is related to my question.

#include<iostream>
#include <vector>
#include <cassert>
using std::vector;
using namespace std;

class Matrix {
private:
    int rows;
    int cols;
    int **vtr;

public:
    Matrix(int m = 2, int n = 2)
    {
        rows = m;
        cols = n;
        vtr = new int*[m];
        for (int i = 0; i < m; i  )
        {
                vtr[i] = new int [n];
        }

        for (int i = 0; i < m; i  )
        {
            for (int j = 0; j < n; j  )
            {
                vtr[i][j] = 0;
            }

        }
    }
void read()
    {
        cout << "Enter the number of rows and columns of Matrix separated by a space: ";
        cin >> rows >> cols;
        Matrix a(rows, cols);
        a.write();
        

        for (int i = 0; i < rows; i  )
        {
            for (int j = 0; j < cols; j  )
            {
                cout << "(" << i << "," << j << ") : ";
                cin >>vtr[i][j];
                
            }
        }
    }

    void write()
    {
        for (int i = 0; i < rows; i  )
        {
            for (int j = 0; j < cols; j  )
            {
                cout <<  vtr[i][j] <<  " ";
                
            }
            cout << endl;
        }
        cout << endl << endl;
    }
};

int main()
{

    Matrix A, B, C;
    int row, column ;

    cout << "For Matrix A" << endl;
    A.read();
    cout << "For Matrix B " << endl;
    B.read();
    cout << "For Matrix C" << endl;
    C.read();
}

CodePudding user response:

The three matrixs will be construct when you define Matrix A, B, C. So the matrix size is 2x2. When you call function cin to assign value to some position is not in 2x2 will not work.

CodePudding user response:

Since the 2D array, vtr is created when declaring the Matrix object, you can move the vtr creation after reading the console input like below.

Matrix(int m = 2, int n = 2)
{
    /*rows = m;
    cols = n;
    vtr = new int*[m];
    for (int i = 0; i < m; i  )
    {
        vtr[i] = new int [n];
    }

    for (int i = 0; i < m; i  )
    {
        for (int j = 0; j < n; j  )
        {
            vtr[i][j] = 0;
        }
    }*/
}

void read()
{
    cout << "Enter the number of rows and columns of Matrix separated by a space: ";
    cin >> rows >> cols;
        
    vtr = new int*[rows];
    for (int i = 0; i < rows; i  )
    {
        vtr[i] = new int [cols];
    }
        
    //Matrix a(rows, cols);
    //write();
        
    for (int i = 0; i < rows; i  )
    {
        for (int j = 0; j < cols; j  )
        {
            cout << "(" << i << "," << j << ") : ";
            cin >>vtr[i][j];
        }
    }
        
    write(); //Prints the array
}
  • Related