0

I completed the code for pset1 problem set 2 readability as follows

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

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

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


    // number of letters
    int n_let = count_letters(text);

    // number of words
    int n_words = count_words(text);

    // number of sentences
    int n_sent = count_sent(text);


    // averages
    float l = (100 * n_let) / n_words; // L = average number of letters per 100 words

    float s = (100 * n_sent) / n_words; // S = average number of sentences per 100 words

    // the actual index
    float index = (0.0588 * l) - (0.296 * s) - 15.8;

    //index = floor(index);
    // Output Logic
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index < 16)
    {
        printf("Grade %.0f\n", index);
    }
    else
    {
        printf("Grade 16+\n");
    }

    //printf("The actual index %i\n", index);

    //output = "Grade X"   "Before Grade 1"  "16+"
}


int count_letters(string text)
{
    int n = strlen(text);
    int counter = 0;
    for (int i = 0; i < n; i++)
    {
        char uc =text[i];
        char c = tolower(uc);
        if (isalpha(c))
        {
            counter += 1;
        }
        else
        {
            counter += 0;
        }
    }
    return counter;
}

int count_words(string text)
{
    int n = strlen(text);
    int counter = 1; // counter starts on 1 because at the end of the text there's no whitespace to count so the result will give one less word total.
    for (int i = 0; i < n; i++)
    {
        if (isspace(text[i]))
        {
            counter += 1;
        }
        else
        {
            counter += 0;
        }
    }
    return counter;
}

int count_sent(string text)
{
    int n = strlen(text);
    int count = 0;
    for (int i = 0; i < n; i++)
    {
        if (text[i] == '?' || text[i] == '.' || text[i] == '!')
        {
            count += 1;
        }
        else
        {
            count +=0;
        }
    }
    return count;
}

It works well in every case but the one graded to 7, When i run debug50 on it it seems that the "Coleman-Liau index" which in my code is the var index, is about 7.53 so it rounds up to 8 when outputing in. And i can't solve it, Ieven tryed to floor it with a math.h function but doing so will break the other answers. Can you help me please?

1

One of the hidden lessons of this pset is to teach the impact of integers and floats on calculations. Look at the following code:

float l = (100 * n_let) / n_words;

Each of the 3 numbers involved in the calculation is an int. Since all of them are integers, this code is performing integer division, not regular division. (It's not the only place.) This is introducing an error into the calculation. It also means that its capable of producing correct results in some cases and bad results in others.

To remedy this, the code needs to have a float in the calculations. You could change the 100 to 100.0. It would be better to cast one or both of the vars as a float. Any of these would force standard division instead of integer division.

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 .