Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

my first program!

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: Anonymous 2010-05-08 8:46

>>11

#include <stdio.h>
#include <ctype.h>

#define MAXWORDLENGTH 9
#define MAXBARHEIGHT 15

int main(void)
{ int length[MAXWORDLENGTH] = {0};

  for(int c, count = 0; (c = getchar()) != EOF;)
    if(isspace(c))
    { ++length[count];
      count = 0; }
    else ++count;

  for(int line = MAXBARHEIGHT, bars = 0; line; --line)
    for(int i = 1; i <= MAXWORDLENGTH; fputs(length[i++] < line ? "       " : "  █████", stdout))
      if(bars++ == MAXWORDLENGTH) bars = putchar('\n');

  return puts("\n   one    two   three  four   five    six   seven  eight   nine "); }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List