0

I make a class to make Arithmetic operations on rational numbers the problem is when I run the program it keeps running and never stops or show results after many tries I discovered that the problem is the reduce method it is just stuck and never completed . any ideas ?

private void normalize() {
    if ( (this.num > 0 && this.den < 0) || (this.num < 0 && this.den < 0)){
        this.num = -this.num;
        this.den = -this.den;
    }
}

private void reduce(int n , int d) {
    int  r , L = 0; 
    while ( (n!=0) && (d!=0) ) {
        if (this.den > this.num)    
            r = den % num ; 
        else 
            r = num % den ;
        while (r != 0 ){
            for ( int i= r ; i>0 ; i-- ){
                if (den % i == 0 && num % i == 0 )
                    L = i ; 
            }
            num = num / L ; 
            den = den / L ; 
        }
    }
}

public Rational (int num , int den) {
        this.num = num ; 
        this.den = den ;
        normalize();
        reduce (this.num , this.den) ; 
}
Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27
Ali Zain
  • 29
  • 12
  • note : when I delete the line reduce(this.num , this.den) the program work properly so the problem in the method reduce (int n , int d ) – Ali Zain Oct 24 '17 at 20:38
  • 1
    `n` and `d` are not changing within the loop. you'll need to somehow make sure eventually both `n` and `d` are equal to `0`. – Ousmane D. Oct 24 '17 at 20:39
  • the while loop says (n!=0) && (d!=0) but n and d are never re-assigned in the loop – Nick Vanderhoven Oct 24 '17 at 20:39
  • Apart from not changing within the loop: You never do anything with those 2 passed values anyway, so what is their point exacty? – OH GOD SPIDERS Oct 24 '17 at 20:41
  • n refers to numerator (num ) and d for the denumerator (den) and I enter the values not equal to 0 , can someone please reedit the code or clearing the problem ? – Ali Zain Oct 24 '17 at 20:45
  • Additionally, `r` is never changing as well so there's another infinite loop. – QBrute Oct 24 '17 at 20:46
  • I dont know how to solve the problem of infinite loop I am new to java could you please edit the code , the reduce method should simplify the fraction for example if its ( 2 / 4 ) it should make it ( 1 / 2 ) – Ali Zain Oct 24 '17 at 20:50
  • @AliZain: Java is [Pass-By-Value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). So even if you pass `this.num` and `this.den` into the method it will not change the original values. But passing fields is pointless anyway and while i still have no idea what exactly you are trying to do you should probably just use the fields directly and get rid of the method parameters. – OH GOD SPIDERS Oct 24 '17 at 20:51

2 Answers2

3

I think I see the source of your confusion. You're passing num and den to reduce(int n, int d), but n and d get assigned the same value as num and den, not the same reference. As such, even though the values of num and den are changing in the while loop, those changes aren't reflected in the values of n and d.

Note also that you're doing division on integers and assigning the result to integers, which may be mathematically undesirable.

One other thing: Even after you fix the problem with n and d, you have an inner while loop that will never exit, because the value of r never changes. You need to update the value of r in the loop so that it eventually becomes 0.

Bill Horvath
  • 1,336
  • 9
  • 24
0

The problem with your loop is that you're never changing the value of r, which contains the terminal value

while (r != 0 )
    {
        for ( int i= r ; i>0 ; i-- )
        {
            if (den % i == 0 && num % i == 0 )
            L = i ; 
        }

        num = num / L ; 
        den = den / L ; 
    }

Somewhere within your while loop, you need to decrement r, or create a condition to help it reach 0.

Chris Phillips
  • 1,997
  • 2
  • 19
  • 34
  • ok I dint know how to break the loops and maintain my goal from the method so lets just forget about my code . I want to update the num and den ( numerator and denumerator ) so that if I entered ( num = 5 and den = 10 ) it will become ( num = 1 and den = 2 ) , I would really appreciate it if someone posted a method to do this ( code ) – Ali Zain Oct 24 '17 at 21:19
  • If you really can't figure it out, search for "common factors [java]" – Bill Horvath Oct 24 '17 at 21:38
  • If this answer allowed you to resolve your infinite loop bug, please accept it as the answer. – Chris Phillips Oct 25 '17 at 13:41