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

Pages: 1-

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 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: Anonymous 2010-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".

Name: Anonymous 2010-05-08 3:47

Okay, so, [code] tags.

Name: Anonymous 2010-05-08 3:54

#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 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: Anonymous 2010-05-08 3:59

Except for that extra z variable; That's faggot shit.

Name: Anonymous 2010-05-08 6:18

my first code tags

Name: Anonymous 2010-05-08 7:51

>>8
at least insult the program you useless twat

Name: Anonymous 2010-05-08 8:03

>>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: Anonymous 2010-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};

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

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

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

Name: Anonymous 2010-05-08 8:26

>>11

sweet thanks

Name: Anonymous 2010-05-08 8:30

>>11
Pro indentation bro.

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 "); }

Name: Anonymous 2010-05-08 9:04

>>14
forgot to fix some errors:
#include <stdio.h>
#include <ctype.h>

#define MAXWORDLENGTH 9
#define MAXBARHEIGHT 15

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

  for(int c, count = 0; (c = getchar()) != EOF;)
    if(isspace(c))
    { ++length[count];
      count = 0; }
    else if(++count > MAXWORDLENGTH) return !fputs("FUCK YOU, ASSHOLE.", stderr);

  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 "); }

Name: Anonymous 2010-05-08 11:00

>>11,14,15
PIG DISGUSTING

Name: sage 2010-05-10 2:41

[quote]asd[/quote]

Name: ​​​​​​​​​​ 2010-10-26 11:12

Name: Anonymous 2010-12-17 1:32

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

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