Skip to content

Instantly share code, notes, and snippets.

💻
Coding

Luis Cabrera Benito parzibyte

💻
Coding
Block or report user

Report or block parzibyte

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View media_mediana.c
/**
* Obtener media y mediana de arreglo en C
* Media: promedio
* Mediana: el elemento que se encuentra en el centro de una serie ordenada
*
* @author parzibyte
* @see https://parzibyte.me/blog
* */
#include <stdio.h>
#include <stdlib.h>
View imprimir.c
printf("Imprimendo arreglo:\n");
for (int x = 0; x < longitud; x++) {
printf("%d ", arreglo[x]);
}
printf("\nMediana: %d", mediana);
printf("\nMedia: %f", media);
View media.c
// Obtener media
int sumatoria = 0;
for (int x = 0; x < longitud; x++) {
sumatoria += arreglo[x];
}
float media = (float) sumatoria / (float) longitud;
View mediana.c
int mediana;
// Después vemos si la longitud es par
if (longitud % 2 == 0) {
// Debemos promediar los valores del centro
mediana = (arreglo[mitad - 1] + arreglo[mitad]) / 2;
} else {
mediana = arreglo[mitad];
}
View ordenar.c
int funcionQueCompara(const void *a, const void *b) {
// Castear a enteros
int aInt = *(int *) a;
int bInt = *(int *) b;
// Al restarlos, se debe obtener un número mayor, menor o igual a 0
// Con esto ordenamos de manera ascendente
return bInt - aInt;
}
qsort(arreglo, longitud, tamanioElemento, funcionQueCompara);
View definir.c
int arreglo[] = {28, 11, 96, 21, 97, 6, 18, 13, 1, 19, 51};
int tamanioElemento = sizeof arreglo[0];
int longitud = sizeof arreglo / tamanioElemento;
View ventas.txt
MariaDB [ventas]> describe ventas;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| idPaciente | int(11) | NO | MUL | NULL | |
| fecha | datetime | NO | | NULL | |
| total | decimal(7,2) | YES | | NULL | |
| vista | tinyint(1) | NO | | 0 | |
| idUsuario | bigint(20) unsigned | NO | | NULL | |
View consulta.sql
ALTER TABLE ventas
ADD COLUMN idMedico int(200) NOT NULL default 1,
ADD CONSTRAINT `fk_id_medico` FOREIGN KEY (idMedico)
REFERENCES medicos (id);
View medicos.txt
MariaDB [ventas]> describe medicos;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(200) | NO | PRI | NULL | auto_increment |
| Nombre | text | NO | | NULL | |
| Cedula | varchar(10) | NO | | NULL | |
| Especialidad | text | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
View ventas.txt
MariaDB [ventas]> describe ventas;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| idPaciente | int(11) | NO | MUL | NULL | |
| fecha | datetime | NO | | NULL | |
| total | decimal(7,2) | YES | | NULL | |
| vista | tinyint(1) | NO | | 0 | |
| idUsuario | bigint(20) unsigned | NO | | NULL | |
You can’t perform that action at this time.