Version:0.9 StartHTML:0000000105 EndHTML:0000008838 StartFragment:0000000152 EndFragment:0000008804
/******************************************************/
/*************** Isidro Pastor Jorda ******************/
/******************************************************/
/****************** Telematica ************************/
/******************************************************/
/************* Practica 6 Ejercicio 4 *****************/
/******************************************************/
#include <iostream.h>
#include <stdlib.h>
const int FILAS = 3;
const int COLUMNAS = 3;
typedef int Matriz[FILAS][COLUMNAS];
/* Prototipos de las funciones */
void InicializaMatriz(Matriz M, int reset);
void GrabarMatriz(Matriz M);
void MostrarMatriz(Matriz M);
void ProductoMatrices (Matriz v1,Matriz v2,Matriz producto);
int main()
{
Matriz m1, m2, producto;
InicializaMatriz(m1, 0);
InicializaMatriz(m2, 0);
InicializaMatriz(producto, 0);
cout << " Programa para calcular el producto de dos matrices " << FILAS << "X" << COLUMNAS << endl;
cout << " Introduce los " << FILAS*COLUMNAS << " valores de la matriz 1: \n";
GrabarMatriz(m1);
cout << endl;
cout << " Introduce los " << FILAS*COLUMNAS << " valores de la matriz 2: \n";
GrabarMatriz(m2);
cout << " El producto de las dos matrices es: \n";
ProductoMatrices (m1, m2, producto);
MostrarMatriz(producto);
system("PAUSE");
return 0;
}
/******************************************************************
* Funcion: InicializaMatriz
* Descripcion: Funcion que inicializa una matriz al valor de reset
*
*
* Parametros:
*
* Nombre E/S Descripcion
* ------ ----- -----------
* M E/S Matriz a inicializar
* reset E Valor usado para inicializar la matriz
*
* Valor devuelto:
* void, la matriz no devuelve nada pero modifica el vector
*******************************************************************/
void InicializaMatriz(Matriz M, int reset)
{
for (int i = 0; i < FILAS; i++ )
for (int j = 0; j< COLUMNAS; j++)
M[i][j] = reset;
return;
}
/******************************************************************
* Funcion: GrabarMatriz
* Descripcion: Funcion para grabar en la matriz los datos introducidos
* por teclado.
*
* Parametros:
*
* Nombre E/S Descripcion
* ------ ----- -----------
* M E/S Matriz que iremos asignando valores leidos por teclado
*
*
* Valor devuelto:
* void, no devuelve nada la funcion pero modifica la matriz M
*******************************************************************/
void GrabarMatriz(Matriz M)
{
for (int i = 0; i < FILAS; i++ )
for (int j = 0; j< COLUMNAS; j++)
cin >> M[i][j];
}
/******************************************************************
* Funcion: MostrarMatriz
* Descripcion: Funcion para mostrar por pantalla la matriz tabulada
*
*
* Parametros:
*
* Nombre E/S Descripcion
* ------ ----- -----------
* M E Matriz a mostrar por pantalla
*
*
* Valor devuelto:
* void, la funcion solo muestra por pantalla la matriz
*******************************************************************/
void MostrarMatriz(Matriz M)
{
cout << endl << endl;
for (int i = 0; i < FILAS; i++ )
{
for (int j = 0; j< COLUMNAS; j++)
{
if (M[i][j] < 10)
cout << " " << M[i][j];
else
cout << " " << M[i][j];
}
cout << endl;
}
cout << endl;
}
void ProductoMatrices (Matriz m1,Matriz m2,Matriz producto)
{
int aux;
aux = 0;
for (int i = 0; i<FILAS; i++) // Nos indicara la fila de la matriz m1
{
for (int k = 0; k < COLUMNAS; k++) // k sera el numero de la columna que estamos multiplicando, columna de m2 y de producto
{ for (int j = 0; j < FILAS; j++)// j sera la fila de m2 que es la que va variando con cada multiplicacion
aux = aux + m1[i][j] * m2[j][k]; // multiplicamos cada elemento de la fila m1 con cada uno de la columna de m2
producto[i][k] = aux; // Almacenamos en producto el aux que es la suma de los productos de la filaxcolumna
aux = 0;// Iniciamos aux a cero para no acarrear la suma del producto anterior
/* Avanzaremos en una fila la matriz m1 en la siguiente iteracion y volveremos a recorrer todas las columnas de m2 */
}
}
}