Problem As Matrices

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

//Calcular el determinante de una matriz cualquiera

#include <iostream>
#include <math.h>
using namespace std;
double det(double**, int);
int main()
{
int n; // n = numero de renglones = numero de columnas
double **m = NULL;
cout << "Orden de la matriz: ";cin >> n;
// Se crea la matriz de forma dinamica
m = (double **)malloc(sizeof(double)*n);
for (int i = 0; i<n; i++)
m[i] = (double *)malloc(sizeof(double)*n);

// Pide el valor de cada elemento de la matriz


for (int y = 0; y<n; y++)
for (int w = 0; w<n; w++){
cout << "Introduzca el valor de matriz[" << y << "][" << w << "]
: ";
cin >> m[y][w];
}
cout << "\n";
//Impresion de los valores dados
cout << " MATRIZ DADA ";
cout << "\n";
for (int y = 0; y < n; y++)
{
for (int w = 0; w < n; w++)
{
cout << " " << m[y][w];
}
cout << "\n";
}
cout << "\n";
// Despliego el resultado de la funcion det
cout << "Determinante: " << det(m, n);
cout << "\n";

// Libero la memoria utilizada por la matriz


for (int r = 0; r<n; r++) free(m[r]);
free(m);

system("pause");
return 0;
}
double det(double **m, int b)
{
double determinante = 0, aux = 0;
int c;
if (b == 2)
return m[0][0] * m[1][1] - m[1][0] * m[0][1];
else{
for (int j = 0; j<b; j++){
double **menor = (double **)malloc(sizeof(double)*(b - 1
));
for (int h = 0; h<(b - 1); h++) menor[h] = (double *)mal
loc(sizeof(double)*(b - 1));
for (int k = 1; k<b; k++){
c = 0;
for (int l = 0; l<b; l++){
if (l != j){
menor[k - 1][c] = m[k][l];
c++;
}
}
}
aux = pow(-1, 2 + j)*m[0][j] * det(menor, b - 1);
determinante += aux;
for (int q = 0; q<(b - 1); q++)
free(menor[q]);
free(menor);
}
return determinante;
}

//Matriz de Vandermonde
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i, j,det,p,k,t,orden;
long int A[100][100];
cout << "Orden de la matriz: "; cin >> orden;
for (int i = 0; i<orden; i++)
{
A[i][0] = 1;
}
cout << "\n";
for (int i = 0; i<orden; i++)
{
cout << "El numero A[" << i << "][1]:"; cin >> A[i][1];
}
for (int i = 0; i<orden; i++)
{
for (int j = 2; j<orden; j++)
{
A[i][j] = A[i][j - 1] * A[i][1];
}
}
cout << "\n";
cout << "Matriz de Vandermonde";
cout << "\n";
for (int i = 0; i<orden; i++)
{
for (int j = 0; j<orden; j++)
{
cout << setw(7) << A[i][j];
}
cout << "\n";
}
det= 1;
for (i = 0; i < orden; i++){
t = A[i][1];
k = i + 1;
if (k == orden){
break;
}
p = 1;
while (k < orden){
p = p*(A[k][1] - t);
k++;
}
det = det*p;
}

cout << "\nDeterminante de la matriz: " << det << endl;


system("pause");
return 0;
}

//Invertir una matriz


#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int fm, aux,n;
int A[100][100] ;
srand(time(NULL));
cout << "Orden de la matriz: "; cin >> n;
cout << "MATRIZ DADA\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
A[i][j] = rand() % 100;
cout << setw(7) << A[i][j];
}
cout << endl;
}
if (n % 2 == 0)
fm = (n / 2) - 1;
else
fm = n / 2;
for (int i = 0; i <= fm; i++)
{
for (int j = 0; j < n; j++)
{
if ((n % 2 != 0) && (i == fm) && (j == fm + 1))
break;
aux = A[i][j];
A[i][j] = A[(n - 1) - i][(n - 1) - j];
A[(n - 1) - i][(n - 1) - j] = aux;
}
}
cout << endl;
cout << "MATRIZ INVERTIDA\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout <<setw(7)<< A[i][j];
}
cout << endl;
}

system("pause");
return 0;
}

//Resolucion de sistemas de ecuaciones de n incognitas


#include <iostream>
#include<iomanip>
using namespace std;
int i, j, k, l, s;
double m1[1000][1000], coef, aux[1000], elemento;
int main()
{
cout << "Bienvenido al solucionador de ecuaciones";
cout << "\nPor favor ingrese el numero de incognitas a resolver: ";
cin >> k;
l = k + 1;
for (i = 0; i<k; i++)
{
for (j = 0; j<l; j++)
{
cout << "Coeficiente [" << i << "][" << j << "]=";
cin >> m1[i][j];
}
}
for (i = 0; i<k; i++)
{
for (j = 0; j<l; j++)
{
cout<<setw(7)<<m1[i][j];
}
cout << endl;
}
for (s = 0; s<k; s++)
{
elemento = m1[s][s];
for (j = 0; j<l; j++)
m1[s][j] = m1[s][j] / elemento;
for (i = 0; i<k; i++)
{
if (i == s)
;
else
{
coef = m1[i][s];
for (j = 0; j<l; j++)
aux[j] = m1[s][j] * (coef*-1);

for (j = 0; j<l; j++)


m1[i][j] = m1[i][j] + aux[j];
}
}
}
//Imprimir las soluciones
for (i = 0; i<k; i++)
{
cout << "X[" << i << "]=" << m1[i][k] << endl;
}
system("pause");
return 0;
}

You might also like