Fraction Problem - Infinite Loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

void faddiction (int& a, int& b, int& c, int& d)
{
    int fr1, fr2, fi, fz1, fz2;
    fi = 2;
    if (b == d)
    {
        fr1 = a + c;
        fr2 = b;
    }
    else
    {
        a *= d;
        b *= d;
        c *= b;
        d *= b;
        fr1 = a + c;
        fr2 = b;
    }
    do
    {
        fz1 = (fr1 % fi);
        fz2 = (fr2 % fi);
        if ((fz1 + fz2) == 0)
        {
            cout << fr1 / fi << "/" << fr2 / fi << "\n";
        }
        else
        fi++;
    }
    while (fi < 100);
}

int main ()
{
    int a, b, c, d;
    a = 5;
    b = 12;
    c = 3;
    d = 2;
    faddiction (a, b, c, d);
    cin.get();
}


Why that dosen't work correctly? It does compile, but loops forever...
Is your if statement always returning true?
Yes... It keeps printing the fraction on the screen...
If fz1 + fz2 == 0 then you never increment fi so the while() condition never changes....
I can't find a way to correct it >.< """ I did find that if I use while ((fz1 + fz2) != 0, fi <= 100) I would correct that, but it dosen't worked >.<" Any idea on how I could make that?
And the fz1 + fz2 == 0 is needed, because fr1 % fi and fr2 % fi MUST return 0...
Ok, it looks like you are trying to reduce the resulting fraction. Google "greatest common divisor"... there are well-known algorithms to do this.

Ok, thanks!
Topic archived. No new replies allowed.