Skip to content

Instantly share code, notes, and snippets.

@RamonLopezEscudero
Last active February 6, 2016 19:47
Show Gist options
  • Save RamonLopezEscudero/838d75b6441236922633 to your computer and use it in GitHub Desktop.
Save RamonLopezEscudero/838d75b6441236922633 to your computer and use it in GitHub Desktop.
Multiplicación de dos matrices
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* ---------------------------------- */
int i, j, k;
int n_row_m1, n_col_m1, n_row_m2, n_col_m2;
/* ---------------------------------- */
printf("Programa para resolver multiplicaciones matriciales\n \n");
/* Entrada de las dimensiones de las matrices a operar */
printf("Matriz Numero 1\n");
printf("Ingrese el numero de filas: ");
scanf("%i", &n_row_m1);
printf("Ingrese el numero de columnas: ");
scanf("%i", &n_col_m1);
printf("\nMatriz Numero 2\n");
printf("Ingrese el numero de filas: ");
scanf("%i", &n_row_m2);
printf("Ingrese el numero de columnas: ");
scanf("%i", &n_col_m2);
/* Iteracion hasta que las matrices esten definidas */
while (n_col_m1 != n_row_m2)
{
printf("\nEl numero de columnas de la matriz numero 1 debe ser igual al numero de filas de la matriz 2");
printf("\n \nMatriz Numero 1\n");
printf("Ingrese el numero de filas: ");
scanf("%i", &n_row_m1);
printf("Ingrese el numero de columnas: ");
scanf("%i", &n_col_m1);
printf("\nMatriz Numero 2\n");
printf("Ingrese el numero de filas: ");
scanf("%i", &n_row_m2);
printf("Ingrese el numero de columnas: ");
scanf("%i", &n_col_m2);
}
/* ---------------------------------- */
float val_temp, matriz_1[n_row_m1][n_col_m1], matriz_2[n_row_m2][n_col_m2], matriz_resultado[n_row_m1][n_col_m2];
/* ---------------------------------- */
/* Iteracion de entrada de datos de la matriz 1 */
printf("\nIngrese los valores de la matriz numero 1");
for (i = 0; i < n_row_m1; i++)
{
printf("\nIngrese los elementos del renglon %i \n", i + 1);
for (j = 0; j < n_col_m1; j++)
{
printf("Elemento %i: ", j + 1);
scanf("%f", &matriz_1[i][j]);
}
}
/* Iteracion de entrada de datos de la matriz 2 */
printf("\nIngrese los valores de la matriz numero 2");
for (i = 0; i < n_row_m2; i++)
{
printf("\nIngrese los elementos del renglon %i \n", i + 1);
for (j = 0; j < n_col_m2; j++)
{
printf("Elemento %i: ", j + 1);
scanf("%f", &matriz_2[i][j]);
}
}
/* Multiplicacion de Matrices */
val_temp = 0.0;
for (i = 0; i < n_row_m1; i++)
{
for (j = 0; j < n_col_m2; j++)
{
for (k = 0; k < n_row_m2; k++)
{
val_temp = val_temp + (matriz_1[i][k] * matriz_2[k][j]);
}
matriz_resultado[i][j] = val_temp;
val_temp = 0.0;
}
}
/* Impresion de los resultados */
printf("\nVector Resultado:\n");
for (i = 0; i < n_row_m1; i++)
{
printf("[ ");
for (j = 0; j < n_col_m2; j++)
{
printf("%f ", matriz_resultado[i][j]);
}
printf("]\n");
}
printf("\n");
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment