2

I'm learning C with "The C Programming Language" book, and I'm trying to solve exercise 1.13:

"Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging."

I wrote the code, but when I press CTRL+Z (End-of-File), it shows all zeros instead of the length of words.

Could anyone give me a hint on where I'm going wrong?

#include <stdio.h>

/* print a histogram of the length of words from input */
main()
{
    int c, i, wordn, space;
    int lengthn[20];

    wordn = space = 0;
    for (i = 0; i < 20; ++i)
        lengthn[i] = 0;

    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\t' || c == '\n')
            if (space == 1) {
                ++wordn;
                space = 0;
                ++i;
            }
        if (c != ' ' && c != '\t' && c != '\n') {
            ++lengthn[i];
            space = 1;
        }
    }
    printf("Length: ");
    for (i = 0; i < 16; ++i)
        printf("%d   ", lengthn[i]);
    printf("\n        --------------------------------------------------------------\n");
    printf("Word:   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15\n");
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Have you tried adding `printf` statements to find out what your code is doing? Or stepping it through with a debugger? – Oliver Charlesworth Apr 30 '11 at 16:43
  • I think the problem is with the lengthn[i] array, it shows all zeros instead of the length of words. I just can't figure out what is the correct way to get it to show the length of the 1st, 2nd, 3rd word, etc... –  Apr 30 '11 at 17:04
  • It's probably a good idea to learn. But it's not necessary; like I suggested, you can just sprinkle your code with `printf`s that print the current value of variables, etc., to give you an idea of whether things are being updated as you expect. – Oliver Charlesworth Apr 30 '11 at 17:05

3 Answers3

2

(Because the OP is asking for hints, not the solution)

So ... what does i equal after this loop?

for (i = 0; i < 20; ++i)
    lengthn[i] = 0;

And where do you use it next?

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • After this loop, all the values in the array are initialized to 0, if I'm correct. And then I use it next in the printf statement at the end to print the values in the array. So that would mean that it will print all zeros when I press CTRL-Z. But in the while loop, I wrote code for what should i variable do if c is space, tab or newline, or if c is not space. If c is space, jump to the next array. If c is not space, increase value in the array by one. –  Apr 30 '11 at 17:35
  • @BlueSky ... (Because you wanted hints) - `i` ... not your array. Read the questions I pose above again ;) – Brian Roach Apr 30 '11 at 17:38
  • @BlueSky: Slightly bigger hint: What will `i` be when you first `++i` or `++lengthn[i]` inside the `while` loop? – mu is too short Apr 30 '11 at 17:45
  • @Brian Roach. You should of put that bold heading there earlier. Apologies. :) –  Apr 30 '11 at 17:57
  • No worries - I just tend to be overly sarcastic at times ;-P – Brian Roach Apr 30 '11 at 17:59
1
for (i = 0; i < 20; ++i)     
    lengthn[i] = 0;

The value of i after this loop will be i=20

so you must initialize i before while loop

agf
  • 171,228
  • 44
  • 289
  • 238
prof
  • 11
  • 1
0

I wrote a code for the vertical orientation. I'm new to C, so may be the code is not good.

#include <stdio.h>
#include <conio.h>

#define MAX_WORDS 100
#define IN 1
#define OUT 0

int maxlength(int length[], char num_of_word);

int main()
{
    char c,i,j,state,num_of_word;
    int length[MAX_WORDS];
    /*initialize length[]*/
        for(i=0;i<MAX_WORDS;i++){
        length[i]=0;
        }
    /* find the length of each word */
    num_of_word=0;
    while(num_of_word<MAX_WORDS && (c = getchar()) != EOF && c != 'a'){
        if(c != ' ' && c!= '\t' && c!= '\n'){
            state = IN;
            length[num_of_word]++;
        }else{
            if(state != OUT){
                state = OUT;
                num_of_word++;
            }
        }
    }
    /*   draw histogram            */
    for(i= maxlength(length[],num_of_word);i>0;i--){
        for(j=0;j<num_of_word;j++){
            if(length[j]<i){
                printf(" ");
            }else{
                printf("|");
            }
        }
        printf("\n");
    }
    /* print name of each column*/
    for(i=0;i<num_of_word;i++){
        printf("%d",i+1);
    }

    _getch();
    return(0);
}
/*sub-function that find the longest word */
int maxlength(int length[], char num_of_word){
    int i, max;
    max = length[0];
    for(i=1;i<num_of_word;i++){
        if(max<length[i]){
            max = length[i];
        }
    }
    return max;
}
Minh Do
  • 43
  • 7