Thread: K&R Exercise 1-14

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    K&R Exercise 1-14

    Hi all,

    I hope you dont mind me asking such a simple question but anyway....

    I am working through K&R and I am having a bit of a problem with exercise 1-14 in the Arrays section (1.6). I am doing ok I think. Anyhow, for some reason my program seems to be counting the new line character, the spaces and the tab character instead of just skipping them. I think I understand where the problem is (the if statement inside the getchar while loop) but the problem is that I am not sure why that statement is not working in preventing the newlines etc from being counted. Heres what I have so far:

    Code:
    #include <stdio.h>
    
    #define NUM_CHARS 256
    
    /* Prints a histogram of the frequencies of different characters in its input */
    
    int main(void)
    {
        int c, i, j, max;
        int charFreq[NUM_CHARS];
        
        max = 0;
        
        for (i = 0; i < NUM_CHARS; ++i)
            charFreq[i] = 0;
            
        while ((c = getchar()) != EOF)
        {
              if (c != ' ' || c != '\n' || c != '\t')
              {
                 ++charFreq[c];
                 if (charFreq[c] >= max)
                    max = charFreq[c];
              }
        }
        
        for (j = max; j >= 1; --j)
        {
            printf(" %d ", j);
            for (i = 0; i < NUM_CHARS; ++i)
            {
                if (charFreq[i] != 0 && j <= charFreq[i])
                   printf("  x  ");
                else if (charFreq[i] != 0 && j > charFreq[i])
                    printf("     ");
            }
            printf("\n");
        }
        
        printf("   ");
        
        for (i = 0; i < NUM_CHARS; ++i)
        {
            
            if (charFreq[i] != 0 && j <= charFreq[i])
            {
               if (i > 100)
                  printf(" %d ", i);
               if (i < 100 && i >= 10)
                  printf(" 0%d ", i);
               if (i < 10)
                  printf(" 00%d ", i);  
                
            }   
            
        }
        printf("\n");
    
        return(0);   
    }
    Please excuse the dodgy histogram display code, I just quickly hacked that in so I could see what was going on but I will go back and tidy it up after I have sorted this problem.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Code:
              if (c != ' ' || c != '\n' || c != '\t')
    This is your problem line.
    Say for example the character you get for c is a space. In that case it goes through and sees that c is equal to a space, but it then sees that it is not equal to a newline or for that matter a tab. Because of this it execute the if statement anyway. To resolve this problem you need to use && instead of ||.
    Code:
              if (c != ' ' && c != '\n' && c != '\t')

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is always true.
    Code:
    if (c != ' ' || c != '\n' || c != '\t')
    Perhaps you meant this.
    Code:
    if (c != ' ' && c != '\n' && c != '\t')
    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.*

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Ahh, thanks very much guys. Now I see why it didnt work as I originally expected it to. Its funny how you can stare at something for so long and when you find the solution to your problem (or someone else does) you really kick yourself for not spotting it sooner, hehe.

    Thanks again

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, 01:24 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM
  5. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM