Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active October 15, 2020 19:27
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/82cc3141f004bd533175e3eac80117d8 to your computer and use it in GitHub Desktop.
Save parzibyte/82cc3141f004bd533175e3eac80117d8 to your computer and use it in GitHub Desktop.
#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