-
-
Save parzibyte/35c1b45c04c77e6674cb219cf4794171 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
/* | |
Comparación de arreglos en C | |
By parzibyte | |
Visita mi blog: https://parzibyte.me/blog | |
*/ | |
int main(void) { | |
int arreglo[] = {1, 2, 3}; | |
int otroArreglo[] = {1, 2, 4}; // Ojo: el último número cambia | |
// Primero comparar su longitud | |
int longitudDeArreglo = sizeof(arreglo) / sizeof(arreglo[0]), | |
longitudDeOtroArreglo = sizeof(otroArreglo) / sizeof(otroArreglo[0]); | |
if (longitudDeArreglo != longitudDeOtroArreglo) { | |
printf("Los arreglos no son iguales"); | |
return 0; | |
} | |
// Si llegamos aquí es porque la longitud es la misma, así que | |
// vamos a comparar valor por valor | |
// Nota: en el ciclo se puede tomar la longitud de cualquier arreglo, pues es | |
// la misma | |
for (int i = 0; i < longitudDeArreglo; i++) { | |
// Obtener elementos de ambos arreglos en misma posición o índice | |
int valorDelArreglo = arreglo[i], valorDelOtroArreglo = otroArreglo[i]; | |
// Comparar ;) | |
if (valorDelArreglo != valorDelOtroArreglo) { | |
printf("Los arreglos NO son iguales"); | |
return 0; // Terminar ejecución | |
} | |
} | |
// Si terminamos de recorrer ambos arreglos | |
// y no salimos del programa, significa que | |
// ambos sí son iguales | |
printf("Los arreglos SON iguales"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment