Reputation: 300
I am very new to programming in general, so please bear with my lack of knowledge.
I have spent a couple of hours now on exercise 1-13. I finally decided to look up the answer, which I found at this link https://github.com/ccpalettes/the-c-programming-language-second-edition-solutions/blob/master/Chapter1/Exercise%201-13/word_length.c .
Because I didn't want to copy it completely for the sake of learning, I tried to understand the code and then remake it. (This resulted in almost a complete copy, but I understand it better than I would have otherwise.)
This is what I have so far:
#include <stdio.h>
#define IN 1
#define OUT 0
#define LARGEST 10
main()
{
int c, state, l, i, j;
int length[LARGEST + 1];
for (i = 0; i <= LARGEST; ++i)
length[i] = 0;
state = OUT;
while ((c = getchar()) != EOF) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if (state == OUT) {
l = 0;
state = IN;
}
++l;
}
else
if (state == IN) {
if (l <= LARGEST)
++length[l - 1];
//minus 1 because the nth term of an array is actually array[n-1]
else //if (l > LARGEST)
++length[LARGEST];
state = OUT;
}
if (c == EOF)
break;
}
for (i = 0; i <= LARGEST; ++i) {
if (i != LARGEST) //because array[10] refers to the 11th spot
printf("\t %2d |", i + 1); //plus one because 1st is array [0]
//this actually results in 1-10 because the 0-9 plus one makes the highest 10
else
printf("\t>%2d |", LARGEST);
for (j = 0; j < length[i]; ++j)
putchar('x');
putchar('\n');
}
return 0;
}
Please ignore my comments. They were meant for me, so that I could explain the program to myself.
I am having two issues that I just can't figure out, and they're driving me crazy:
The output always accounts for one word less than in the input, meaning "my name is not bob" results in:
...
2 |xx
3 |x
4 |x
...
Also, I don't understand what is going on at the end of the program. Specifically, I don't understand here why the variable j
is being used:
for (j = 0; j < length[i]; ++j)
putchar('x');
Thanks so much for your help, and I'm sorry if this is too beginner for the community.
Upvotes: 3
Views: 262
Reputation: 918
Well, trying to sum up all the answers since the question is not closed. First, we need to correct the main() line:
int main(void) {
...
return 0;
}
The int is necessary because you return a value at the end of the function, and the void means that the function doesn't receive any arguments.
I've copied your code and executed on my machine (Ubuntu 12.04) and it worked perfectly. Could you present some examples to generate the error?
As everybody said, j is just a variable to traverse the vector. length[i] is a vector that holds, in each position i the number of words with length i. For instance, if position 3 has a value of 4, e.g. length[3] = 4, it means that there are 4 words with length 3.
Finally, if I may, I'd like to give you a tip. It is good practice choosing meaningful names for your variables. The code you linked here helped me to understand what the program should do. Variable names such, length, or defines IN, OUT or LARGEST are too vague.
I hope this gather all answers until now and helped you even more.
Upvotes: 1