Question

In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

In this assignment, you are asked to:

1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure:

Private member variables:

- int ** mat;

- int rows;

- int cols;

Public member functions:

+Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically

+Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions.

+Destructor: deallocates the dynamically-allocated array. Make sure to delete both dimensions.

+AcceptValues: fill the array with values from the user.

+PrintValues: prints the matrix elements.

+int ** GetMatrix (): returns mat

+void AddMatrix (Matrix & b ): Accepts a Matrix object b by REFERENCE and adds it to the current Matrix. The two matrices are assumed to have the same dimensions. When adding two matrices, every two elements that are at the same position are added together. The following is an example of how this function is called:

Matrix a (2,3);

Matrix b(2,3);

a.AcceptValues();

b.AcceptValues();

a.AddMatrix (b); // the result is stored in a.

a.PrintValues(); // a now has the some of both matrices

2. Create a main where you implement the above example usage of the class.

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

Following is the code implementation:

Output is as follows:

Following are the lines of code in the program:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

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

public: Matrix() //this is the default constructor which initializes the value of rows and //column to be 3 by default
{
rows=3;
cols=3;
mat=new int*[rows];
for(int i=0;i<rows;i++)
{
mat[i]=new int[cols];
}
}

Matrix(int r,int c) //this is the parameterized constructor that accepts accepts parameter
{
rows=r;
cols=c;
mat=new int*[rows];
for(int i=0;i<rows;i++)
{
mat[i]=new int[cols];
}
}

~Matrix() //this destructor runs at the end for each object and deallocates memory
{
for(int i=0;i<rows;i++)
{
delete [] mat[i];
}
delete [] mat;
}

void AcceptValues()
{
cout<<"Enter the value for the matrix \n";
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cin>>mat[i][j];
}
}
}

void PrintValues()
{
cout<<"The matrix is as follows \n";
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<"\n";
}
}

int **GetMatrix()
{
return mat;
}

void AddMatrix(Matrix &b) //here the object b is passed by reference and in order to refer to the //matrix "a" we use "this" operator
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
this->mat[i][j]=this->mat[i][j]+b.mat[i][j];
}
}
}
};

int main()
{
Matrix a(2,3);
Matrix b(2,3);
a.AcceptValues();
b.AcceptValues();
a.AddMatrix(b);
a.PrintValues();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In this assignment, you are asked to: 1. create a Matrix class that stores and operate...
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
  • 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...

  • Create a simple Matrix class with appropriate constructors/destructor. The constructor should dynamically allocate memory for the...

    Create a simple Matrix class with appropriate constructors/destructor. The constructor should dynamically allocate memory for the array based on the dimensions provided. this is in C++

  • NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a...

    NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...

  • #ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int...

    #ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int );    //TODO: Add implementation here    //TODO: Implement parenthesis operator complex& operator()(int, int) { return m_vals[rows][cols]; } friend std::ostream& operator<<(std::ostream& os, gtmath::matrix& m);    int get_num_cols(); int get_num_rows();          private: complex** m_vals; int rows; int cols; }; } #include "matrix.h" #include <limits> namespace gtmath { //constructor & inlitilize dynamic array matrix::matrix(int rows, int cols) { this->cols = cols; this->rows = rows;...

  • i created a two dimensional matrix class which holds my object complex number. I am trying...

    i created a two dimensional matrix class which holds my object complex number. I am trying to overload the parenthesis () so that I can access and modify the value at specified location of the matrix. say matrix[a][b] = 5; and my_vals is a private member of the matrix class. What is the problem with my code //constructor & inlitilize dynamic array matrix::matrix(int rows, int cols) { complex** m_vals = new complex * [rows]; for (int i = 0; i...

  • 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...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...

  • Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a...

    Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a 2D array): (2) int columns; (3) int rows Public: (1) A default constructor that creates a 2D array, 8 6 4 (2) A constructor that takes three values for the three private members, and creates a 2D array accordingly. (3) The "get" and "set" methods for the three private members. (4) A method that displays a 2D array, stored in the private member matrix,...

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