Help with K&R Exercise 1.13

This is a discussion on Help with K&R Exercise 1.13 within the C Programming forums, part of the General Programming Boards category; Hello, I just started delving into C and programming in general a few days ago with K&R and I'm struggling ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    1

    Help with K&R Exercise 1.13

    Hello,

    I just started delving into C and programming in general a few days ago with K&R and I'm struggling with exercise 1.13 (The Histogram). A bit too early to struggle eh? :P

    Instead of horizontal asterisks I only get '0' which is the printlen[] initialization. I'm guessing I can't add characters the way I imagined?

    This is what I've come up with so far and it compiles fine as is. Not sure if my thought process is correct here. I hope I'm on the right path at least.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	int printlen[10];
    	int clen = 0;
    	int c = 0;
    	
    	for(clen = 0; clen <= 10; ++clen) /* Initialize printlen array */
    		printlen[clen] = 0;
    			
    
    	while ((c = getchar()) != EOF) /* Loop until EOF */
    	{
    		if ( c != ' ' || c != '\n' || c != '\t') /* Check start and end of word */
    			++clen;
    			
    		printlen[clen] = '*' + printlen[clen]; /* Count '*' */
    	}									
    	
    	printf("Length\n");
    	for(clen = 0; clen < 10; ++clen)
    		printf("  %d\t %c\n", clen, printlen[clen]);
    	printf("  >10\t %c\n", printlen[clen]);
    
    	return 0;
    }
    What I am guessing is going wrong is that it's not adding the '*' to the initialization of printlen[] in this line:

    printlen[clen] = '*' + printlen[clen];

    If I can get passed that I should be able to fix all the other 'bugs' in here.

    Thanks for any advice on the matter.

    Yannis

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >for(clen = 0; clen <= 10; ++clen) /* Initialize printlen array */
    > printlen[clen] = 0;

    You are going one beyond the end of the array.

    >if ( c != ' ' || c != '\n' || c != '\t') /* Check start and end of word */

    This is always true.

    >printlen[clen] = '*' + printlen[clen]; /* Count '*' */

    It looks like you are attempting to do string concatentation with the above code; this is not the way to do it. You don't need to do string concatenation to solve this problem.

    For additional help, take a look at this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    You have the right idea:
    Code:
    #include <stdio.h>
    
    void printhist(int lengths[]) {
      int i;
      int j;
    
      for (i = 0; i < 20; i++) {
        printf("%d\t", i+1);
        for (j = 0; j < lengths[i]; j++)
          putchar('*');
        putchar('\n');
      }
    }
    
    int main ( ) {
      int lengths[20] = {0}; /* Up to 20 characters */
      int currlen;           /* Current word length */
      int c;                 /* Current char taken from the stream */
    
      currlen = 0;
      while ((c = getchar()) != EOF) {
        if (c == '\n' || c == ' ' || c == '\t') {
          lengths[currlen-1]++;
          currlen = 0;
        }
        else
          currlen++;
      }
      printhist(lengths);
    
      return 0;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 02:24 PM
  2. Exercise 2-6 K&R help
    By allix in forum C Programming
    Replies: 19
    Last Post: 08-18-2006, 10:25 AM
  3. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. k&r exercise 1-9
    By xion in forum C Programming
    Replies: 14
    Last Post: 07-15-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21