Last active
December 11, 2015 03:39
-
-
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"
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> | |
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