Вы находитесь на странице: 1из 11

EJERCICIO 3:

Programar la suma de dos matrices cuadradas de dimensin DIM.


/*Suma de dos matrices cuadradas*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DIM 100
void leer_matriz(int,float[DIM][DIM]);
void escribir_matriz(int,float[][DIM]);
void suma_matrices (int,float[][DIM],float[][DIM],float[][DIM]);
int main()
{
int n; float x[DIM][DIM], y[DIM][DIM], z[DIM][DIM];
do
{
printf("Introduce la dimension de la matriz (<=100): ");
scanf("%d",&n);
}while ((0>n) || (n>DIM));
printf("\nIntroduce la primera matriz\n");
leer_matriz(n,x);
system("cls");
printf("\nIntroduce la segunda matriz\n");
leer_matriz(n,y);
system("cls");
suma_matrices(n,x,y,z);
printf("\nSuma de las dos matrices:\n");
escribir_matriz(n,z);
system("PAUSE");
return 0;
}
void leer_matriz(int n, float v[][DIM])
{
int i,j;
for(i=0;i<n;i++)
{
printf("\nVamos a introducir la fila %d.\n",i+1);
for(j=0;j<n;j++)
{
printf("Introduce la componente (%d,%d): ",i+1,j+1);
scanf("%f",&v[i][j]);
}

}
}
void escribir_matriz(int n, float v[][DIM])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%12.2f",v[i][j]);
}
printf("\n");
}
}
void suma_matrices (int n,float v1[][DIM],float v2[][DIM],float v3[][DIM])
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
v3[i][j]=v1[i][j]+v2[i][j];
}

EJERCICIO 4:
Lo mismo para el producto de dos matrices cuadradas de dimensin DIM.
/*Producto de dos matrices cuadradas*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DIM 100
void leer_matriz(int,float[DIM][DIM]);
void escribir_matriz(int,float[][DIM]);
void producto_matrices (int,float[][DIM],float[][DIM],float[][DIM]);
int main()
{
int n; float x[DIM][DIM], y[DIM][DIM], z[DIM][DIM];
do
{
printf("Introduce la dimension de la matriz (<=100): ");
scanf("%d",&n);
} while ((0>n) || (n>DIM));

printf("\nIntroduce la primera matriz\n");


leer_matriz(n,x);
system("cls");
printf("\nIntroduce la segunda matriz\n");
leer_matriz(n,y);
system("cls");
producto_matrices(n,x,y,z);
printf("\nProducto de las dos matrices:\n");
escribir_matriz(n,z);
system("PAUSE");
return 0;
}
void leer_matriz(int n, float v[][DIM])
{
int i,j;
for(i=0;i<n;i++)
{
printf("\nVamos a introducir la fila %d.\n",i+1);
for(j=0;j<n;j++)
{
printf("Introduce la componente (%d,%d): ",i+1,j+1);
scanf("%f",&v[i][j]);
}
}
}
void escribir_matriz(int n, float v[][DIM])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%12.2f",v[i][j]);
}
printf("\n");
}
}
void producto_matrices (int n,float v1[][DIM],float v2[][DIM],float v3[][DIM])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
v3[i][j]=0;
for(k=1;k<n;k++)
v3[i][j]=v3[i][j]+v1[i][k]*v2[k][j];

}
}

Hola, estoy haciendo un programa que me tiene que calcular la determinante de una matriz de
3x3(sin utilizar funciones solo arreglos). Pero no me hace los clculos correctamente y no se
dnde est mi error
por ejemplo cuando le ingreso los nmeros:
m(0,0)=5
m(0,1)=2
m(0,2)=4
m(1,0)=-1
m(1,1)=5
m(1,2)=3
m(2,0)=6
m(2,1)=3
m(2,2)=-2
me sale -95 cuando el resultado debera ser -195
muchas gracias
Cdigo:

#include <stdio.h>
#include <stdlib.h>
#define TAMANIO 3
/* Leer una matriz cuadrada de tamao 3x3 e imprimir la determinante*/
int main()
{
int i,j,m[TAMANIO][TAMANIO],det;
printf("Imprime la matriz cuadrada(3x3)\n");
for(i=0;i<TAMANIO;i++)
{
for(j=0;j<TAMANIO;j++)
{
scanf("%d",&m[i][j]);
system("cls");
}
}
printf("\n %d | %d | %d\n",m[0][0],m[0][1],m[0][2]);
printf("---+---+---\n");
printf("\n %d | %d | %d\n",m[1][0],m[1][1],m[1][2]);
printf("---+---+---\n");
printf("\n %d | %d | %d\n\n",m[2][0],m[2][1],m[2][2]);
det= -(m[0][0]*m[1][1]*m[2][2])+(m[0][1]*m[1][2]*m[2][0])+(m[1][0]*m[2][1]*m[0][2])(m[0][2]*m[1][1]*m[2][0])-(m[0][0]*m[1][2]*m[2][1])-(m[0][1]*m[1][0]*m[2][2]);
printf("El determinante de la matriz es %d\n",det);
return 0;

Este programa obtiene el determinante de una matriz n*n empleando


recursividad. Hay que darle como entreada el tamao de la matriz (n) y los n*n
elementos de la matriz (m[i][j]). Devuelve el determinante de la matriz.
#include <iostream.h>
#include <alloc.h>
#include <math.h>

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 *)malloc(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;
}
}

int main(){
int n; // n = numero de renglones = numero de columnas
double **m=NULL;
cout<<"Elija el numero de renglones o columnas que tiene su 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];
}

// Despliego el resultado de la funcion det


cout<<"nDeterminante: "<<det(m, n);

// Libero la memoria utilizada por la matriz


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

return 0;
}

1.- Escribir un programa que haga el producto de dos matrices 3x3. El


programa debe incluir un procedimiento que lea las matrices, una funcin que
haga el producto y otro procedimiento que escriba el resultado:
SOLUCIN
#include<cstdlib>
#include<iostream>
using namespace std;
void leermatriz (float m[3][3])
{
int i,j;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
cout<<"introducir el elemento "<<i<<","<<j<<endl;
cin>>m[i][j];
}
}
}
void producto_matrices (float a [3][3],float b[3][3],float p[3][3])
{
int i,j,k;
for (i=1;i<=3;i++)
{
p[i][j]=0;
for (j=1;j<=3;j++)
{
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
}
}

void escribir_matriz (float m[3][3])


{
int i,j;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
cout<<m[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
float a[3][3];
leermatriz (a);
leermatriz (b);
producto_matrices (a,b,p);
escribir_matriz (p);

system("PAUSE");
return EXIT_SUCCESS;
}

#include <iostream>

using namespace std;

int row,fil,i,j;

int main()
{
cout <<"Ingrese el numero de filas "<<endl;
cin >>fil;
cout <<"Ingrese el numero de columnas "<<endl;
cin >>row;
int matriz1[fil][row];
for

(i=0;i<=fil-1;i++)

{
for

(j=0;j<=row-1;j++)

{
cout <<"Ingrese la posicion
"<<"("<<i<<")"<<"("<<j<<")"<<" de la matriz 1"<<endl;
cin >>matriz1[i][j];
}
}
system("pause");
int matriz2[fil][row];
for

(i=0;i<=fil-1;i++)

{
for

(j=0;j<=row-1;j++)

{
cout <<"Ingrese la posicion
"<<"("<<i<<")"<<"("<<j<<")"<<" de la matriz 2"<<endl;
cin >>matriz2[i][j];

}
}
system("pause");
cout << "A continuacion se sumaran las matrices ingresadas
"<<endl;
system("pause");
int matriz3[fil][row];
for

(i=0;i<=fil-1;i++)

{
for

(j=0;j<=row-1;j++)

{
matriz3[i][j]= matriz1[i][j]+matriz2[i][j];
cout << "Valor posicion "<<"("<<i<<")"<<"("<<j<<"):
"<<matriz3[i][j]<<endl;
}
}
cout << "Gracias por usar este programa "<<endl;
system("pause");
return
}

0;

Вам также может понравиться