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");
}
Name:
Anonymous2010-05-08 1:57
There's a reason people recommend reading SICP (http://mitpress.mit.edu/sicp/) as your first programming book, you know. K&R can wait.
Name:
Anonymous2010-05-08 2:01
After lurking this forum and /g/ I realized that and it's en route from Amazon as we speak. This book however came two weeks ago and it's fun so far.
I went to Borders and glanced with contempt at the various books offering instant mastery of a language, thinking, "/prog/ would shit bricks".
#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");
}
Name:
Anonymous2010-05-08 3:58
it's the most beautiful thing ever made, it's better than that time I took a two foot shit at work and didn't flush. Run it, see how the bars MATCH UP EXACTLY. Can you even believe something that perfect can happen on the command line? I can't. fucking. believe. it.
Name:
Anonymous2010-05-08 3:59
Except for that extra z variable; That's faggot shit.
>>9 CamelCase#Defines
No return type or arguments for main()
Unused variables
Unicode bar characters instead of some universal ascii character
Array zeroing with a loop instead of calloc
Confusing input method with no explanation (unless it's in the question)
Outputting spaces instead of tabs
Some other things probably
Mostly nitpicking, but whatever.
Name:
Anonymous2010-05-08 8:23
cleaned up your shitty code for you: #include <stdio.h>
#define MAXWORDLENGTH 9
#define MAXBARHEIGHT 15
int main(void)
{ int c, count = 0,
length[MAXWORDLENGTH] = {0};