Knowee
Questions
Features
Study Tools

write a C++ program implements matrix multiplication using a Matrix class, featuring constructors, destructors, and static member functions. Users input matrix elements, and the program displays matrices and their product, while tracking the total number of matrices created.sample input and outputEnter the number of rows for the first matrix: 2Enter the number of columns for the first matrix: 2Enter the elements of the first matrix:2 34 5Enter the elements of the second matrix:1 23 4Product:11 1619 28

Question

write a C++ program implements matrix multiplication using a Matrix class, featuring constructors, destructors, and static member functions. Users input matrix elements, and the program displays matrices and their product, while tracking the total number of matrices created.sample input and outputEnter the number of rows for the first matrix: 2Enter the number of columns for the first matrix: 2Enter the elements of the first matrix:2 34 5Enter the elements of the second matrix:1 23 4Product:11 1619 28

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C++ program that implements matrix multiplication using a Matrix class. This program also tracks the total number of matrices created.

#include<iostream>
using namespace std;

class Matrix {
    static int count;
    int rows, cols;
    int **data;

public:
    Matrix(int r, int c) : rows(r), cols(c) {
        count++;
        data = new int*[rows];
        for(int i=0; i<rows; i++)
            data[i] = new int[cols];
    }

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

    void input() {
        for(int i=0; i<rows; i++)
            for(int j=0; j<cols; j++)
                cin >> data[i][j];
    }

    void display() {
        for(int i=0; i<rows; i++) {
            for(int j=0; j<cols; j++)
                cout << data[i][j] << " ";
            cout << endl;
        }
    }

    static int getCount() {
        return count;
    }

    Matrix multiply(Matrix &m) {
        if(cols != m.rows) {
            cout << "Multiplication not possible.";
            exit(0);
        }
        Matrix result(rows, m.cols);
        for(int i=0; i<rows; i++)
            for(int j=0; j<m.cols; j++) {
                result.data[i][j] = 0;
                for(int k=0; k<cols; k++)
                    result.data[i][j] += data[i][k] * m.data[k][j];
            }
        return result;
    }
};

int Matrix::count = 0;

int main() {
    int r, c;
    cout << "Enter the number of rows for the first matrix: ";
    cin >> r;
    cout << "Enter the number of columns for the first matrix: ";
    cin >> c;
    Matrix m1(r, c);
    cout << "Enter the elements of the first matrix:\n";
    m1.input();

    cout << "Enter the number of rows for the second matrix: ";
    cin >> r;
    cout << "Enter the number of columns for the second matrix: ";
    cin >> c;
    Matrix m2(r, c);
    cout << "Enter the elements of the second matrix:\n";
    m2.input();

    Matrix m3 = m1.multiply(m2);
    cout << "Product:\n";
    m3.display();

    cout << "Total matrices created: " << Matrix::getCount() << endl;

    return 0;
}

This program first asks for the number of rows and columns for the first matrix, then the elements of the first matrix. It does the same for the second matrix. It then multiplies the two matrices and displays the product. Finally, it displays the total number of matrices created.

This problem has been solved

Similar Questions

Problem StatementHarpreet is a student learning about matrix multiplication. She wants to write a program to multiply two matrices, a and b, each of size N x N. Can you help her with the code?Write a program that takes an integer N as input and two matrices a and b of size N x N. Implement matrix multiplication and print the resulting matrix c.Note: A square matrix is where the number of rows equals the number of columns.Input format :The first line of input consists of an integer N, representing the matrix size.The next N lines consist of N space-separated elements in each line, representing the first matrix.After being separated by a new line, the next N lines consist of N space-separated elements in each line, representing the second matrix.Output format :The output prints the product of two matrices.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ N ≤ 80 ≤ elements ≤ 9Sample test cases :Input 1 :32 3 23 2 33 3 34 5 62 3 11 2 3Output 1 :16 23 21 19 27 29 21 30 30 Input 2 :22 22 35 07 8Output 2 :24 16 31 24 Input 3 :81 5 2 4 7 3 2 12 3 4 5 6 7 8 94 5 6 1 2 3 7 82 5 8 7 4 1 9 61 4 7 8 5 2 6 99 8 6 4 7 5 1 27 5 3 9 5 1 4 83 4 8 9 7 1 2 05 8 7 4 6 1 2 09 7 4 5 6 3 2 17 5 6 8 4 1 2 31 4 5 2 3 9 8 72 5 8 7 4 3 6 94 5 8 7 1 0 0 11 5 6 7 8 4 2 23 2 1 4 3 5 3 6Output 3 :99 131 152 141 106 88 97 115 145 200 236 248 183 155 137 181 155 177 179 207 172 108 88 113 157 201 217 233 202 166 146 169 149 186 206 223 179 176 157 194 204 243 255 231 185 110 128 136 152 208 212 197 188 177 164 179 136 178 206 184 146 133 148 159

Write a program for matrix multiplication in java using threads.Input format :The first line consists of the row and column value of Matrix 1 separated by a single spaceThe second line consists of the row and column value of Matrix 2 separated by a single spaceThe next input Matrix 1 elements of the matrix.The next input Matrix 2 elements of the matrix.Output format :Display the matrix after multiplication.Refer to the sample input and output for format specifications.Code constraints :Integers only.Sample test cases :Input 1 :2 22 21 23 45 67 8Output 1 :19 22 43 50

Write C Program to Find the Sum of Each Row and Column of a 2x2 Matrix  -  15 marksSample InputEnter the co-efficients of the matrix23 4580 97Sample OutputSum of the 0 row is = 68Sum of the 1 row is = 177Sum of the 0 column is = 103Sum of the 1 column is = 142

Write a python program for multiplication of 2×3 and 3×2 matrix. Input should be taken from user. Put compiled Program and output snapshot here.

Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.