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

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

Example:

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

Product of 0 column is 108
Product of 1 column is 42
Product of 2 column is 80

C++ code to find the product of each column of matrix 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 matrices
  void getMatrix() {
    cout << "Enter Matrix elements :" << endl;

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

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

    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 multiply each 
    // columns elements of the matrix
    for (row = 0; row < 3; row++) {
      productcolumn = 1;

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

      cout << "Product of " << row << " column is " << productcolumn << endl;
    }
  }
};

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

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

  // calling productColumn() function to 
  // multiply each column
  M.productColumn();

  return 0;
}

Output:

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

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

Product of 0 column is 28
Product of 1 column is 80
Product of 2 column is 162

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 productColumn() to store the matrix elements and to find product of each column 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 productColumn() member function to find product of each column of matrix. The productColumn() function contains the logic to find product of each column of 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.