-1

In C language I want to check if my float has some decimal value other than 0 then I want to apply if condition on that, I have a formula with 2 ways, I want this program to go with way 1 if float decimals holds some values other than 0, and go way 2 if it has only 0s in decimals. Here is my current code:

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

int letter;
int word;
int sentence;

int main(void) {

    // prompt the user with the question
    string article = get_string("TEXT: ");

    // set the length of article
    int n = strlen(article);

    // add +1 if the article starts with alphanumeric letter
    if (isalnum(article[0])) {
        word = 1;
    }

    // count words
    for (int i = 0; i < n;  i++) {
        // count letters
        if (isalnum(article[i])) {
            letter++;
        }

        // count for words
        if (i < n - 1 && isspace(article[i]) && isalnum(article[i + 1])) {
            word++;
        }

        // count for sentences
        if (i > 0 && (article[i] == '!' || article[i] == '?' || article[i] == '.') && isalnum(article[i - 1])) {
            sentence++;
        }
    }

    // Formula Coleman's calculations:
    float L = letter / word;
    float S = sentence / word;

    //THIS IS WHERE I NEED HELP
    if (#it have some value other then "0") {
        float grade1 = 0.0588 * (100 * L) - 0.296 * (100 * S) - 15.8;
    } else
    if (# it does have only 0s in decimals) {

        float grade1 = 0.0588 * (100 * letter/word) - 0.296 * (100 * sentence/word) - 15.8;
    }

    grade1 = round(grade1);
    grade1 = truncl(grade1);
    int grade = grade1;

    // print result
    if (grade <= 1) {
        printf("Before Grade 1\n");
    } else
    if (grade < 16) {
        printf("Grade %i\n", grade);
    } else {
        printf("Grade 16+\n");
    }
}

Enis Arik
  • 665
  • 10
  • 24
Ali Khan
  • 1
  • 1
  • If you code in C, then why exactly did you tag this C++11? – Lundin Mar 11 '20 at 10:25
  • Are you sure that integer division with truncation is what you want here: `float L = letter/word;`? Also, you are wildly mixing float and double precision inconsistently, which one do you actually want? `0.0` is a double constant, `0.0f` is a float constant. – Lundin Mar 11 '20 at 10:28
  • Also, your program leaks memory because of CS-50. CS-50 is infamous for teaching bad C programming, such as hiding pointers behind typedefs and creating memory leaks. – Lundin Mar 11 '20 at 10:30
  • What is the “it” that you are trying to test? Is it one of `L` or `S` or both? What is the difference in the two formulas controlled by the `if`? They look the same except for the uses of `L` versus `letter/word` and `S` versus `sentence/word`. Do you think you need separate formulas to get the right arithmetic depending on whether `letter` is a multiple of `word` and/or whether `sentence` is a multiple of `word`? That is not right. Tell us what you are really try to achieve in these calculations. – Eric Postpischil Mar 11 '20 at 11:58

1 Answers1

-1

You want the modff() function, which breaks a floating pointer number into its integral and floating point components.

float ipart, fpart;
fpart = modff(num, &ipart);
if (fabsf(fpart) < 0.00001f) { // Or whatever precision you need
 // stuff
}

Due to the way floating point math works, you don't want to compare directly to 0. More information.

solid.py
  • 2,782
  • 5
  • 23
  • 30
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • (a) `fabsf(fpart) < 0.00001f` is not appropriate in this code. The quotients involved are each the result of a single division of two integers, and, if there is any non-zero fraction part of the floating-point result, then the real-number quotient also has a non-zero fraction. Therefore, accepting a floating-point result with a non-zero fraction creates a false positive. (b) If comparison with a tolerance were needed, this code would be incorrect because using `modff` in the face of floating-point errors can produce erroneous results near 1 as well as 0, and this does not test for that. – Eric Postpischil Mar 11 '20 at 12:02