What are the last lines of code performed in Exercise 1-13 of the K&R C Programming Language?

I am very new to programming in general, so please carry my knowledge.

I spent a couple of hours doing exercise 1-13. Finally, I decided to find the answer that 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 .

Since I didn't want to copy it completely for the sake of learning, I tried to understand the code and redo it. (This resulted in an almost complete copy, but I understand this better than 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 I could explain the program to myself.

I have two problems that I just cannot figure out and they are driving me crazy:

  • The output always counts one less word than the input, which means "my name is not bob" results in:

    ...
    2 |xx
    3 |x
    4 |x
    ...
    
          

  • Also, I don't understand what's going on at the end of the program. In particular, I don't understand here why the variable is being used j

    :

     for (j = 0; j < length[i]; ++j)
         putchar('x');
    
          

Thanks a lot for your help and I'm sorry if this is too new to the community.

+3


source to share


1 answer


Ok, trying to summarize all the answers since the question is not closed. First, we need to fix the main () line:

    int main(void) {
    ...
    return 0;
    }

      

int is necessary because you are returning a value at the end of the function, and void means the function does not receive any arguments.



  • I copied your code and ran it on my machine (Ubuntu 12.04) and it worked fine. Could you give some examples for generating an error?

  • As everyone said, j is just a variable to move the vector. length [i] is a vector that contains at each position i the number of words with length i . For example, if position 3 is 4, for example. length [3] = 4, which means there are 4 words with length 3.

Finally, if possible, I would like to give you some advice. It is good practice to choose meaningful names for your variables. The code you linked here helped me figure out what the program should do. Variable names, lengths, or IN, OUT, or LARGEST definitions are too vague.

I hope these are all the answers so far and have helped you even more.

+1


source







All Articles