Name: (for certain values of first) 2010-05-08 1:52
I'm reading The C Programming Language (K&R) as my first programming book. Here's my answer to exercise 1-13 (p-24). It makes me happy-in-the-pants to have done it, but it hurts my head to think about.
#include <stdio.h>
#define MaxWordLength 9 /*the maximum wordsize to track*/
#define MaxBarHeight 15 /*the maximum height of the bar graph*/
/* To display this program properly your console encoding must be set to unicode-8 */
main()
{
int c, i, count, bars, line, z;
int length[MaxWordLength];
c = bars = count = 0;
for (i = 0; i <= MaxWordLength; ++i)
length[i] = 0;
while ((c = getchar()) != EOF) {
if (c == '\n' || c == ' ' || c == '\t'){
++length[count];
count = 0;
}
else ++count;
}
for (line = MaxBarHeight; line > 0; --line){
for (i = 1; i <= MaxWordLength; ++i){
if (bars == MaxWordLength) {
printf("\n"); bars = 0;
}
if (length[i] < line)
printf(" ");
else printf(" █████");
++bars;
}
}
printf("\n one two three four five six seven eight nine \n");
}
#include <stdio.h>
#define MaxWordLength 9 /*the maximum wordsize to track*/
#define MaxBarHeight 15 /*the maximum height of the bar graph*/
/* To display this program properly your console encoding must be set to unicode-8 */
main()
{
int c, i, count, bars, line, z;
int length[MaxWordLength];
c = bars = count = 0;
for (i = 0; i <= MaxWordLength; ++i)
length[i] = 0;
while ((c = getchar()) != EOF) {
if (c == '\n' || c == ' ' || c == '\t'){
++length[count];
count = 0;
}
else ++count;
}
for (line = MaxBarHeight; line > 0; --line){
for (i = 1; i <= MaxWordLength; ++i){
if (bars == MaxWordLength) {
printf("\n"); bars = 0;
}
if (length[i] < line)
printf(" ");
else printf(" █████");
++bars;
}
}
printf("\n one two three four five six seven eight nine \n");
}