Program to read ‘n’ number and print them in matrix terms in all orders in C++

Written by

Garvit Gulati

# Understanding the problem

Order of a matrix containing ‘m’ rows and ‘n’ columns is

(mXn)

Also, the total number of elements of the matrix can be calculated by m*n.

Now, in the given  problem we will be given ‘n’ elements and we would have to display them in a matrix form of all possible orders.

# Approaching the problem

Since the total number of elements of a matrix is equal to the product of number of rows and columns, we can say, the total number of possible matrices will be equal to the number of factors of ‘n’.

To find each order, we will take one factor, say ‘i’, of ‘n’ at a time and set it equal to the number of rows, then number of columns can be found by dividing ‘n’ by ‘i’. In this way, we can get order of all possible matrices and we just have to print the given ‘n’ numbers according to the found order.

# Algorithm and Explanation

  1. Input ‘n’ from the user i.e. the total count of numbers to be entered.
  2. Initialise three arrays of size ‘n’:
    1. arr:      to store the input numbers
    2. row:     to store the number of rows of a matrix
    3. col: to store the number of columns of a matrix
  3. Initialise a variable ‘j’ to store the number of possible matrices and set it to zero.
  4. Run a for loop from i=1 till i<=n
  5. In the for-loop check if ‘i’ is a factor of n or not.
  6. If yes store it in row[j], find the corresponding number of columns i.e. n/I and store it in col[j] and increment j in order to store the order of next matrix.

[Now, we have the number of matrices j and for each j, row[j] represents the number of rows and col[j] gives the corresponding number of columns]

  1. Run a for-loop from 0 to j, this loop will print the j matrices found.
  2. Inside the loop use nested for-loops to print a 2-D array, where number of rows will be given by row[j] and columns by col[j] and element by print the elements of arr in sequence.

Code

#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter the count of numbers you wish to enter\n";  //inputting the numbers

    cin >> n;

    int row[n], col[n], arr[n], j = 0;

    cout << "Enter " << n << " numbers\n";

    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }

    for (int i = 1; i <= n; ++i)  //finding the factors of n
    {
        if (n % i == 0)
        {
            row[j] = i;  //setting the factor as number of rows
            col[j] = n / i;  //finding corresponding number of columns
            ++j;
        }
    }

    cout << "The numbers can be printed in " << j << " matrices\n";

    for (int i = 0; i <= j; ++i)  //printing the found matrices
    {
        int m = 0;
        for (int r = 0; r < row[i]; ++r)
        {
            for (int c = 0; c < col[i]; ++c)
            {
                cout << arr[m] << " ";
                ++m;
            }
            cout << "\n";
        }
        cout << "\n\n";
    }

    return 0;
}

Output

5
1 2 3 4 5

The numbers can be printed in 2 matrices

1 2 
3 4 
5 

1 
2 
3 
4 
5 

Program to read ‘n’ number and print them in matrix terms in all orders in C++