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.