Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Created April 7, 2015 15:03
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 thomaswilburn/1c36481715d64615d89e to your computer and use it in GitHub Desktop.
Save thomaswilburn/1c36481715d64615d89e to your computer and use it in GitHub Desktop.
K&R Exercise 1-13
#include <stdio.h>
#define true 1
#define false 0
#define LEN 30
void main()
{
int c, length, highest;
int counts[LEN];
highest = 0;
//zero the array
for (int i = 0; i < LEN; i++) counts[i] = 0;
//build count array
while ((c = getchar()) != EOF) {
if (c == ' ') {
if (length) {
// printf("Increasing %d by one\n", length);
counts[length]++;
if (highest < counts[length]) highest = counts[length];
length = 0;
}
} else {
length++;
}
}
//print histogram
for (int i = highest; i >= 0; i--) {
putchar('\n');
for (int j = 0; j < LEN; j++) {
// printf("Length: %d, count: %d\n", j, counts[j]);
if (counts[j] >= i) {
if (i)
putchar('#');
else
putchar('=');
} else {
putchar(' ');
}
}
}
putchar('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment