Laboratorio 4
Uriol Evangelista Cristhian José
11 / Julio / 2018
Parte 1 :
#include<iostream>
#include<math.h>
# include <stdio.h>
using namespace std;
double Leer_Matriz() , Ver_Matriz() , Leer_2_Matrices() , Sumar_2_Matrices() , Restar_2_Matrices() , Inversa() , Menor_Indice() , Mayor_Indice() , Matriz_Identidad() , Tanspuesta() ;
double Promedio_de_la_Suma() , Multiplicacion_de_Matrices() , Determinante_de_Matriz() , Menores_Complementarios() ;
double M [5] [5] ;
int i , j , MIN ,MAX , IND , ij , Prom , Suma , det , k , l ;
double A [5] [5] ;
double B [5] [5] ;
double C [5] [5] ;
double I [5] [5] ;
double De [5] [5] ;
double N [5] [5] ;
double Mu [5] [5] ;
int main()
{
int opcion;
do
{
cout<< " Menu Opciones : \n " ;
cout<< " 1.- Leer Matriz : \n " ;
cout<< " 2.- Mirar Matriz : \n " ;
cout<< " 3.- Leer 2 Matrices : \n " ;
cout<< " 4.- Sumar 2 Matrices : \n " ;
cout<< " 5.- Restar 2 Matrices : \n " ;
cout<< " 6.- Transpuesta : \n " ;
cout<< " 7.- Mayor Indice de la Matriz : \n " ;
cout<< " 8.- Menor Indice de la Matriz : \n " ;
cout<< " 9.- Matriz Identidad : \n " ;
cout<< " 10.- Promedio de la Suma de dos Matrices : \n " ;
cout<< " 11.- Multiplicacion de dos Matrices : \n " ;
cout<<" Opciones : " ; cin >> opcion;
switch (opcion)
{
case 1:
{
Leer_Matriz();
};break ;
case 2:
{
Ver_Matriz();
};break;
case 3:
{
Leer_2_Matrices();
};break;
case 4:
{
Sumar_2_Matrices();
};break;
case 5:
{
Restar_2_Matrices();
};break;
case 6:
{
Tanspuesta();
};break;
case 7:
{
Mayor_Indice();
};break;
case 8:
{
Menor_Indice();
};break;
case 9:
{
Matriz_Identidad();
};break;
case 10:
{
Promedio_de_la_Suma();
};break;
case 11:
{
Multiplicacion_de_Matrices() ;
};break;
}//fin switch
}while (opcion=!0);
//system ("pouse") ;
return 0;
}
//Zona de Funciones
//caso 1
double Leer_Matriz()
{
for( i = 1 ; i <= 4 ; i++ )
for( j = 1 ; j <= 4 ; j++ )
{
cout<<"La matriz M["<<i<<"]["<<j<<"]"<<" es = "; cin>> M [i] [j];
}
}
//Caso 2
double Ver_Matriz()
{
cout<<" La matriz "<<endl;
for ( i = 1 ; i <= 4 ; i++ )
{
for ( j=1 ; j <= 4 ; j++ )
{
cout<<" M ["<<i<<"]["<<j<<"] "<<"=" << M [i] [j]<<endl;
}
cout<<endl;
}
}
//Caso 3
double Leer_2_Matrices()
{
cout<<" Ingrese la Matriz A[] "<<endl;
for ( i = 1 ; i <= 4 ; i++)
for ( j = 1 ; j <= 4 ; j++)
{
cout<<" La matriz A ["<<i<<"]["<<j<<"]"<<" es = "; cin>>A [i] [j] ;
}
cout<<" Ingrese la Matriz B[]"<<endl;
for( i = 1 ; i <= 4 ; i ++)
for( j = 1 ; j <= 4 ; j ++)
{
cout<<" La matriz B ["<<i<<"]["<<j<<"]"<<" es = "; cin>>B [i] [j];
}
}
//Caso 4
double Sumar_2_Matrices()
{
cout<<" La Suma de las Matrices A[] + B[] ES = "<<endl;
cout<<endl;
for ( i = 1 ; i <= 4 ; i++)
{
for ( j = 1 ; j <= 4 ; j++)
{
C [i] [j] = A [i] [j] + B [i] [j] ;
cout<<" A+B["<<i<<"]["<<j<<"]"<<" = "<<C [i] [j]<<endl;
}
cout<<endl;
}
}
//Caso 5
double Restar_2_Matrices()
{
cout<<" La Resta de las Matrices A[] + B[] ES = "<<endl;
cout<<endl;
for ( i = 1 ; i <= 4 ; i++)
{
for ( j = 1 ; j <= 4 ; j++)
{
N [i] [j] = A [i] [j] - B [i] [j] ;
cout<<" A-B["<<i<<"]["<<j<<"]"<<" = "<<N [i] [j]<<endl;
}
cout<<endl;
}
}
//Caso 6
double Tanspuesta()
{
cout<<" La Tanspuesta de la Matriz C es : "<<endl;
for ( i = 1 ; i <= 4 ; i++ )
{
for ( j = 1 ; j <= 4 ; j++ )
{
cout<<" A+B Tanspuesta ["<<j<<"]["<<i<<"] "<<"=" << C [i] [j] ;
}
cout<<endl;
}
}
//Caso 7
double Mayor_Indice()
{
for ( i = 1 ; i <= 4 ; i++)
for ( j = 1 ; j <= 4 ; j++)
{
if ( C [i] [j] >=MAX)
{
MAX = C [i] [j] ;
IND=ij;
}
}
cout<<" El Maximo Valor se Encuentra en = ""C["<<i<<"]["<<j<<"]"<<" es = "<<MAX<<endl;
}
//Caso 8
double Menor_Indice()
{
MIN = C [i] [j] ;
for ( i = 1 ; i <= 4 ; i++)
for( j = 1 ; j <= 4 ; j++)
{
if( C [i] [j] <= MAX )
{
MIN = C [i] [j] ;
IND = ij;
}
}
cout<<" EL Minimo Valor se Encuentra en = ""C["<<i<<"]["<<j<<"]"<<" es = "<<MIN<<endl;
}
//Caso 9
double Matriz_Identidad()
{
cout<<" \n Matriz Identidad \n";
for ( i = 1 ; i <= 4 ; i++)
{
for( j = 1 ; j <= 4 ; j++)
{
if(i==j)
{
I [i] [j] = 1 ;
}
if ( i != j)
{
I [i] [j] = 0 ;
}
cout<<" I["<<i<<"]["<<j<<"] = "<<I [i] [j] ;
}
cout<<endl;
}
}
//Caso 10
double Promedio_de_la_Suma()
{
Suma = 0 ;
for( i = 1 ; i <= 4 ; i++ )
for( j = 1 ; j <= 4 ; j++ )
Suma = Suma + C [i] [j];
Prom = Suma / ( i * j );
cout<<" El Promedio de la Suma es : "<<Prom<<endl;
}
//Caso 11
double Multiplicacion_de_Matrices()
{
cout<<" La Multiplicacion de la Matriz A[] y la matriz B[] es = "<<endl;
for ( i = 1 ; i <= 3 ; i++)
{
for ( j = 1 ; j <= 3 ; j++)
{
Mu [i] [j] = (A [i] [1] * B [1] [j] ) + ( A [i] [2] * B [2] [j] ) + ( A [i] [3] * B [3] [j] ) ;
cout<<" A*B["<<i<<"]["<<j<<"]"<<" = "<<Mu[i][j];
}
cout<<endl;
}
}
Parte 2 :
#include<iostream>
using namespace std;
int main(){
int n=0,m=0;
cout<<"Ingrese el Tamaño de la Matriz "<<endl;
cin >>n>> m;
int mat[n][m],cof[n][m],adj[n][m],cofaux[n][m],inv[n][m],aux_inv[n][m];
if(n==m && n>0 && m>0){
cout<<"Es una Matriz Cuadrada "<<endl;
}
else{
cout<<"No se Puede Calcular la Matriz. Tiene que ser Cuadrada "<<endl;
}
int i=0,j=0,det=0,det2=0,det3=0,det4=0,aux_cof=1,d=0,k=0,e=0;
float det5;
cout<<"Ingrese Valores de la Matriz "<<endl;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin >> mat[i][j];
}
}
//calculamos la matriz cofactor
for(i=0;i<n;i++){
for(j=0;j<m;j++){
for(d=0;d<n;d++){
for(e=0;e<m;e++){
for(k=0;k<m;k++){
if((i+k)%2==0){
aux_cof = 1;
}
else if((i+k)%2!=0){
aux_cof= -1;
}
if((d==k || d!=k) && (e==k || e==d)){
cofaux[i][j]= aux_cof*((mat[i][k]*mat[d][j])-(mat[i][k]*mat[e][j]));
}
}
}
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cof[i][j]= cofaux[i][j];
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
adj[i][j] = cof[j][i];
}
}
//buscamos determinante
for(i=0;i<n;i++){
for(j=0;j<m;j++){
for(k=0;k<n;k++){
for(e=0;e<n;e++){
det=0;
det2=0;
for(d=0;d<m;d++){
if((d!=k || d==k) && (e==k || e==d)&& i!=j){
if(i%2==0 & j%2==0 ){
det= det + ((mat[i][d]*mat[k][j])-(mat[i][e]*mat[d][j]));
}
else if(i%2!=0 && j%2!=0 ){
det2= det-((mat[i][d]*mat[k][j])-(mat[i][d]*mat[e][j]));
}
}
if(i==j){
det3= det3 +(mat[i][j]*det);
det4= det4 +(mat[i][j]*det2);
}
}
}
}
}
}
det5 = det3 + det4;
if(det5==0){
cout<<"La Matriz no Tiene Inversa"<<endl;
}
else{
for(i=0;i<n;i++){
for(j=0;j<m;j++){
aux_inv[i][j]= (1/det5)*adj[i][j];
inv[i][j] = aux_inv[i][j];
}
}
}
cout<<"Los Elementos de la Matriz Inversa "<<endl;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<< inv[i][j]<<" ";
}
cout << endl;
}
return 0;
}
No hay comentarios.:
Publicar un comentario