Oct 2, 2015

K & R : Exercise 1.13 - Horizontal histogram [Part 1]

Problem Statement:

Exercise 1-13: Write a program to print a histogram of the lengths of words in input. It is easy to draw the histogram with bars horizontal; a vertical orientation is more challenging.



Solution:

Lets first do the horizontal histogram. We need to find two things
1. length of each word
2. and count it

Find length would be simple, we need to count all the non space characters till we hit a space. We will use the state variable which will use IN and OUT to count characters in a letter.

For counting we would require a array variable such that
count[1] -> will store count the words with length 1
count[2] -> will store count the words with length 2
count[3] -> will store count the words with length 3

Putting all the above thoughts into a program



#include <stdio.h>

/* intenal states to count words */
#define IN 1
#define OUT 0

#define MAX_WORD_LEN 20

int main()
{
  int count[MAX_WORD_LEN] = {0};
  int c, len = 0;
  int state = OUT;
  int i = 0, j = 0;

  while((c = getchar()) != EOF) {
    if(c == ' ' || c == '\t' || c == '\n') {
      if(state == IN && len >= 1 &&
         len < MAX_WORD_LEN) {
           count[len]++;
      }
      len = 0;
      state = OUT;
    }else if(state == OUT) {
      state = IN;
      len++;
    }else {
      len++;
    }
  }

  printf("\n");
  for(i = 1; i < MAX_WORD_LEN; i++) {
    printf("%2d | ", i);
    for(j = 0; j < count[i]; j++)
      printf("*");
    printf("\n");
  }
 
  return 0;
}
Output of above program is

$ cat mary
Mary had a little lamb
Little lamb, little lamb
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
Mary went, Mary went
Everywhere that Mary went
The lamb was sure to go

He followed her to school one day
School one day, school one day
He followed her to school one day
Which was against the rule
It made the children laugh and play
Laugh and play, laugh and play
It made the children laugh and play
To see a lamb at school

And so the teacher turned him out
Turned him out, turned him out
And so the teacher turned him out
But still he lingered near
And waited patiently
Patiently, patiently
And wai-aited patiently
Til Mary did appear

Mary had a little lamb
Little lamb, little lamb
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
Mary went, Mary went
Everywhere that Mary went
The lamb was sure to go
$ ./a.out < mary

 1 | *****
 2 | *****************
 3 | ************************************************
 4 | *********************************************
 5 | *************
 6 | *********************
 7 | ***
 8 | *****
 9 | ****
10 | *****
11 | 
12 | 
13 | 
14 | 
15 | 
16 | 
17 | 
18 | 
19 | 
$ 


Next Article - K & R : Exercise 1.13 - Vertical histogram [Part 2]
Previous Article - K & R : Exercise 1.12 - Print word per line
All Article - K & R Answers

2 comments :

  1. We can get the same result without STATEs, why is it neccesary?

    ReplyDelete
  2. We can get the same Histogram without STATE. Why is it neccesary?

    ReplyDelete