Skip to content

Instantly share code, notes, and snippets.

@rampantmonkey
Last active December 11, 2015 03:39
Show Gist options
  • Save rampantmonkey/4539594 to your computer and use it in GitHub Desktop.
Save rampantmonkey/4539594 to your computer and use it in GitHub Desktop.
Create a vertically oriented histogram based on the concepts introduced in chapter 1 of "The C Programming Language"
#include <stdio.h>
int main() {
const int MAX_LENGTH = 20;
int hist_data[MAX_LENGTH] = {0};
char c='a';
int current_length = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t'){
hist_data[current_length]++;
current_length = 0;
}
else
current_length++;
}
int max = 0;
for(int j=0; j<MAX_LENGTH; j++)
if (hist_data[j] > max) max = hist_data[j];
for(int j=max; j > 0; j--) {
for(int k=0; k<MAX_LENGTH; k++) {
if (hist_data[k] < j)
printf(" ");
else
printf("-");
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment