determinante en una matriz
hola, necesito un programa que me calcule el deternimante de una matriz si es posible que sea nxn aunque creo que no se puede por la memoria pero si la tienen mas pequeña me la podrian mandar.
Te envio un programa que calcula el determinante de una matriz N*N, en forma recursiva, para N = 4, valor que puedes adaptar a tus necesidades.
// program k7c9 - page 139
// recursion : calculate matrix determinant.
// 12/2/2002
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#define N 4
#define dx 5
#define dy 2
#define xo (80 - N*dx)/2
#define yo (25 - N*dy)/2 + 1
int a[N][N] = {{ 1, 2,-2, 0},
{ 2, 3,-4, 1},
{-1,-2, 0, 2},
{ 0, 2, 5, 3}};
int row[N] = {0,0,0,0};
void ShowMatrix()
{
for(int i = 0;i < N;i++)
{
cout << endl << endl << setw(20) << " ";
for(int j = 0;j < N;j++)
cout << setw(5) << a[i][j];
}
} // SHOW MATRIX
int Process(int col)
{
int ret = 0,k = 0;
if(col == N-1)
{
while(row[k])
k++;
ret = a[k][col]; // return the value of determinant with
} // only one element
else
{
int sign = 1;
for(k = 0;k < N;k++)
{
if(row[k])
continue;
row[k]++;
ret += sign*a[k][col]*Process(col+1);
sign = -sign;
row[k]--;
}
}
return ret;
} // PROCESS
void main()
{
clrscr();
gotoxy(10,2);
cout << "recursion : calculate matrix determinant" << endl << endl;
ShowMatrix();
cout << endl << endl << endl;
cout << setw(38) << "determinant = "<< Process(0);
gotoxy(10,20);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
// program k7c9 - page 139
// recursion : calculate matrix determinant.
// 12/2/2002
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#define N 4
#define dx 5
#define dy 2
#define xo (80 - N*dx)/2
#define yo (25 - N*dy)/2 + 1
int a[N][N] = {{ 1, 2,-2, 0},
{ 2, 3,-4, 1},
{-1,-2, 0, 2},
{ 0, 2, 5, 3}};
int row[N] = {0,0,0,0};
void ShowMatrix()
{
for(int i = 0;i < N;i++)
{
cout << endl << endl << setw(20) << " ";
for(int j = 0;j < N;j++)
cout << setw(5) << a[i][j];
}
} // SHOW MATRIX
int Process(int col)
{
int ret = 0,k = 0;
if(col == N-1)
{
while(row[k])
k++;
ret = a[k][col]; // return the value of determinant with
} // only one element
else
{
int sign = 1;
for(k = 0;k < N;k++)
{
if(row[k])
continue;
row[k]++;
ret += sign*a[k][col]*Process(col+1);
sign = -sign;
row[k]--;
}
}
return ret;
} // PROCESS
void main()
{
clrscr();
gotoxy(10,2);
cout << "recursion : calculate matrix determinant" << endl << endl;
ShowMatrix();
cout << endl << endl << endl;
cout << setw(38) << "determinant = "<< Process(0);
gotoxy(10,20);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN