No of visitors who read this post: 763
Category: C++
Type: Question
Author: Asquare25
Your rating: None Average: 5 (1 vote)

I need to build up a code where I can find the DETERMINANT of (nxn) matrix in C++, please assume the given matrix and also show the working step-by-step so that I can understand how it is done ... plus The program asks the user which method to follow:
|1 0 0 9 8|
|2 1 0 7 6|
|3 2 1 5 3|
|0 0 0 7 4|
|0 0 0 5 3|
METHODS: Properties of DETERMINANTS.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# Comment Id: 54565

Hi Asquare25,
 
Take a look at this sample code:
 
// prob_4.cpp : Defines the entry point for the console application.
//
 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
typedef vector<double>Vect;
typedef vector<Vect>Matrix;
 
unsigned int fact(unsigned int v_mat);
 
int main()
{
 
unsigned int i;
int counter = 0;
unsigned int i_row_col_col = 0;
cout << " Enter number of v_row_cols or columns :\n";
cin >> i_row_col_col;
 
cout << " Enter the square Matrix :\n";
Vect v_row_col(i_row_col_col);
Vect v_determinant;
Matrix v_mat;
double product = 1;
int determ = 0;
int ve1 = 0;
 
for(i = 0; i < i_row_col_col; i++)
{
for(unsigned int j = 0; j < i_row_col_col; j++)
{
if(j<(i_row_col_col-1))
cout << " enter element\n";
else cout << " enter last element of this v_row_col\n";
cin >> v_row_col[j];
}
v_mat.push_back(v_row_col);
}
 
vector<int>vec;
for(i = 1; i <= i_row_col_col; i++)
vec.push_back(i);
vector<int>::iterator iter1 = vec.begin();
vector<int>::iterator iter2 = vec.end();
 
 
for(unsigned int i = 0; i < fact(i_row_col_col); i++)
{
 vector<int>vec1(i_row_col_col);
 copy(vec.begin(), vec.end(), vec1.begin());
 for(unsigned int j = 0; j < (i_row_col_col); j++)
 for(unsigned int i = 0; i < vec1.size(); i++)
 {
 
 for(unsigned int j = i; j < vec1.size(); j++)
 {
 if(vec1[j]<vec1[i])
 counter++;
 }
 }
 
 for(unsigned int w = 0; w < i_row_col_col; w++)
 {
 ve1 = (vec1[w])-1;
 product *= (v_mat[w][ve1]);
 }
 if(counter%2!=0)
 product = (- product);
 
 v_determinant.push_back(product); 
 
          counter = 0;
 product = 1;
 next_permutation(iter1, iter2);
}
 
for(unsigned int x = 0; x < v_determinant.size(); x++)
{
determ += (int)v_determinant[x];
}
 
for(i = 0; i < i_row_col_col; i++)//print the Matrix
{
cout << endl;
for(unsigned int j = 0; j < i_row_col_col; j++)
{
cout << v_mat[i][j] << " ";
}
}
cout << endl;
cout << " The determinant is :";
cout << determ << endl;
 
  system("pause");
    return 0;
}
// get factorial of v_mat
unsigned int fact(unsigned int i_element)
{
if(i_element<=i_element)
{
return i_element;
}
return (i_element * fact(i_element - 1));
}
 

# Comment Id: 65537

I had been looking for a method to find determinant in C++. Thankfully, I found a solution here! I tried the program given by the expert and did run it. It was 100% right. So then I studied the program and now I know how to write a program for finding determinant. Thank you so much.

Lee