Skip to content

Instantly share code, notes, and snippets.

@MrDave1999
Last active February 24, 2021 18:51
Show Gist options
  • Save MrDave1999/c79dffc18a621ff0d9d24e55661ad5a1 to your computer and use it in GitHub Desktop.
Save MrDave1999/c79dffc18a621ff0d9d24e55661ad5a1 to your computer and use it in GitHub Desktop.
Algoritmo básico para imprimir un histograma de forma vertical.
#include <stdio.h>
int main(void)
{
int array[] = {1, 5, 8, 0, 12, 13, 1, 2, 10, 7};
int len = sizeof array / sizeof *array;
const int mayor = array[5];
for(int line_current = 1; line_current <= mayor; ++line_current)
{
for(int j = 0; j < len; ++j)
{
int line = (mayor + 1) - array[j];
printf("%-5s", line_current >= line ? "*" : " ");
}
printf("\n");
}
for(int i = 0; i < len; i++)
printf("%-5d", array[i]);
printf("\n");
return 0;
}
/*
Resultado en pantalla:
*
* *
* *
* * *
* * *
* * * *
* * * * *
* * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * * *
* * * * * * * * *
1 5 8 0 12 13 1 2 10 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment