1

Please check the code below that why this code did not display output??? The program works fine when calling a constructor but when I call the function to display record, it did not display it. I want to complete this work without overloading of = and << operators.

#include <iostream>
#include <string.h>
using namespace std;

class Matrix{
    private:
        int noOfRows;
        int noOfColumns;
        int **data;
    public:
        Matrix(int noOfRows, int noOfColumns);
        void displayData();
        ~Matrix();
        Matrix (const Matrix &ref);
};

Matrix::Matrix(int noOfRows=0, int noOfColumns=0){
    data=new int*[noOfColumns];
    for(int i=0;i<noOfRows;i++)
        data[i]=new int[noOfColumns];
    int d;
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++){
            cout<<"Enter ...";cin>>d;
            data[r][c]=d;
        //  cout<<data[r][c]<<"\t"; 
        }
        cout<<endl;
    }
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++)
            cout<<data[r][c]<<"\t";
        cout<<endl;
    }
}

Matrix::Matrix (const Matrix &ref){
    this->data=new int*[ref.noOfColumns];
    for(int i=0;i<ref.noOfRows;i++)
        this->data[i]=new int[ref.noOfColumns];
    
    for(int r=0;r<ref.noOfRows;r++){
        for(int c=0;c<ref.noOfColumns;c++){
            this->data[r][c]=ref.data[r][c];
            cout<<this->data[r][c]<<"\t";
        }
        cout<<endl;
    }
}

void Matrix::displayData(){
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++)
            cout<<data[r][c]<<"\t";
        cout<<endl;
    }
}

Matrix::~Matrix(){
//  delete[] data;
}

main(){
    Matrix M(2,2);
    M.displayData();
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    Probably unrelated: `main` must always return `int`. – user4581301 Mar 24 '22 at 17:52
  • 1
    Side note: [Here's a really simple and fast matrix](https://stackoverflow.com/a/2076668/4581301) suitable for beginners. – user4581301 Mar 24 '22 at 17:54
  • 2
    Recommended reading: [The Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three). As currently written the matrix will behave badly if it is copied. – user4581301 Mar 24 '22 at 17:59
  • Note: Not only must you `delete[] data;` in the destructor, but you must also (and first) `delete[]` all of the arrays `data` points at. – user4581301 Mar 24 '22 at 18:00
  • 1
    Super short minimal 2D matrix class. https://stackoverflow.com/questions/36123452/statically-declared-2-d-array-c-as-data-member-of-a-class/36123944#36123944 – doug Mar 24 '22 at 18:01
  • Once you get the code working to your satisfaction and have knocked out all of the bugs, consider asking for a [Code Review](https://codereview.stackexchange.com/help/asking). – user4581301 Mar 24 '22 at 18:02
  • I comment replace `int **data;` with `std::vector> data`. Then rule of zero will work for free. – Marek R Mar 24 '22 at 18:07
  • Search the internet for "C++ FAQ Matrix". Always search the internet first. Search the FAQs also. – Thomas Matthews Mar 24 '22 at 20:03

1 Answers1

1

You didn't initialize the data members noOfRows and noOfColumns in the constructor, and you confused matters by giving the constructor parameters the same names.

The constructor defintion should begin with:

Matrix::Matrix(int inr=0, int inc=0): noOfRows(inr), noOfColumns(inc)

(I would use unsigned int instead of int, but whatever floats your boat.)

Also, notice that if you actually create a matrix with zero rows or columns, the program won't output anything. Maybe displayData should print something "empty matrix" if NoOfRows or noOfColums is 0.

And more fun, data=new int*[0]; is legal, but unusable. Either check for a size 0 and consequently do nothing, or throw an exception.

Spencer
  • 1,924
  • 15
  • 27