Answer to Question #320122 in C++ for khan

Question #320122

You are required to create a class by the name of ‘Matrix’. The matrix private domain must

contain a 2-Dimentional array of floats. The constructor should initialize a passed value to all the locations of the array being created and if the argument is missing, it should assign 0 by default to the entire array. Define appropriate functions for inserting data into the matrix and displaying the array in a matrix form. You need to define a function by the name of ‘dot’ that would take a matrix object as an argument and would return the matrix object (temporary) to where it was called from. From where it was called, there it should assign the returned object to a new matrix class instance.

Matrix obj3=obj1.dot(obj2);

Call the display function from the new instance to see the result of two matrices multiplied.

NOTE: Define a function ’float retreive(int, int)’ where if two indices are

passed to it, it must return the value located at those locations.


1
Expert's answer
2022-03-29T07:41:30-0400
#include <iostream>
#include <iomanip>
using namespace std;


class Matrix {
public:
    Matrix(int row, int col, float val=0.0);
    ~Matrix();
    void set(int i, int j, float v);
    void display();
    Matrix dot(Matrix& M);
    float retrive(int i, int j);


private:
    int row;
    int col;
    float **data;
};


Matrix::Matrix(int r, int c, float v) {
    row = r;
    col = c;
    data = new float* [row];
    for (int i=0; i<row; i++) {
        data[i] = new float[col];
        for (int j=0; j<col; j++) {
            data[i][j] = v;
        }
    }
}


Matrix::~Matrix() {
    for (int i=0; i<row; i++) {
        delete [] data[i];
    }
    delete [] data;
}


void Matrix::set(int i, int j, float v) {
    data[i][j] = v;
}


void Matrix::display() {
    for (int i=0; i<row; i++) {
        for (int j=0; j<col; j++) {
            cout << setw(5) << data[i][j] << " ";
        }
        cout << endl;
    }
}


Matrix Matrix::dot(Matrix& M) {
    Matrix res(row, M.col);


    for (int i=0; i<row; i++) {
        for (int j=0; j<M.col; j++) {
            for (int k=0; k<col; k++) {
                res.data[i][j] += data[i][k] * M.data[k][j];
            }
        }
    }
    return res;
}


float Matrix::retrive(int i, int j) {
    return data[i][j];
}


int main() {
    Matrix obj1(2, 3);
    for (int j=0; j<3; j++) {
        obj1.set(0,j,j);
        obj1.set(1,j,j+1);
    }
    cout << "obj1:" << endl;
    obj1.display();
    cout << endl;

    Matrix obj2(3, 2, 1);
    for (int i=0; i<3; i++) {
        obj2.set(i, 1, 1-2*(i%2));
    }
    cout << "obj2:" << endl;
    obj2.display();
    cout << endl;

    Matrix obj3 = obj1.dot(obj2);
    cout << "obj3:" << endl;
    obj3.display();
    cout << endl;

    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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog