Question

In C++

Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, ev

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution: In the solution if matrix are not in the form required for particular operation then it will return matrix with row = 0 and column = 0 as a result, and print a error message.

other solution may be that check condition before calling any method.

#include <bits/stdc++.h>
using namespace std;
//class MatrixType
class MatrixType
{
    //variables
    public:
    int rows;
    int columns;
    int **matrix;
    //constructor
    MatrixType(int row,int column )
    {
        //set rows and columns
        rows=row; 
        columns=column;
        //define and initialize array
        matrix=new int* [columns];
        
        for(int i =0;i<rows;i++)
        {
            matrix[i]=new int[columns];
        }
        for(int i=0;i<rows;i++)
            for(int j=0;j<columns;j++)
                matrix[i][j]=0;            
    }
    //overloding + operator
    MatrixType operator +(MatrixType other) 
    {
        /*if matrix two matrix have different rows or different columns then 
        return same matrix*/
        if (rows!=other.rows ||columns!=other.columns)
        {
            //create new object of MatrixType
            MatrixType temp(0,0);  
            cout<<"Matrix can't be added!\n";
            return temp;
        }
        //otherwise add 2 matrix
        else
        {   //create new object of MatrixType
            MatrixType temp(rows,columns);   
            for(int i=0;i<rows;i++)
                for(int j=0;j<columns;j++)
                    temp.matrix[i][j]+=other.matrix[i][j]+matrix[i][j];
        
            //return MatrixType object
            return temp;
        }
     
    }
    //overloding - operator
    MatrixType operator -(MatrixType other) 
    {
         
        /*if matrix two matrix have different rows or different columns then 
        return same matrix*/
        if (rows!=other.rows ||columns!=other.columns)
        {
            //create new object of MatrixType
            MatrixType temp(0,0);  
            cout<<"Matrix can't be subtracted!\n";
            return temp;
        }
        //otherwise subtract matrix
        else
        {
            //create MatrixType object
            MatrixType temp(rows,columns); 
            for(int i=0;i<rows;i++)
                for(int j=0;j<columns;j++)
                    temp.matrix[i][j]+=(matrix[i][j]-other.matrix[i][j]);
        
            //return MatrixType object
            return temp;
        }
     
    }

    //overloding * operator
    MatrixType operator *(MatrixType other)   
    {
        /*if columns of 1st matrix not equal to rows of 2nd matrix then return
        1st matrix because multiplication can't take place*/
        if (columns!=other.rows)
        {
            MatrixType temp(0,0);
            cout<<"Matrix can't be multiplied!\n";
            return temp;
        }
        //otherwise calculate multiplication
        else
        {
            //create object of MatrixType
            MatrixType temp(rows,other.columns);  
            for(int i=0;i<rows;i++)
                for(int j=0;j<other.columns;j++)
                    for(int k =0;k<columns;k++)
                        temp.matrix[i][j]+=(matrix[i][k]*other.matrix[k][j]);
            return temp;          
        }

    }
    //overloding << operator
    friend ostream& operator<<(ostream& output, MatrixType& m)
    {
        if(m.rows!=0&&m.columns!=0)
        for(int i=0;i<m.rows;i++)
        {   for(int j=0;j<m.columns;j++)
                output<<m.matrix[i][j]<<" ";
            cout<<"\n";
        } 
        return output;  
    }
    
};
//main() for testing
int main()
{
    //variables
    int row,column;
    //read row and column from user
    cout<<"Enter row and column of matrix: ";
    cin>>row>>column;
    //create object of MatrixType
    MatrixType m1(row,column);
    cout<<"Enter matrix\n";
    //read element of matrix
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<column;j++)
        {
            cin>>m1.matrix[i][j];
        }
    }
    
    //read row and column from user
    cout<<"Enter row and column of matrix 2: ";
    cin>>row>>column;
    //create object of MatrixType
    MatrixType m2(row,column);
    cout<<"Enter matrix\n";
    //read element of matrix
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<column;j++)
        {
            cin>>m2.matrix[i][j];
        }
    }
    
    //performing various operation
    MatrixType m3=m1+m2;
    MatrixType m4=m1-m2;
    MatrixType m5=m1*m2;
    
    //print matrix
    cout<<"\nMatrix 1\n"<<m1;
    cout<<"\nMatrix 2\n"<<m2;
    cout<<"\nMatrix 3\n"<<m3;
    cout<<"\nMatrix 4\n"<<m4;
    cout<<"\nMatrix 5\n"<<m5;
    

    return 0;
}

Output:

input Enter row and column of matrix: 22 Enter matrix 58 38 Enter row and column of matrix 2: 2 2 Enter matrix 38 89 Matrix 1Matrix 4 20 -5 -1 Matrix 5 79 112 73 96 ... Program finished with exit code 0 Press ENTER to exit console.

Thank You!

Add a comment
Know the answer?
Add Answer to:
In C++ Design a class to perform various matrix operations. A matrix is a set of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Implement a C++ class to model the mathematical operations of a matrix. Your class should include...

    Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which adds two matrices: power() which raises the first matrix to power n: "" which returns true if both matrices are equal. You need to overload the C++ equality operator. A sample run follows. Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of matrix 1 row by row: 1 5 0 1 3...

  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...

    Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods described: 1. read: reads in a matrix from the command line 2. display: prints the matrix to the command line 3. getRows: returns the number of rows 4. getCols: returns the number of columns 5. set: sets the double value at a particular column/row position 6. get: returns the value stored at a particular column/row position 7. Plus: returns a new Matrix object that...

  • IT a) If one row in an echelon form for an augmented matrix is [o 0 5 o 0 b) A vector bis a linear combination of the columns of a matrix A if and only if the c) The solution set of Ai-b is the set o...

    IT a) If one row in an echelon form for an augmented matrix is [o 0 5 o 0 b) A vector bis a linear combination of the columns of a matrix A if and only if the c) The solution set of Ai-b is the set of all vectors of the formu +vh d) The columns of a matrix A are linearly independent if the equation A 0has If A and Bare invertible nxn matrices then A- B-'is the...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • Q1. Using your class notes write 1-2 paragraphs about your learning in first four (three hours cl...

    please explain the following in detail.. Q1. Using your class notes write 1-2 paragraphs about your learning in first four (three hours class counts as 2 classes) classes of this course. Explain how your learning can be related to your previous knowledge? Also how you expect to use it in future (maybe in relevance to any project that you want to do in future)? This is just to make you realize the relevance of your learning to practical applications We...

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • 47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all...

    47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all the rows of column n of the matrix A. b. Refers to the elements in all the rows between columns m and n of the matrix A. c. Refers to the elements in all the columns between rows m and n of the matrix A. d. Refers to the elements in all the columns of row n of the matrix A. 48. What does...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT