Thread: Write a histogram of word length (K&R 1.13 assignment)

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    1

    Write a histogram of word length (K&R 1.13 assignment)

    Hello everybody,

    without any prior knowledge of programming, I have started to learn C thru a book "C Programming Language" by K&R.
    While I have followed it with ease up until now, I am stuck at the 1.13 assignment where I have to print a vertical histogram of word length.

    I have found the solution on the web, but have hard time figuring out what it really does. Here is the code.
    Code:
    #include <stdio.h>
    #define MAXWL 20
    #define MAXWN 30
    int main()
    {
        int word[MAXWN];            // defines the array of 30 values (max word number)
        int i, c, j, nc, nw;
        
        for (i=0; i < MAXWN; ++i)
            word[i]=0;              // sets all values of array to 0
        
        nw = nc = 0;                // sets counters to 0
        
        while ((c=getchar()) != EOF){
            ++nc;                   // counts characters
            if ( c == ' ' || c == '\t' || c == '\n'){
                word[nw] = nc -1;   // if white space, subtract 1 from counter value
                ++nw;               // than add that value to array position (first 0, than 1 etc.)
                nc = 0;             // reset character counter
            }
        }
        
        for (i=MAXWL; i>=1; --i){   // this prints the histogram
            for (j=0; j<= nw; ++j){ // but how it does it
                if (i<=word[j])     // is beyond me....
                    putchar('*');   
                else
                    putchar(' ');
            }
            putchar('\n');        
        }
        return 0;
    }
    Could someone walk me thru the mechanics of the last part of the code so I can understand the process and continue my studies?
    Thank you

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Since nobody answered this, let me do the honors, if you're still around:

    That last part of the code prints the histogram lines vertically. In order to do that though, because standard output prints horizontally first, it deploys a little trick.

    It can't help but print one line after another, sequentially. It makes the outer for loop go backwards(from MAXWL to 1 with step -1), and in the inner loop it checks if the current histogram line should be printed on that "pixel" or not. Think of it this way. The horizontal method is like drawing lines from left to right representative of the data, while the vertical method is like making a rubbing of the underlying data, by "rubbing" one line at a time.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Word Length Statistics
    By tobig in forum C Programming
    Replies: 2
    Last Post: 10-25-2014, 12:20 PM
  2. Help understanding Histogram Word Count Program
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 01-18-2013, 05:14 AM
  3. problem in histogram word
    By elwad in forum C Programming
    Replies: 1
    Last Post: 04-18-2009, 09:55 AM
  4. changing word length
    By mltngpot in forum C++ Programming
    Replies: 7
    Last Post: 01-21-2006, 09:55 AM
  5. Replies: 5
    Last Post: 09-28-2004, 12:38 PM