-
-
Save parzibyte/82cc3141f004bd533175e3eac80117d8 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> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define MAXIMA_CANTIDAD 100 | |
/* | |
____ _____ _ _ _ | |
| _ \ | __ \ (_) | | | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___ | |
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \ | |
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/ | |
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___| | |
__/ | __/ | | |
|___/ |___/ | |
____________________________________ | |
/ Si necesitas ayuda, contáctame en \ | |
\ https://parzibyte.me / | |
------------------------------------ | |
\ ^__^ | |
\ (oo)\_______ | |
(__)\ )\/\ | |
||----w | | |
|| || | |
Creado por Parzibyte (https://parzibyte.me). Este encabezado debe mantenerse intacto, | |
excepto si este es un proyecto de un estudiante. | |
*/ | |
int aleatorio_en_rango(int minimo, int maximo) { | |
return minimo + rand() / (RAND_MAX / (maximo - minimo + 1) + 1); | |
} | |
struct nodo { | |
int numero; | |
int frecuencia; | |
struct nodo *siguiente; | |
}; | |
struct nodo *superior = NULL; | |
struct nodo *obtenerApuntadorANumero(int numeroBuscado) { | |
struct nodo *temporal = superior; | |
while (temporal != NULL) { | |
if (temporal->numero == numeroBuscado) { | |
return temporal; | |
} | |
temporal = temporal->siguiente; | |
} | |
return NULL; | |
} | |
void agregarPorPrimeraVez(int numero) { | |
struct nodo *nuevoNodo = malloc(sizeof(struct nodo)); | |
nuevoNodo->numero = numero; | |
nuevoNodo->frecuencia = 1; | |
nuevoNodo->siguiente = superior; | |
superior = nuevoNodo; | |
} | |
void registrarNumero(int numero) { | |
struct nodo *apuntador = obtenerApuntadorANumero(numero); | |
if (apuntador == NULL) { | |
agregarPorPrimeraVez(numero); | |
} else { | |
apuntador->frecuencia++; | |
} | |
} | |
void imprimirFrecuencias() { | |
struct nodo *temporal = superior; | |
while (temporal != NULL) { | |
printf("Número %d frecuencia %d\n", temporal->numero, temporal->frecuencia); | |
temporal = temporal->siguiente; | |
} | |
} | |
int main() { | |
srand(getpid()); | |
int minimo = 1; | |
int maximo = 20; | |
int i; | |
int numeros[MAXIMA_CANTIDAD]; | |
for (i = 0; i < MAXIMA_CANTIDAD; i++) { | |
int numeroAleatorio = aleatorio_en_rango(minimo, maximo); | |
numeros[i] = numeroAleatorio; | |
registrarNumero(numeroAleatorio); | |
} | |
printf("Imprimiendo números\n"); | |
for (i = 0; i < MAXIMA_CANTIDAD; i++) { | |
printf("%d,", numeros[i]); | |
} | |
printf("\n"); | |
printf("Imprimiendo frecuencias\n"); | |
imprimirFrecuencias(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment