Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The C Programming Language. Ex1-13. HELP!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
01001101




PostPosted: Fri May 04, 2012 6:38 pm   Post subject: The C Programming Language. Ex1-13. HELP!

Ex1-13 of The C Programming Language.
Write a program to print a histogram of the lengths of words in its input.

Kernighan, B. W., and D. M. Ritchie. The C Programming Language. 2nd ed. New Jersey: PTR Prentice Hall, 1988. Print.

This is the code I wrote so far:
code:

#include <stdio.h>
#define MAX 10
#define IN 1
#define OUT 0

main()
{
        int wordlens[MAX];
        int c, i, count, state;

        count = 0;
        /*Zero out the array*/
        for (i = 0; i < MAX; ++i)
                wordlens[i] = 0;
       
        state = OUT;
        while((c = getchar()) != EOF){
                if (c != ' ' || '\n' || '\t'){
                        state = OUT;
                        count = 0;
                }
                else if(state == OUT){
                        state = IN;
                        ++count;                       
                        ++wordlens[count];
                }
        printf("digits =");
                for (i = 0; i < MAX; ++i)
                        printf(" %d\n", wordlens[i]);
        }
       
}


but the code does not seem to work. So some help would be appreciated. Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri May 04, 2012 7:09 pm   Post subject: RE:The C Programming Language. Ex1-13. HELP!

in which way does it not seem to work?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
bl0ckeduser




PostPosted: Fri May 04, 2012 7:16 pm   Post subject: Re: The C Programming Language. Ex1-13. HELP!

Hi, I'm not an expert, but I've been writing C for about 5 years as hobby.
I hope my comments help.

You wrote:
code:

                if (c != ' ' || '\n' || '\t'){


It doesn't work that way. || takes a boolean value on either side;
'\n' alone will be interpreted as true if it is not equal to zero
and as false if it is equal to zero. The expression c == '\n'
will be true if c is equal to '\n'. So you should write, for example:
code:

                if (c == ' ' || c == '\n' || c == '\t'){


You wrote:
code:

                if (c != ' ' || '\n' || '\t'){
                        state = OUT;
                        count = 0;
                }
                else if(state == OUT){
                        state = IN;
                        ++count;                       
                        ++wordlens[count];
                }


1. In the "else if" part, you are incrementing both the array index (count) and the data at the
incremented index in the array (wordlens[count]). That doesn't make any sense.
I would suggest just using i as an index variable and incrementing that whenever
a new word is encountered.

2. You only are incrementing the count when new words are found.
The count should be increment for every character in every word.

3. I do not understand why you set count = 0 when new words are found,
as all the counts have already been zeroed, and what you must really do is
increment the word index so you can start counting characters in the new
word.

So I would write:

code:

                if (c == ' ' || c == '\n' || c == '\t'){
                        state = OUT;
                }
                else if(state == OUT){
                        state = IN;
                        i++;
                        wordlens[i] = 0;
                }

                if(state == IN)
                        wordlens[i]++;


You wrote:
code:

        printf("digits =");
                for (i = 0; i < MAX; ++i)
                        printf(" %d\n", wordlens[i]);
        }  /* closes while((c = getchar()) != EOF){ */


Since the for-loop is inside the while(getchar()) loop,
that for every character of input, the program will print its whole
stash of character-in-word counts. Furthermore, it will print all the way to i = MAX,
even if far fewer words are read in. What I would suggest is to take that code
out of the while, and only have it print up to the index variable (i, as I suggested
earlier). Of course to do that a new index variable (say j) must be declared.
So you could write:

code:

for (j = 0; j <= i; ++j)
                        printf("%d\n", wordlens[j]);


Here is the complete corrected program:
code:

#include <stdio.h>
#define MAX 10
#define IN 1
#define OUT 0

main()
{
        int wordlens[MAX];
        int c, i, state;
        int j;

        /* zero out the array */
        for (i = 0; i < MAX; ++i)
                wordlens[i] = 0;

        state = IN;
        i = 0;
        while((c = getchar()) != EOF){
                if (c == ' ' || c == '\n' || c == '\t'){
                        state = OUT;
                }
                else if(state == OUT){
                        state = IN;
                        i++;
                        wordlens[i] = 0;
                }

                if(state == IN)
                        wordlens[i]++;
        }

        for (j = 0; j <= i; ++j)
             printf("%d\n", wordlens[j]);
}
01001101




PostPosted: Fri May 04, 2012 8:58 pm   Post subject: Re: The C Programming Language. Ex1-13. HELP!

Thank you so very much with the help. Spent ages trying to figure things out.


bl0ckeduser @ Fri May 04, 2012 7:16 pm wrote:
Hi, I'm not an expert, but I've been writing C for about 5 years as hobby.
I hope my comments help.

You wrote:
code:

                if (c != ' ' || '\n' || '\t'){


It doesn't work that way. || takes a boolean value on either side;
'\n' alone will be interpreted as true if it is not equal to zero
and as false if it is equal to zero. The expression c == '\n'
will be true if c is equal to '\n'. So you should write, for example:
code:

                if (c == ' ' || c == '\n' || c == '\t'){


You wrote:
code:

                if (c != ' ' || '\n' || '\t'){
                        state = OUT;
                        count = 0;
                }
                else if(state == OUT){
                        state = IN;
                        ++count;                       
                        ++wordlens[count];
                }


1. In the "else if" part, you are incrementing both the array index (count) and the data at the
incremented index in the array (wordlens[count]). That doesn't make any sense.
I would suggest just using i as an index variable and incrementing that whenever
a new word is encountered.

2. You only are incrementing the count when new words are found.
The count should be increment for every character in every word.

3. I do not understand why you set count = 0 when new words are found,
as all the counts have already been zeroed, and what you must really do is
increment the word index so you can start counting characters in the new
word.

So I would write:

code:

                if (c == ' ' || c == '\n' || c == '\t'){
                        state = OUT;
                }
                else if(state == OUT){
                        state = IN;
                        i++;
                        wordlens[i] = 0;
                }

                if(state == IN)
                        wordlens[i]++;


You wrote:
code:

        printf("digits =");
                for (i = 0; i < MAX; ++i)
                        printf(" %d\n", wordlens[i]);
        }  /* closes while((c = getchar()) != EOF){ */


Since the for-loop is inside the while(getchar()) loop,
that for every character of input, the program will print its whole
stash of character-in-word counts. Furthermore, it will print all the way to i = MAX,
even if far fewer words are read in. What I would suggest is to take that code
out of the while, and only have it print up to the index variable (i, as I suggested
earlier). Of course to do that a new index variable (say j) must be declared.
So you could write:

code:

for (j = 0; j <= i; ++j)
                        printf("%d\n", wordlens[j]);


Here is the complete corrected program:
code:

#include <stdio.h>
#define MAX 10
#define IN 1
#define OUT 0

main()
{
        int wordlens[MAX];
        int c, i, state;
        int j;

        /* zero out the array */
        for (i = 0; i < MAX; ++i)
                wordlens[i] = 0;

        state = IN;
        i = 0;
        while((c = getchar()) != EOF){
                if (c == ' ' || c == '\n' || c == '\t'){
                        state = OUT;
                }
                else if(state == OUT){
                        state = IN;
                        i++;
                        wordlens[i] = 0;
                }

                if(state == IN)
                        wordlens[i]++;
        }

        for (j = 0; j <= i; ++j)
             printf("%d\n", wordlens[j]);
}
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: