C++ Program to Find Basis and Dimension of a Matrix

This is a C++ Program to find the basis and dimension of the given matrix.

Here is source code of the C++ Program to Find Basis and Dimension of a Matrix. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<conio.h>
  2. #include<iostream>
  3. #include<math.h>
  4.  
  5. using namespace std;
  6. double d = 0;
  7. double det(int n, double mat[10][10]);
  8. double det(int n, double mat[10][10])
  9. {
  10.     double submat[10][10];
  11.     if (n == 2)
  12.         return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
  13.     else
  14.     {
  15.         for (int c = 0; c < n; c++)
  16.         {
  17.             int subi = 0; //submatrix's i value
  18.             for (int i = 1; i < n; i++)
  19.             {
  20.                 int subj = 0;
  21.                 for (int j = 0; j < n; j++)
  22.                 {
  23.                     if (j == c)
  24.                         continue;
  25.                     submat[subi][subj] = mat[i][j];
  26.                     subj++;
  27.                 }
  28.                 subi++;
  29.  
  30.             }
  31.             d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
  32.         }
  33.     }
  34.     return d;
  35. }
  36. int main(int argc, char **argv)
  37. {
  38.  
  39.     cout << "Enter the number of vectors:\n";
  40.     int n;
  41.     cin >> n;
  42.     double mat[10][10];
  43.     cout << "Enter the vectors one by one:\n";
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         for (int j = 0; j < n; j++)
  47.         {
  48.             cin >> mat[j][i];
  49.         }
  50.     }
  51.     d = det(n, mat);
  52.     if (d != 0)
  53.         cout << "The vectors forms the basis of R" << n
  54.                 << " as the determinant is non-zero";
  55.     else
  56.         cout << "The vectors doesn't form the basis of R" << n
  57.                 << " as the determinant is zero";
  58.  
  59. }

Output:

$ g++ BasisAndDimension.cpp
$ a.out
 
Enter the number of vectors:
3
Enter the vectors one by one:
1 2 3
2 3 4
3 4 5
The vectors doesn't form the basis of R3 as the determinant is zero
 
Enter the number of vectors:
4
Enter the vectors one by one:
2 3 5 8
1 6 2 9
3 4 2 7 
2 5 3 9
The vectors forms the basis of R4 as the determinant is non-zero

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.