Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Point Exception Error

Tags:

c++

c

This program compiles fine, but it returns a message "Floating Point Exception" when I run it. I've looked at other threads and the problem appears to be dividing by 0, but I have looked over the program and there's no division by zero in my entire program. I even used the absolute value function in case.

By the way, the program is meant to reduce fractions.

Example input: 6 12, representing the fraction 6/12
Expected output: 1/2

#include <stdio.h>

/*declaring variables*/
int num1, num2, num1b, num2b, gcd, x;
int higher, lower, higher_2, lower_2;

/*declaring functions*/
int find_gcd(int num1, int num2);
void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator);

int main(void)
{
    do
    {
        printf("enter 2 numbers:  ");
        scanf("%d %d", &num1, &num2);
        reduce(higher, lower, &higher_2, &lower_2);
        printf("enter 0 to end program and any number continue: \n");
        scanf("%d", &x);
    } while(x != 0);

    return 0;
}

void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator)
{
    num1=numerator;
    num2=denominator;

    gcd =find_gcd(numerator, denominator);

    *reduced_numerator = (numerator/abs(gcd));
    *reduced_denominator = (denominator/abs(gcd));

    printf("The GCD is %d/%d\n", *reduced_numerator, *reduced_denominator); 
}

int find_gcd(int m, int n)
{
    while (n != 0) {
        int remainder = m % n;
        m = n;
        n = remainder;
    }
    return m;
}
like image 481
Mike Tucker Avatar asked Aug 22 '26 05:08

Mike Tucker


1 Answers

Your main problem is that you are not passing your input values num1 and num2 into your reduce() function. Instead you are passing in the global variables higher and lower. You didn't assign any values to them, but global variables are always initialized to 0 by default. Therfore, you run into the exception, because in reduce() you divide 0 by 0. You can verify that with a debugger.

If I change your main() as follows, then your code is at least working for your test case with 6 and 12 as input:

int main(void)
{
    do
    {
        printf("enter 2 numbers:  ");
        scanf("%d %d", &num1, &num2);
        reduce(num1, num2, &higher_2, &lower_2);
        printf("enter 0 to end program and any number continue: \n");
        scanf("%d", &x);
    } while(x != 0);

    return 0;
}

Output:

enter 2 numbers: 6
12
The GCD is 1/2
enter 0 to end program and any number continue:


As indicated in the comments you should also get rid of global and spurious variables. Therefore, you should first delete the following lines in your code:

/*declaring variables*/
int num1, num2, num1b, num2b, gcd, x;
int higher, lower, higher_2, lower_2;

Then let your main() function start the following way:

int main(void)
{
    int num1, num2, higher_2, lower_2, x;
    ...
}

And your reduce() function should read like this:

void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator)
{
    int gcd = find_gcd(numerator, denominator);

    *reduced_numerator = (numerator/abs(gcd));
    *reduced_denominator = (denominator/abs(gcd));

    printf("The GCD is %d/%d\n", *reduced_numerator, *reduced_denominator); 
}

So far, you don't use your variables higher_2 and lower_2 in the main() function, but I guess you plan to do so. If not, you can also get rid of them together with parameters 3 and 4 of your reduce() function.


There is another issue with the code you provided (thanks to @user3629249 for pointing it out): You are missing an include for the abs() function. So you need to add the line #include <stdlib.h> at the beginning of your code (include <math.h> will also so the trick, as well as include <Windows.h> on Windows).

like image 76
honk Avatar answered Aug 23 '26 17:08

honk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!