0

Background: C noob here. I'm trying to tidy up a program that calculates the readability of a piece of text. To do this I'm using the Coleman–Liau index. I'm trying to build a function and pass 3 arguments, how many letters, words and sentences there are in the text.

Problem: Function doesn't handle variables as expected. I've included some print statements to see what's going on in the function

Function Declared below my main() function:

float Coleman_Liau_index(int words_, int letters_, int sentences_)

{
    float L = (letters_ / words_) * 100;
    printf("Average number of letters per 100 words %.2f\n", L);

    float S = (sentences_ / words_) * 100;
    printf("Average number of sentences per 100 words %.2f\n", S);

    float index = 0.0588 * L - 0.296 * S - 15.8;

    return index;
}

Implementation of function inside main() :

float words = spaces + 1;

printf("%i letter(s)\n", letter);
printf("%.0f word(s)\n", words);
printf("%i sentence(s)\n", sentences);

//Putting it all together - Coleman_Liau_index
float index = Coleman_Liau_index(words, letter, sentences);

Example Incorrect Output:

Average number of letters per 100 words 400.00
Average number of sentences per 100 words 0.00

Example of Desired Output:

Average number of letters per 100 words 421.43
Average number of sentences per 100 words 4.29
user13641095
  • 107
  • 6
  • 2
    You're doing integer arithmetic when you calculate `letters_ / words_` and `sentences_ / words_`, so you're losing the fractions. – Barmar Dec 27 '20 at 17:11
  • 2
    Can you explain to us what you think what the type of `(letters_ / words_)` is? And of `(sentences_ / words_)`? – Cheatah Dec 27 '20 at 17:11
  • Interesting. I totally understand why it would be an issue to not properly cast these variables but I tried doing this without casting outside of the function and it won't fine. Why does the function change the required declaration? – user13641095 Dec 27 '20 at 17:34
  • It has nothing to do with being inside a function or not, it has to do with the type of the operands. If you divide two integers you get an integer result. You would need to show the code where it "works" as you say. – Blastfurnace Dec 27 '20 at 17:46

1 Answers1

1

You must cast integers before division

    float L = (letters_ / (float)words_) * 100;
    printf("Average number of letters per 100 words %.2f\n", L);

    float S = (sentences_ / (float)words_) * 100;
    printf("Average number of sentences per 100 words %.2f\n", S);

Notice (float) in words_.

sigsuspend
  • 56
  • 3
  • Thanks for your suggestion. Why would casting these only be necessary inside the function? I got my desired output when I didn't use a function – user13641095 Dec 27 '20 at 17:29
  • 1
    It's not related with that but with types. If you're dividing numbers (e. g.5/2 will be 2.5, but not x/y with x=5 & y=2 as int), compiler will treat them as floats (if needed). The point is that in your function, arguments are specified as integers, so they are being casted when you call the function. Your type will always be what type definition say, so be careful with that. – sigsuspend Dec 27 '20 at 18:42
  • 1
    Due to this, despite of you're treating words as float, as soon as you pass words as argument it's being treated as integer. Here is not a problem, but imagine having words_=2.5 and calling your function. What you'll receive is 2 instead of 2.5 – sigsuspend Dec 27 '20 at 18:45