Last active
February 16, 2023 16:05
-
-
Save parzibyte/860d855e7028317461fe9d49d2e45f7d to your computer and use it in GitHub Desktop.
Producto de matrices en C | https://parzibyte.me/blog/2019/11/11/producto-matrices-c-multiplicacion/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Producto de matrices en C | |
* | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
* */ | |
#include <stdio.h> | |
#define FILAS_MATRIZ_B 3 | |
#define COLUMNAS_MATRIZ_B 2 | |
#define FILAS_MATRIZ_A 3 | |
#define COLUMNAS_MATRIZ_A 3 | |
int main(void) { | |
int matrizA[FILAS_MATRIZ_A][COLUMNAS_MATRIZ_A] = { | |
{3, 2, 1}, | |
{1, 1, 3}, | |
{0, 2, 1}, | |
}; | |
int matrizB[FILAS_MATRIZ_B][COLUMNAS_MATRIZ_B] = { | |
{2, 1}, | |
{1, 0}, | |
{3, 2}, | |
}; | |
if (COLUMNAS_MATRIZ_A != FILAS_MATRIZ_B) { | |
printf("Columnas de matriz A deben ser igual a filas de matriz B"); | |
return 0; | |
} | |
//Lugar en donde se almacena el resultado | |
int producto[FILAS_MATRIZ_B][COLUMNAS_MATRIZ_B]; | |
// Necesitamos hacer esto por cada columna de la segunda matriz (B) | |
for (int a = 0; a < COLUMNAS_MATRIZ_B; a++) { | |
// Dentro recorremos las filas de la primera (A) | |
for (int i = 0; i < FILAS_MATRIZ_A; i++) { | |
int suma = 0; | |
// Y cada columna de la primera (A) | |
for (int j = 0; j < COLUMNAS_MATRIZ_A; j++) { | |
// Multiplicamos y sumamos resultado | |
suma += matrizA[i][j] * matrizB[j][a]; | |
} | |
// Lo acomodamos dentro del producto | |
producto[i][a] = suma; | |
} | |
} | |
// Recorrer producto | |
printf("Imprimiendo producto\n"); | |
for (int i = 0; i < FILAS_MATRIZ_B; i++) { | |
for (int j = 0; j < COLUMNAS_MATRIZ_B; j++) { | |
printf("%d ", producto[i][j]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment