Count of matrices (of different orders) with given number of elements in C++

C++Server Side ProgrammingProgramming

We are given the total number of elements and the task is to calculate the total number of matrices with different orders that can be formed with the given data. A matrix has an order mxn where m are the number of rows and n are the number of columns.

Input − int numbers = 6

Output −Count of matrices of different orders that can be formed with the given number of elements are: 4

Explanation − we are given with the total number of elements that a matrix of any order can contain which is 6. So the possible matrix order with 6 elements are (1, 6), (2, 3), (3, 2) and (6, 1) which are 4 in number.

Input − int numbers = 40

Output − Count of matrices of different orders that can be formed with the given number of elements are: 8

Explanation − we are given with the total number of elements that a matrix of any order can contain which is 40. So the possible matrix order with 40 elements are (1, 40), (2, 20), (4, 10), (5, 8), (8, 5), (10, 4), (20, 2) and (40, 1) which are 8 in number.

Approach used in the below program is as follows

  • Input the total number of elements that can be used to form the different order of matrices.

  • Pass the data to the function for further calculation

  • Take a temporary variable count to store the count of matrices of with different order

  • Start loop FOR from i to 1 till the number

  • Inside the loop, check IF number % i = 0 then increment the count by 1

  • Return the count

  • Print the result

Example

 Live Demo

#include <iostream>
using namespace std;
//function to count matrices (of different orders) with given number of elements
int total_matrices(int number){
   int count = 0;
   for (int i = 1; i <= number; i++){
      if (number % i == 0){
         count++;
      }
   }
   return count;
}
int main(){
   int number = 6;
   cout<<"Count of matrices of different orders that can be formed with the given number of elements are: "<<total_matrices(number);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of matrices of different orders that can be formed with the given number of elements are: 4
raja
Published on 02-Nov-2020 06:12:11
Advertisements