Here is a program that finds the basis and dimension of a matrix using C++.

A program to find the basis and dimension of a matrix in C++

In this program, we will take the number of vectors as input from the user, then input the values into the vector and calculate the determinant of the matrix to find the basis and dimension.

Source code

Here is a program to find the basis and dimensions of a matrix using C++. The program source code is successfully compiled, and output is also given below.

#include<math.h>
#include<iostream>
using namespace std;

int det = 0;
int determinent(int n, int matrix[20][20]);

int main()
{
	int Nvector;
    cout << "Enter Number of Vector: ";
    cin >> Nvector;
    
    int matrix[20][20];
    
    cout << "\nEnter the vectors: ";
    for (int i = 0; i < Nvector; i++)
    {
        for (int j = 0; j < Nvector; j++)
        {
            cin >> matrix[j][i];
        }
    }
    
    det = determinent(Nvector, matrix);
    if (det != 0)
        cout << "\nThe vectors form the basis of R" << Nvector << " as the determinant is non-zero";
    else
        cout << "\nThe vectors doesn't form the basis of R" << Nvector << " as the determinant is zero";
 
 return 0;
}

int determinent(int vector, int matrix[20][20])
{
    int submatrix[20][20];
    if (vector == 2)
        return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
    else
    {
        for (int k = 0; k < vector; k++)
        {
            int subIvalue = 0;
            for (int i = 1; i < vector; i++)
            {
                int subJvalue = 0;
                for (int j = 0; j < vector; j++)
                {
                    if (j == k)
                        continue;
                    submatrix[subIvalue][subJvalue] = matrix[i][j];
                    subJvalue++;
                }
                subIvalue++;
 
            }
            det = det + (pow(-1, k) * matrix[0][k] * determinent(vector - 1, submatrix));
        }
    }
    return det;
}

Output

Enter Number of Vector: 4

Enter the vectors:
5 7 5 4
3 4 5 6
7 4 3 5
6 7 8 7

The vectors form the basis of R4 as the determinant is non-zero

Program Explanation

We Include “math.h” header file to use the pow() function in the program. All the mathematical libraries are predefined in “math.h” header file. Take one variable as a local variable to use it in both primary and user-defined function.

Input the value of the vector from the user, keep in mind that the value should be positive. Use a “for loop” to input the vector elements. Declare and define a function to calculate the determinant. In the determinant function, an “if-else” statement is used; if the vector is two then the “if” part will be executed and determine will be calculated manually by using determent formula.

Otherwise, the “else” part will execute. Inside else part, nested “for loop” is used to calculate the value of determinant, and the function returns the determinant value. If determent value is zero, then the vector doesn’t form the basis. Otherwise, the vector forms the basis.

Related C++ Articles

Last modified: July 12, 2019

Comments

Write a Reply or Comment

Your email address will not be published.