Thread: Help understanding Histogram Word Count Program

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    12

    Help understanding Histogram Word Count Program

    Hello everyone, sorry I had to make a new thread, but I could not edit the last, and special thanks goes to Salem who showed me how to properly output the code on here.
    Code:
    #include <stdio.h>
    
    #define MAXWORDLEN 10
    
    
    int main(void)
    {
      int c;
      int inspace = 0;
      long lengtharr[MAXWORDLEN + 1];
      int wordlen = 0;
    
    
      int firstletter = 1;
      long thisval = 0;
      long maxval = 0;
      int thisidx = 0;
      int done = 0;
    
    
      for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
      {
        lengtharr[thisidx] = 0;
      }
    
    
      while(done == 0)
      {
        c = getchar();
    
    
        if(c == ' ' || c == '\t' || c == '\n' || c == EOF)
        {
          if(inspace == 0)
          {
            firstletter = 0;
            inspace = 1;
    
    
            if(wordlen <= MAXWORDLEN)
            {
              if(wordlen > 0)
              {
                thisval = ++lengtharr[wordlen - 1];
                if(thisval > maxval)
                {
                  maxval = thisval;
                }
              }
            }
            else
            {
              thisval = ++lengtharr[MAXWORDLEN];
              if(thisval > maxval)
              {
                maxval = thisval;
              }
            }
          }
          if(c == EOF)
          {
            done = 1;
          }
        }
        else
        {
          if(inspace == 1 || firstletter == 1)
          {
            wordlen = 0;
            firstletter = 0;
            inspace = 0;
          }
          ++wordlen;
        }
      }
    
    
      for(thisval = maxval; thisval > 0; thisval--)
      {
        printf("%4d  | ", thisval);
        for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
        {
          if(lengtharr[thisidx] >= thisval)
          {
            printf("*  ");
          }
          else
          {
            printf("   ");
          }
        }
        printf("\n");
      }
      printf("      +");
      for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
      {
        printf("---");
      }
      printf("\n       ");
      for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++)
      {
        printf("%2d ", thisidx + 1);
      }
      printf(">%d\n", MAXWORDLEN);
      getch();
    
    
      return 0;
    }
    Question 1: When the lengtharr[thisidx] is being initialized, does that make thisidx equal to zero or its just a first element?

    Question 2: Which part stores the word count in the array or is it being store at all?

    Question 3: what does
    Code:
    thisval = ++lengtharr[MAXWORDLEN];

    do, does it adds an element or what?

    The output of the code is on The C Programming Language Exercise 1-13

    Any help is greatly appreciated and thank you for your time in reading this lengthy post.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
      {
        lengtharr[thisidx] = 0;
      }
    A1 : Here. Every element of the array is set to zero.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    As for the Q2 you are not helping me enough so that I can answer (which I really do want to do).

    A3 :
    Code:
    thisval = ++lengtharr[MAXWORDLEN];
    is equivalent to
    Code:
    thisval = 1 + lengtharr[MAXWORDLEN];
    Notice that this is the last element of the lengtharr.
    Also notice that the ++ operator is preordered. Otherwise the variable thisval would hold only the value of the array, like this
    Code:
    thisval = lengtharr[MAXWORDLEN]++;
    |
    v
    Code:
    thisval = lengtharr[MAXWORDLEN];
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Thank you very much std10093!

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome piratemonkey!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding the word count program from K&R book
    By alter.ego in forum C Programming
    Replies: 11
    Last Post: 06-13-2011, 04:55 AM
  2. Unique Word count program
    By vjefcoat in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 06:51 PM
  3. Word count program
    By Jpeg6 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 10:34 PM
  4. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM