0

LINK TO ASSIGNMENT: https://cs50.harvard.edu/x/2024/psets/2/readability/ heres my code for the CS50x Readability assignment

for an input of "Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard." an output of "Grade 5" is needed...

but i keep geting "Grade 4", since it rounds down but i cant manipulate it without breeaking another case output needed...

an help would be appreciated

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

// A number of “readability tests” have been developed over the years that define formulas for
// computing the reading level of a text. One such readability test is the Coleman-Liau index. The
// Coleman-Liau index of a text is designed to output that (U.S.) grade level that is needed to
// understand some text. The formula is

// index = 0.0588 * L - 0.296 * S - 15.8
// where L is the average number of letters per 100 words in the text, and S is the average number
// of sentences per 100 words in the text.

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

    char currentcharacter = 'A';

    int number_of_periods = 0;
    int number_of_spaces = 0;
    int number_of_letters = 0;
    int number_of_words = 0;
    int number_of_sentences = 0;
    int number_of_characters = 0;

    double gradelevel = 0;

    char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // cycle thru all text characters
    for (int i = 0; text[i] != '\0'; i++)
    {
        // loop thru all characters in letters
        currentcharacter = toupper(text[i]);
        // check all letters for a match to either letter period(ot exclamations) or spaces
        for (int j = 0; j < 25; j++)
        {
            if (currentcharacter == letters[j])
            {
                number_of_letters++;
                break;
            }
            else if (currentcharacter == ' ')
            {
                number_of_spaces++;
                break;
            }
            else if (currentcharacter == '.' || currentcharacter == '!' || currentcharacter == '?')
            {
                number_of_periods++;
                break;
            }
        }

        // count number of characters in text
        number_of_characters++;
    }

    // check number of periods (number of periods  = sentences)
    number_of_sentences = number_of_periods;
    // check number of spaces (number of spaces + 1 = number of words)
    number_of_words = number_of_spaces + 1;

    double L = ((double) number_of_letters * 100) / (double) number_of_words;
    double S = ((double) number_of_sentences * 100) / (double) number_of_words;

    gradelevel = (0.0588 * L) - (0.296 * S) - 15.8;

    // debug prints
    // printf("number_of_spaces: %d\n", number_of_spaces);
    // printf("number_of_periods: %d\n", number_of_periods);
    // printf("number_of_characters: %d\n", number_of_characters);
    printf("number_of_letters: %d\n", number_of_letters);
    printf("number_of_words: %d\n", number_of_words);
    printf("number_of_sentences: %d\n", number_of_sentences);
    printf("L: %f\n", L);
    printf("S: %f\n", S);
    printf("gradelevel : %f\n", gradelevel);

    if (gradelevel < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (gradelevel >= 16)
    {
        printf("Grade 16+\n");
    }
    else
    {  
        printf("Grade %.f\n", round(gradelevel));
    }

    // index = 0.0588 * L - 0.296 * S - 15.8  ..... means reading level =
    // where L is the average number of letters per 100 words in the text, and S is the average
    // number of sentences per 100 words in the text.
    return 0;
}
3
  • 2
    It looks like a simple issue with your "for" loop. Instead of a range of "0" to "25" it should be "0" to "26" as your test is a "less than" test and you want to test all 26 letters. When I made that change to your code, the grade level for the Harry Potter sentence calculated out to be "5". And randomly testing out some of the other examples calculated the correct grade level. Commented Dec 3, 2024 at 0:59
  • To elaborate on my earlier comment, testing for letters against the "letters" array, the test for the letter "z" was being omitted, which is in the various examples (e.g. wizard). Commented Dec 3, 2024 at 1:10
  • thank you @NoDakker!!! i really appreciate it! wow i feel kinda dumb what an oversight, 0 to 25 represent the 26 digits, i get it now thank you!!!! Commented Dec 3, 2024 at 2:03

2 Answers 2

1

I thought I would go ahead and take my comments and turn it into an answer, as it's kind of an example of a mistake I think all programmers make starting out.

In your test loop to test for any letter from "A" to "Z" you have a for loop as such.

for (int j = 0; j < 25; j++)

And entering in the "Harry Potter" example yielded the grade level of "4".

craig@Vera:~/C_Programs/Console/GradeLevel/bin/Release$ ./GradeLevel 
Text: Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
number_of_letters: 213
number_of_words: 56
number_of_sentences: 4
L: 380.357143
S: 7.142857
gradelevel : 4.450714
Grade 4

Having the loop exit once the value of "j" reached "25" skips testing for the letter "Z" and so is skewing the tests.

So this line of code is refactored as follows:

for (int j = 0; j < 26; j++)

Which yields the desired grade level.

craig@Vera:~/C_Programs/Console/GradeLevel/bin/Release$ ./GradeLevel 
Text: Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.
number_of_letters: 214
number_of_words: 56
number_of_sentences: 4
L: 382.142857
S: 7.142857
gradelevel : 4.555714
Grade 5

The main takeaway is be cognizant of not only array sizes and index values in order to test array elements, but also sometimes walking through the mechanics of your loops and how the tests net out.

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to fix your 'for' loop condition

use for (int j = 0; j < 26; j++)

instead for (int j = 0; j < 25; j++)

your code is incorrect because it doesn't check 'z' letter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.