Missing one case output for the CS50x Readability assignment , grade 5 output is expect but i get grade 4

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] != ''; 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: %dn", number_of_spaces);
    // printf("number_of_periods: %dn", number_of_periods);
    // printf("number_of_characters: %dn", number_of_characters);
    printf("number_of_letters: %dn", number_of_letters);
    printf("number_of_words: %dn", number_of_words);
    printf("number_of_sentences: %dn", number_of_sentences);
    printf("L: %fn", L);
    printf("S: %fn", S);
    printf("gradelevel : %fn", gradelevel);

    if (gradelevel < 1)
    {
        printf("Before Grade 1n");
    }
    else if (gradelevel >= 16)
    {
        printf("Grade 16+n");
    }
    else
    {  
        printf("Grade %.fn", 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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật