1

I have a problem that I have not been able to diagnose on my own or by perusing this stack overflow.

The the tests are here.

All of the index values are correct except for the grade seven sentence which returns 7.71.

Thanks in advance.

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

//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.
//214 letters, 4 sentences, and 56 words

void count_words_letters(string text, int* words, int* letters, int* sentences);

int main(void)
{
    string text = get_string("Text: ");
    int words = 0;
    int letters = 0;
    int sentences = 0;
    count_words_letters(text, &words, &letters, &sentences);
    // Coleman-Liau index:
    float L = 100.0 * letters / words;
    float S = 100.0 * sentences / words;
    float index = 0.0588 * L - 0.296 * S - 15.8;
    printf("%f\n", index);
}

void count_words_letters(string text, int* words, int* letters, int* sentences)
{
    int spaces = 0;
    int count_letters = 0;
    int count_sentences = 0;

    for (int i = 0; text[i] != '\0'; i++)
    {
        if (text[i] == ' ')
        {
            spaces++;
        }
        else if (text[i] == '!' || text[i] == '.' || text[i] == '?')
        {
            count_sentences++;
        }
        else
        {
            if(text[i] != ',')
            {
                count_letters++;
            }
        }
    }
    // there will always be +1 more words than spaces
    *words = spaces + 1;
    *letters = count_letters;
    *sentences = (count_sentences == 0) ? 1 : count_sentences;
}

EDIT: As per Clif B's observation, yes there is no formatted grade being printed. Though the problem is that I am getting an incorrect index and would like to diagnose that problem before proceeding with the remainder of the assignment.

1

"When all else fails, read the directions." ;-)

Simply put, the program is incomplete. It's the old lesson, "code MUST be written EXACTLY to the specification."

In this case, the spec said that the grade level should be whole numbers, along with some special instructions. Also, there are special instructions for "below grade 1" or "above grade 16"

So, maybe a review of the spec and a little extra coding are in order?

[EDIT: problem diagnosed.]

The problem has nothing to do with being above or below a certain grade level, although noting this was a good thing to do. When looking for software bugs, noting patterns is always a good thing, even if they turn out to be red herrings.

The letter counts are off. The logic is counting anything that hasn't been counted as a space, a sentence ending char or a comma as a letter. This isn't a comprehensive check and is allowing non-letters to be counted as letters.

Instead of counting what isn't "not a letter", the code should actually count letters, using isalpha(). This is an absolute count of letters that doesn't allow for the logic errors presently in the code.

After that, it's all about formatting the answer per the program spec.

This is also a good demonstration of tracking the data and making sure it is correct. It should have started by validating that the counts were all correct and including data that could corrupt findings. For instance, adding an apostrophe or a semicolon to the input text would have revealed the problem. Designing test data is another key skill in programming. "What kind of test data could possibly produce incorrect results?"

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Yes, I am aware of the specifications. Though, there is little point in printing out a formatted grade when the grade itself is off.
    – m.ravetch
    Oct 6 '20 at 15:47
0

I was able to solve the problem by using isalpha() rather than rolling my own letter counting code.

Of course, making sure I cast ints to floats for the index calculation helped, too. float S = 100.0 * sentences / words;

sentences and words are floats, though the leading float casts the ints to floats. Of course, you can cast them manually as well;

float S = (float)sentences / words * 100;

You must log in to answer this question.

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