0

I'm struggling with the final calculation of the grade that results from the wrong value of L and S.

Example: "One fish. Two fish. Red fish. Blue fish" counts letters: 29, words: 8, sentences: 3

Expected values: L 362.50, S 37.50

My output: L 300.00, S 0.00

My mistake must be in the lines:

float L = (letters / words * 100);

float S = (sentences / words * 100);

But I have no idea what might be wrong here.

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

int main(void)
{
    string text = get_string("Text: ");
    int letters = 0;
    int words = 1;
    int sentences = 0;

//count words
    for (int i = 0; i < strlen(text); i++)
    {
       if((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
       {
           letters++;
       }
       else if(text[i] == ' ')
       {
           words++;
       }
       else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
       {
           sentences++;
       }
    }   
    printf("letters: %i, words: %i, sentences: %i\n", letters, words, sentences);

 {
     float L = (letters / words * 100);
     float S = (sentences / words * 100);
     float index = (0.0588 * L - 0.296 * S - 15.8);
     printf("L %.2f, S %.2f\n", L, S);

     printf("Index: %.2f\n", index);


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

}

}
0

You are exactly right about where the problem lies. Now, you just have to figure out what the issue really is.

Question: What happens to the remainder when you do integer division?

;-)

1
  • Thank you for that! It's working now :) – MonikaMarch May 3 '20 at 15:24

You must log in to answer this question.

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