0

I'm struggling with PSet 2 Readability. I don't want to cheat the answers because I am genuinely trying to learn how to think better and improve my logic.

I so far have been able to get the letter count, word count and sentence count. I created a function that is intended to run the formula. My function is supposed to return the value of "cfindex"

When I printf the count for letter, word, sentence this seems all correct but my printf for the function returns -15 no matter the sentence I input. Guidance is appreciated!

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <ctype.h> //to count character
#include <string.h>

int formula(int L, int S);
int hundredWordCount = 100;

int main(void)
{
    //Get string
    string text = get_string ("Text: \n");
    int stringCount = strlen(text); //(text) passes the above string variable into strlen function
    int letterCount = 0; //letters for Coleman-Lau formula
    int sentenceCount = 0; //Sentences for Coleman-Lau formula
    int words = 1;
    //count letters
    for (int letters = 0; letters < stringCount; letters++)
    {
       //isalpha checks only for alphabetic characters so if it is a alphabetic character, iterate L++
       if(isalpha(text[letters]))
       {
            // text[letters] =
            letterCount++; //works
       }
       if(isspace(text[letters]))
       {
           //Each space will determine this is a word
           words++; //works
           //Problem with above is it needs to check if there's a letter after
           //Check for a letter and increment

           //I also need to calculate per 100 words
           if (words >= 100)
           {
               hundredWordCount+=100;
               return hundredWordCount;
           }

       }
       //To determine sentences, I need to identify . ! ?
       if(text[letters] == '.' || text[letters] == '!' || text[letters] == '?')
       {
           sentenceCount++; //works
       }

        //If I use this calculation, it will zero out my letters and my sentences
        //Per 100 words calculation
         //   letterCount =  letterCount / hundredWordCount;
        //   stringCount =  sentenceCount / hundredWordCount;
    }

    //testing printf values
    printf("Letters: %i\n Words: %i\n Sentences: %i\n StringCount: %i\n WordCount: %i\n Index: %.1d\n", letterCount, words, sentenceCount, stringCount, hundredWordCount, formula(letterCount/100, sentenceCount/100)); //this printf for formula always returns -15

int formula(int L, int S)
{
    double cfindex;

    //coleman lau index
    cfindex = 0.0588 * L - 0.296 * S - 15.8;
    return cfindex; //output
}
0

Tried to follow the logic for a half a minute, decided it was better to attack the central problem directly.

I suspect that you haven't grasped the concept of normalizing numbers to x per 100. For example, to determine the number of letters per 100 words, divide letters by words and multiply the result by 100.

I really have no idea why the code is incrementing hundredcount variables or how they're being used here.

Also, this code has another problem:

       //I also need to calculate per 100 words
       if (words >= 100)
       {
           hundredWordCount+=100;
           return hundredWordCount;
       }

If it were ever to execute, it would terminate the program because of the return statement.

Hope this helps.

1
  • This was helpful, I got a little tutoring on this to understand better and you know, I've read the assignment about 2 or three times and I totally miss they actually tell you how to do the math here at the beginning. I tackle things step by step so I forgot about it by the time it came to the calculation. – user5854648 Jun 24 '20 at 22:11

You must log in to answer this question.

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