3

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:

  1. 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
    ...
    
  2. 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.

jhschwartz
  • 168
  • 1
  • 2
  • 13
  • 3
    For your question #2, it's what prints the `x` and `xx` in your sample output. That isn't hard to figure out - look at what `putchar()` is being told to output (`x`) and look at your output to see where the `x` is appearing. For question #1, did you try using a debugger to step through the code to see what it's doing and where it's going wrong? – Ken White Oct 31 '14 at 23:45
  • 3
    For question #1 - alternatively to using a debugger, try adding extra `printf` statements to log the state of variables at various points in your code. Then you can compare the output against your expectation of how the code should work. This can be an extremely effective tool for debugging code. – James Oct 31 '14 at 23:59
  • 3
    `j` is a simple integer counter, that is repeating from `0 to `length[i]` times, incrementing (`++j`) each time through the loop. This is basic `for` loop syntax. On the first pass, `j == 0`, on the next pass `j == 1`, on the next pass `j == 2`, etc., until `j == length[i]`, at which point the loop stops. Review your lessons on loops, particularly `for`. – Ken White Nov 01 '14 at 00:00
  • 1
    @jhschwartz: Actually the question is well formatted, still it lacks the actual thingy. What exactly is the question, though you did told, what is the answer, and what exactly went wrong? Where the expected output, doesn't matches with the output? But exactly is the real cause to write this answer, is still missing in the question. Either someone has to read the whole program, to understand, what is happening/One can simply write the problem down, too, then one can easily tell, what logic has been implemented, to solve the problem :-) – nIcE cOw Nov 01 '14 at 05:31

1 Answers1

1

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.

  1. 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?

  2. 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.

delirium
  • 868
  • 6
  • 20