Computer Science Canada The C Programming Language. Ex1-13. HELP! |
Author: | 01001101 [ 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:
but the code does not seem to work. So some help would be appreciated. Thanks. |
Author: | Tony [ 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? |
Author: | bl0ckeduser [ 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:
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:
You wrote:
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:
You wrote:
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:
Here is the complete corrected program:
|
Author: | 01001101 [ 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:
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:
You wrote:
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:
You wrote:
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:
Here is the complete corrected program:
|