the program seems to be running fine but I don't know what went wrong in the index, it just doesn't give the right answer.

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

int main(void)
    float L, S;//average number of letters and sentences per 100 words
    string text;
    int count_letters = 0, count_words = 0, count_sentences = 0;//number of letters, sentences and words

text = get_string("Text: ");//get user input

for (int i = 0; text[i] != '\0'; i ++)
        if (isalpha(text[i]))//count letters
            count_letters ++;
        if (isspace(text[i]))//count words
            count_words ++;
        if (sentences[0][i] == '?' || sentences[0][i] == '!' || sentences[0][i] == '.')//count sentences
            count_sentences ++;
printf("%i letter(s)\n", count_letters);
printf("%i word(s)\n", count_words + 1);
printf("%i sentence(s)\n", count_sentences);

L = (float)count_letters / /*(float)*/((count_words + 1) * 100);
S = (float)count_sentences / /*(float)*/((count_words + 1) * 100);
float index = (0.0588 * (L - 0.296) * (S - 15.8));//Coleman-Liau index
int answer = round(index);
if (answer > 16)
    printf("Grade 16+\n");
else if (answer < 1)
    printf("Before 1\n");
    printf("Grade %i\n", answer);
printf("%i\n", answer);

Your approach is very different and difficult (but don't need to worry, instead I appreciate you that you have taken something next level to solve this PSET)

Obviously in computer science there is always more than one way for approaching a problem

Try to think more effectively and efficiently to think about the problem

Some hints are:

  • Do you really think that you have to copy the text provided to you inside a memory called array?

  • Do you really need to go inside the array to count the letters words and sentences?

  • By declaring array[i][j] actually it creates a 2D array which looks like a table like content containing rows and columns

I hope this helps

  • I've modified it now so it's a 1D array but the answer is still not right Jul 20 '20 at 19:22
  • Yeah really well done that actually you are declaring a function prototype of count_letters before main but no use of it nor I see its definition Jul 20 '20 at 19:41
  • Also you are not correctly taking the average of letters and sentences try to concentrate on it I am not gonna tell you the key Jul 20 '20 at 19:43
  • thank you so much, it's working now. Jul 20 '20 at 20:25
  • No problem please be sure to tick the answer to keep other smiling too 😃 Jul 20 '20 at 20:45

You've taken count_letters and count_sentences as float. That approach is right. But float data type probably overflows and you should use floor() to correct them. I used floor() to assign fixed floats and it worked. I suggest you to use count_words as float only because this way you have only one extra float and answer will be the same.

float count_words = 0

The code you wrote.

count_words += 1

L = count_letters / floor((count_words)) * 100);

S = count_sentences / floor((count_words_f)) * 100);

You must log in to answer this question.

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