0

I'm writing a program that lets the user enter a nominator and a denominator, which the program then converts to mixed form (9/8 -> 1 1/8 and so on). It works just fine, except for when the denominator is greater than the nominator. When I started debugging the code, I found something I couldn't understand. Below is the method fraction, which at the end of it calls the method gcd (greatest common divider) to determine what to divide the remainder of the integer division and the denominator with.

public static int[] fraction(int nominator, int denominator) //Rewrites a fraction to mixed form
{

                                  //nominator == 7 and denominator == 8

    int[] result = {0, 0, 0};
    int quotient;
    int remainder;
    if (denominator == 0)
        return null;
    if (nominator == 0)
    {
        result[0] = 0;
        result[1] = 0;
        result[2] = 0;
        return result;
    }
    kvot = nominator/denominator;
    rest = nominator % denominator;
    result[0] = quotient;
    result[1] = remainder/(gcd(remainder, denominator));    //According to the debugger, the values are still 7 and 8 here
    result[2] = denominator/(gcd(remainder, denominator));


    return result;
}

When called with the above values, something seems to go wrong, because the values the second method recieves from the first is wrong. Instead of 7 and 8, it receives 7 and 1. Is the error in the code provided, or does it seem to be somewhere else?

public static int gcd(int a, int b) //Calculates the greatest common divider for two integers


{  //The debugger tells me that a == 7 and b == 1, even though the method was called with 7 and 8

int temp, c;

if (b == 0)
{
    System.out.println("Nominator is 0, error");;
}
if (b > a)
        {
            temp = b;
            b = a;
            a = temp;
        }

    c = a % b;
    a = b;
    b = c;

    return a;

}
  • What is the `sgd` method? Please show `gcd` – OneCricketeer May 09 '17 at 21:52
  • Oh sorry, I renamed everything to english before posting, I just forgot that one. gcd is the same as sgd, I edited it now – Jakob Eklund May 09 '17 at 21:56
  • 1
    Have you also forgot to mare your method recursive? GCD algorithm that you use looks like a recursive one, but it's missing a base case and it should return gcd(a,b) – Sergey Kalinichenko May 09 '17 at 21:58
  • `c = a%b; a = b; b = c; return a;`? Do you know that these are just local variables, so this doesn't affect the values of the variables where you call them? This is all equivalent to `return b;`. – Andy Turner May 09 '17 at 22:00
  • I'm not sure I understand what you mean, the method is supposed to return just one int, and that is the gcd of the two integers used to call the method – Jakob Eklund May 09 '17 at 22:02

1 Answers1

1

Your gcd function is not ok. If you use the Euclid's algorithm you can calculate gcd like this

public static int gcd(int a, int b) //Calculates the greatest common divider for two integers
{
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a%b);
    }

}

You can also use some other solution, but the proposed is not ok.

Nenad
  • 484
  • 3
  • 14