Asked By
asquare25
50 points
N/A
Posted on - 03/20/2012
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.
Finding the DETERMINANT in C++
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 elementn";
else cout << " enter last element of this v_row_coln";
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));
}
Â
Answered By
zack lee
10 points
N/A
#127246
Finding the DETERMINANT in C++
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