Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 12, 2019 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/a49cae0f90a4defc4714a030260d3d6c to your computer and use it in GitHub Desktop.
Save parzibyte/a49cae0f90a4defc4714a030260d3d6c to your computer and use it in GitHub Desktop.
#include <stdio.h>
/*
Comparación de matrices en C
By parzibyte
Visita mi blog: https://parzibyte.me/blog
*/
int main(void) {
int matriz1[2][2], matriz2[2][2];
// Leer matriz 1
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
printf("Valor para matriz1 en [%d][%d]: ", x, y);
scanf("%d", &matriz1[x][y]);
}
}
// Leer matriz 2
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
printf("Valor para matriz2 en [%d][%d]: ", x, y);
scanf("%d", &matriz2[x][y]);
}
}
// Hora de comparar. Se supone que ambas tienen la misma
// longitud, y al inicio suponemos que son iguales
int sonIguales = 1;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int valorDeMatriz1 = matriz1[x][y];
int valorDeMatriz2 = matriz2[x][y];
if (valorDeMatriz1 != valorDeMatriz2) {
// Nota: aquí deberíamos romper los ciclos, pues ya
// sabemos que no son iguales. Podríamos poner todo
// esto dentro de una función y hacer un return,
// ya que con un break solo se rompería este ciclo y no el
// de arriba
// En fin, solo decía
sonIguales = 0;
}
}
}
if (sonIguales) {
printf("Son iguales");
} else {
printf("NO son iguales");//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment