C++ program to find the product of each row of matrix using class

Given a matrix, we have to find the product of each row of the matrix using the class and object approach.
Submitted by Shubh Pachori, on October 03, 2022

Example:

Input: 
Enter Matrix elements :
[0][0]:9
[0][1]:7
[0][2]:5
[1][0]:3
[1][1]:1
[1][2]:8
[2][0]:6
[2][1]:4
[2][2]:2
Output: 
Entered Matrix :
9  7  5  
3  1  8  
6  4  2  

Product of 0 row is 315
Product of 1 row is 24
Product of 2 row is 48

C++ code to find the product of each row of matrix using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Matrix {
  // private data member
 private:
  int matrix[3][3];

  // public member funtions
 public:
  // getMatrix() funtion to insert matrix
  void getMatrix() {
    cout << "Enter Matrix elements :" << endl;

    for (int row = 0; row < 3; row++) {
      for (int column = 0; column < 3; column++) {
        cout << "[" << row << "][" << column << "]: ";
        cin >> matrix[row][column];
      }
    }
  }

  // productRow() funtion to multiply 
  // elements of each row
  void productRow() {
    // initialising int type variables
    // to perform operations
    int row, column, productrow;

    cout << "\nEntered Matrix :" << endl;

    // for loop to show the inserted matrix
    for (row = 0; row < 3; row++) {
      for (column = 0; column < 3; column++) {
        cout << matrix[row][column] << "  ";
      }

      cout << "\n";
    }

    cout << "\n";

    // nested for loop to add each rows elements of the matrix
    for (row = 0; row < 3; row++) {
      productrow = 1;

      for (column = 0; column < 3; column++) {
        productrow = productrow * matrix[row][column];
      }

      cout << "Product of " << row << " row is " << productrow << endl;
    }
  }
};

int main() {
  // create an object
  Matrix M;

  // calling getMatrix() function to insert Matrix
  M.getMatrix();

  // calling productRow() function to 
  // multiply element of each row
  M.productRow();

  return 0;
}

Output:

Enter Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 9


Entered Matrix :
1  2  3  
4  5  6  
7  8  9  

Product of 0 row is 6
Product of 1 row is 120
Product of 2 row is 504

Explanation:

In the above code, we have created a class Matrix, one int type 2d array data members matrix[3][3] to store the elements of the matrix, and public member functions getMatrix() and productRow() to store the matrix elements and to find the product of each row of matrix.

In the main() function, we are creating an object M of class Matrix, reading the inputted matrix by the user using getMatrix() function, and finally calling the productRow() member function to find the product of each row of the matrix. The productRow() function contains the logic to find the product of each row of the matrix and printing the result.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.