0

I coded this solution myself, but I really can't figure out where I did wrong, I've spent days on this, and I would really appreciate any help!! Please, 😭

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


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

int main(void)
{

    // getting user imput
    string text = get_string("Text: ");


    //show the number of letters in the text
    int countL = count_letters(text);


    //show the number of words in the text
    int countW = count_words(text);


    //show the number of sentences in the text
    int countS = count_sentences(text);


    //Grade Index Calculation (index = 0.0588 * L - 0.296 * S - 15.8)
    // L is average number of letter in 100 words (countL/countW * 100)
    // S is average number of sentence in 100 words (countS/countW * 100)
    int index = rintf(0.0588 * (countL / countW * 100) - 0.296 * (countS / countW * 100) - 15.8);
    // Situation when the index is lower than 1 and higher than 16+
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }


}


int count_letters(string text)
    // to count the letters in the text
{
    int countL = 0;

    for (int i = 0, n = strlen(text); i < n; i++)
    {
        // if the alphabets between a-z (97->122) and A-Z (65->90)
        if ((text[i] >= 97 && text[i] <= 122) || (text[i] >= 65 && text[i] <= 90))
        {
            countL++;
        }
    }
    return countL;
}


int count_words(string text)
    // to count the words in the text
{
    int countW = 1;

    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (text[i] == 32)
        {
            countW++;
        }
    }
    return countW;
}


int count_sentences(string text)
    // to count the sentences in the word
{
    int countS = 0;

    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (text[i] == 46 || text[i] == 33 || text[i] == 63)
        {
            countS++;
        }
    }
    return countS;
}

1 Answer 1

0

I can understand why you're having trouble fixing this. The good news is that it's an easy fix once you know what to look at.

Here's a really, really big hint:

In c programming, what's the difference between regular division and integer division?????

Next hint: what are the precedent orders of calculations in a calculation with many operations?

If you can't sort it out, leave a comment after my answer. ;-)

[Edit] The issue lies here:

int index = rintf(0.0588 * (countL / countW * 100) - 0.296 * (countS / countW * 100) - 15.8);

The problem is that this line of code is doing integer division, not regular division!

Here's the difference. Integer division will do the calculation and truncate the result to produce an integer result. For example, in integer division, ( 8 / 3 ) = 2.

Now let's look at part of the line of code above:

0.0588 * (countL / countW * 100)

Consider the order that operations are done. The code is going to do the stuff inside the parentheses. Since it's all multiplication and "division", it will proceed from left to right. So, the first thing that's done is countL / countW. Since both are integers, the system does integer division. Everything else is downhill from here.

There are a number of ways to force regular division. They all boil down to making sure there's a float in the operation. You could cast one of the operands as a float,

( (float) countL / countW ....)

or you could do something to insert a float into the calculation.

(100.0 * countL / countW)

In this case, note that it's 100.0, a float, and not 100, an integer. The ".0" makes the difference. Since 100.0 is a float, the calculation goes like this. 100.0 * countL is done first and the interim result is a float. That float is used in the division, forcing regular division, so the integer division is no longer happening and the problem goes away.

That's the story. The lesson is that anytime you're doing division, you need to be alert for integer division and whether you want it or not.

1
  • Oh! Thank you so much for your hints, I really appreciate that. But I'm sorry I still can't solve the problem myself, would you mind tell me what is wrong with it?
    – az ab
    Commented Jun 29, 2022 at 13:57

You must log in to answer this question.

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