You are on page 1of 4

#include "stdafx.

h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include <stdio.h>

using namespace std;

int main()
{
int i, j, k, n, k1, l, m, opcion;
float a[10][10] = { 0 }, d, det, b[10][10];
system("cls");
cout << "Introduzca la dimesion "; cin >> n;
cout << "Introduzca los coeficientes " << endl;
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
cout << "Introduzca el valor:(" << i << ", " << j << ") ";
cin >> a[i][j];
}
}
/* copiamos */
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
b[i][j] = a[i][j];
}
}

/*****Calculo del DETERMINANTE*****/


{m = n - 1;
det = b[1][1];
for (k1 = 1; k1 <= m; k1++)
{
l = k1 + 1;
for (i = l; i <= n; i++)
{
for (j = l; j <= n; j++)
b[i][j] = (b[k1][k1] * b[i][j] - b[k1][j] * b[i][k1]) /
b[k1][k1];
}
det = det*b[k1 + 1][k1 + 1];
}
cout << endl;
cout << "DETERMINANTE = " << det << endl;
cout << "------------------------" << endl;
}
//si es cero el determinante no tiene inversa
if (det == 0)
{
cout << "No tiene inversa, el determinante es cero\n\n";
system("pause");
exit(0);
}
else {
for (i = 1; i <= n; i++)
for (j = 1; j <= 2 * n; j++)
if (j == (i + n))

a[i][j] = 1;

/************** Pivoteo parcial **************/


for (i = n; i > 1; i--)
{
if (a[i - 1][1] < a[i][1])
for (j = 1; j <= n * 2; j++)
{
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
}
}
cout << "Pivoteo parcial: " << endl;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
cout << a[i][j] << "

";

cout << endl;


}
/********** reduccin diagonal ***********/

for (i = 1; i <= n; i++)


{
for (j = 1; j <= n * 2; j++)
if (j != i)
{
d = a[j][i] / a[i][i];

for (k = 1; k <= n * 2; k++)


a[j][k] -= a[i][k] * d;
}
}
/************** reduccin unitaria *************/
for (i = 1; i <= n; i++)
{
d = a[i][i];
for (j = 1; j <= n * 2; j++)
a[i][j] = a[i][j] / d;
}

cout << "La inversa es: " << endl;


for (i = 1; i <= n; i++)
{
for (j = n + 1; j <= n * 2; j++)
cout << a[i][j] << "
cout << endl;
}
}
system("pause");
exit(0);

system("pause");
return 0;
}

";

You might also like