0

I've coded this solution for the readability exercise of CS50 Week 2, but after three long days I really don't see where I went wrong. It compiles and calculates the right number of words, sentences and letters, but its not getting the grade :c

#include <stdio.h>
#include <string.h>
#include <math.h>

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
    // Get text
    string text = get_string ("Text: ");

    // Apply functions that measure letters, words and sentences
    int letters = count_letters(text);
    int words = count_words(text);
    int sentences = count_sentences(text);

    //Calculate parameters and apply formula
    float L = 100.0 * (letters / words);
    float S = 100.0 * (sentences / words);

    int index = round(0.0588 * L - 0.296 * S -15.8);

    //Show results
    if (index > 16)
    {
        printf("Grade 16+\n");
    }
    else if (index < 1)
    {
        printf("Before grade 1\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }

}

//Count letters
int count_letters(string text)
{
    int l = strlen(text);
    int s = 0;

    //For function to count through the string
    for (int i = 0; i < l; i++)
    {
        //If to detect the spaces
        if (text[i] == ' ')
        {
            s++;
        }
    }

    //Spaces less total count
    l -= s;

    return l;
}

//Count words
int count_words(string text)
{
    int wc = 1;

    //Same logic as before, but we use the spaces (ASCII 32) to count a new word
    for (int i = 0, l = strlen(text); i < l; i++)
    {
        if (text[i] == 32)
        {
            wc++;
        }
    }

    return wc;
}

//Count sentences
int count_sentences(string text)
{
    int wc = 0;

    //Same logic as the words, but considering more parameters
    for (int i = 0, l = strlen(text); i < l; i++)
    {
        if (text[i] == '.'|| text[i] == '?'||text[i] == '!')
        {
            wc++;
        }
    }

    return wc;
} ```

1 Answer 1

2

Let me guess. The answers are sometimes right but sometimes off by 1?

Look at these lines of code:

//Calculate parameters and apply formula
float L = 100.0 * (letters / words);
float S = 100.0 * (sentences / words);

Now, think about how the operations are done. The divisions inside the parentheses will be done first. Finally, think carefully about the data types involved. letters, words and sentences are all int data types. That means that the code is doing integer division, not regular division.

Integer division will start by doing a standard division and then will truncate the fractional part of the result, producing another integer. It doesn't round up or down. It just cuts the decimal off!

The fix is to force a float into the calculation by casting one of the int types as a float, or by making sure that there's a float in the operation, in order to prevent the code from doing an integer division operation.

For example, `100.0 * ( letters / (float) words );

You could also remove the parentheses in the code so that processing moves from left to right and starts with the float 100.0.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .