Answer to Question #180732 in C++ for pooja

Question #180732

Create a class called Matrix having the following data members int rows; int cols; int mat[][]. Write a program to implement the following operations (the ones mentioned in comments) on Matrix. The main() function of the above program is as below: int main() { Matrix m1(3,3); // it will initialize rows and cols m1.getdata(); //accept the data in matrix Matrix m2=m1; //copy one matrix contents into another m2.putdata(); // display the contents of matrix Matrix m3; m3.putdata(); }


1
Expert's answer
2021-04-12T15:10:10-0400
#include <iostream>
using namespace std;
class Matrix{
    int rows, cols, mat[10][10];
    public:
    Matrix(){
        rows = 0;
        cols = 0;
    }
    Matrix(int r, int c){
        rows = r;
        cols = c;
    }
    void getdata(){
        cout<<"Enter the matrix contents. \n";
        for(int i = 0; i < cols; i++)
            for(int j = 0; j < rows; j++)
                cin>>mat[i][j];
    }
    void putdata(){
        if(rows == 0 && cols == 0)
            cout<<endl<<"\nNo data present";
        else
            for(int i = 0; i < cols; i++){
                cout<<endl;
                for(int j = 0; j < rows; j++)
                    cout<<mat[i][j]<<"\t";
            }
    }
    Matrix operator=(const Matrix &a){
        this->rows = a.rows;
        this->cols = a.cols;
        for(int i = 0; i < cols; i++)
            for(int j = 0; j < rows; j++)
                this->mat[i][j] = a.mat[i][j];
    }
};
int main() { 
    Matrix m1(3,3); //it will initialize rows and cols 
    m1.getdata(); //accept the data in matrix 
    Matrix m2=m1; //copy one matrix contents into another 
    m2.putdata(); //display the contents of matrix 
    Matrix m3; 
    m3.putdata();
    return 0; 
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Aman Kumar
13.04.21, 10:19

There's some problem with this code, no matter what input I give it's always shows "No data present"

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS