-2

I wrote the following code to solve the readability lab. It is compiling well with no problem. The problem is in the results, as for an unknown reason it always calculates s_avg as zero. s_avg is the average number of sentences in 100 words.

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

//prototypes
int count_letters (string text);
int count_words(string text);
int count_sent(string text);

int main(void)
{
    // get the user to prompt part of the story
    string story = get_string("Text: \n");
    
    //variables
    int letters = count_letters(story);
    int words = count_words(story);
    int sentences = count_sent(story);
    
    //show results
    printf("%i letter(s)\n",letters);
    printf("%i word(s)\n",words);
    printf("%i sentence(s)\n",sentences);
    
    // Calculate average number of letters & sentences per 100 words
    int l_avg = (letters/words)*100;
    int s_avg = (sentences/words)*100;
    
    // Calculate Coleman-Liau index
    int index = round(0.0588 * l_avg - 0.296 * s_avg - 15.8);
    
    // check grade level
    if(index<1)
    {
        printf("Before Grade 1\n");
    }
    else if(index>16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n",index);
    }
}

//count the number of letters
int count_letters (string text)
{
    int l = 0;
    for(int i=0,n=strlen(text); i<n; i++)
    {

        if((text[i]>=97 && text[i]<=122)||(text[i]>=65 && text[i]<=90))
        {

            l++ ;
        }
    }
    return l;
}

//count the number of words
int count_words(string text)
{
    int w = 1;
    for(int i=0,n=strlen(text); i<n; i++)
    {
        if (text[i]==32)
        {
            w++ ;
        }
    }
    return w ;
}

//count the number of sentences
int count_sent(string text)
{
    int s=0;
    for(int i=0,n=strlen(text); i<n; i++)
    {
        if ((text[i]==46) || (text[i]==33) || (text[i]==63))
        {
            s++ ;
        }
    }
    return s;
}

I don't know why it keeps calculating int s_avg = (sentences/words)*100; as zero. I realized that using the debug50 tool.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
bee3oo
  • 3
  • 2
  • Let's imagine sentences=2, words=20. `2/20 = 0` because you're working with `int`s. If you want a floating point result, use floating point variables or cast at least one of the values to a float type. –  Sep 26 '21 at 18:26
  • Does this answer your question? [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) –  Sep 26 '21 at 18:27
  • Please extract and provide a [mcve]. For that, keep in mind that nobody here has access to "cs50.h", which is not a standard header. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Sep 26 '21 at 18:42
  • Maybe it's better to post at cs50.stackexchange.com – klutt Oct 05 '21 at 05:35

2 Answers2

0

For calculating average number of letters & sentences, use double type variables instead.

double l_avg = ((double)letters / (double)words)*100;
double s_avg = ((double)sentences / (double)words)*100;
Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14
0

Integer division in C results in an integer, not a float. You have to explicitly cast the dividend and the divisor to float:

int i = 5 / 4;
printf("%d", i); // prints 1

float f = (float)5 / (float)4;
printf("%f", f); // prints 1.250000

So, int s_avg = (sentences/words)*100 should be:

float s_avg = (float)sentences / (float)words * 100;
Zakk
  • 1,935
  • 1
  • 6
  • 17