Floating point exception error

This program compiles fine, but when run it returns a "Floating point exception" message. I have looked at other threads and the problem seems to be divisibility by 0, but I looked at the program and there is no division by zero in my program. I even used the absolute value function in the case.

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

Input example: 6

12

representing fraction 6/12


Expected result: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;
}

      

+3


source to share


1 answer


The main problem is that you do not pass their input values num1

and num2

in its function reduce()

. Instead, you are passing global variables higher

and lower

. You haven't assigned any values ​​to them, but global variables are always initialized to 0 by default. Thus, you are throwing an exception because in reduce()

you you are dividing 0 by 0. You can check this with a debugger.

If I modify yours main()

as follows, then your code at least works 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 digits: 6
12
GCD equals 1/2
enter 0 until the end of the program and any number will continue:


As pointed out in the comments, you should also get rid of globals and spurious variables. Therefore, you must first remove 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 function main()

run like this:

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

      

And your function reduce()

should look 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); 
}

      

As long as you do not use the variables higher_2

and lower_2

in the function main()

, but I think you want to do it. If not, you can also get rid of them along with parameters 3 and 4 of your function reduce()

.


There is another problem with the code you provided (thanks @ user3629249 for pointing it out): you are missing the include for the function abs()

. So you need to add a line #include <stdlib.h>

at the beginning of your code ( include <math.h>

will be a trick too, also include <Windows.h>

on Windows).

+5


source







All Articles