shortening a fraction

Pages: 12
Hello!
Please, how to write a program for shortening fractions?
Many thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
6





__





17


1
2
3
4
5
6

__

17


You mean like that?
Hello!
I ment, shorten f.e. 12/4 to 3/1

or: 10/6 to 5/2

what is wrong with this code:

#include<iostream>
using namespace std;
int main(){

int a= 12;
int b=3;
int c=a;
int d=b;
for (int i=1; i<b; i++){
if (c%i==0&&d%i==0){
do {(c=c/i)&&(d=d/i);}
while ((c%i!=0)&&(d/i!=0));
}
}
cout << c << "/" << d;
return 0;



}

Did I forget & values?
I am getting 12/3 result, instead of expected (wanted) 4/1

MANY THANKS!!!
Edit your post and re-paste the code inside of code tags. They are the <> box under Format: by the text box.
Calculate the "greatest common divisor" of your numerator and denominator. Call it 'gcd'. Then divide both the numerator and the denominator by 'gcd'.
Why do you start your for loop with 1? Any integer value can be divided by one. You should start with 2.


also your do/while seems weird


Here's one method:

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
#include <iostream>

int main()
{
	int numerator = 2940;
	int denominator = 3150;
	int largest_possible_gcd = 0;
	
	if( numerator > denominator )
		largest_possible_gcd = denominator;
	else
		largest_possible_gcd = numerator;
	
	for( int i = largest_possible_gcd; i > 1; --i )
	{
		if( numerator % i == 0 && denominator % i == 0 )
                while( numerator % i == 0 && denominator % i == 0 )
		{
			numerator /= i;
			denominator /= i;
		}
	}
	
	std::cout << numerator << '/' << denominator << std::endl;
	return 0;
}


*fixed slight bug in code
Last edited on
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
//

#include<iostream>
using namespace std;
int main(){

int a= 10;
int b=6;

int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=1; i<=e; i++){
if ((a%i==0)&&(b%i==0)){
do {(a/=i)&&(b/=i);}
while ((a%i!=0)||(b/i!=0));
                    }

                        }
cout << a << "/" << b;
return 0;



}
Last edited on
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

#include<iostream>
using namespace std;
int main(){

int a= 10;
int b=6;

int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=1; i<=e; i++){
if ((a%i==0)&&(b%i==0)){
do {(a/=i)&&(b/=i);}
while ((a%i!=0)||(b/i!=0));
                    }

                        }
cout << a << "/" << b;
return 0;



}


But it is still not ok now.
Last edited on
There seems to be an epidemic of this style of coding:
(a/=i)&&(b/=i)
Why not just put
1
2
a/=i;
b/=i;
Yeah... the thread last night dove headfirst into derpy territory.

Another thing, a and b are awful variables names unless you're doing something like the Pythagorean Theorem or something else where 'a' and 'b' have some widely understood meaning. Longer variable names don't slow down your program, they speed up your comprehension!
Last edited on
Just don't become like Java.... PreAuthenticatedGrantedAuthoritiesWebAuthenticationDetails
Two methods here. A modified version of code from above is used for version one, and a separate hcf (gcd) function for version 2.
http://www.cplusplus.com/forum/beginner/115268/#msg629743

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
#include<iostream>

using namespace std;

int hcf( int num1, int num2 );

int main()
{
    int b = 2022161;
    int a = 3065857;

    //--- Save values for second version
    int numer = a;
    int denom = b;

    //--- Debugged version of "enemy" code
    int &e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
        while ((a%i==0) && (b%i==0))
        {
            a /= i;
            b /= i;
        }

    cout << "One: " << a << " / " << b << endl;

    //--- Alternative method
    int factor = hcf( numer, denom );
    numer /= factor;
    denom /= factor;
    cout << "Two: " << numer << " / " << denom << endl;

    return 0;
}

// recursive function for Highest Common Factor:

int hcf( int num1, int num2 )
{
    if (num2 == 0)
       return num1;
    else
       return hcf(num2, num1 % num2);
}



Hello, Chervil!
MANY MANY THANKS!!!
Please, can U tell me why did U write &e instead of e?
Cause the value of e is not changing!
:)
I did that because I wanted e to change.
The alternative would have been this:
1
2
3
4
5
6
7
8
9
    int e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
        while ((a%i==0) && (b%i==0))
        {
            a /= i;
            b /= i;
            e /= i;
        }

Notice the extra line. If you don't change e, the for-loop will unnecessarily go through very many more iterations.

You could try adding a counter which increments each time inside the loop, to see the difference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int count1 = 0, count2 = 0;
    int &e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
    {
        count1++;
        while ((a%i==0) && (b%i==0))
        {
            count2++;
            a /= i;
            b /= i;
        }
    }
    cout << "count = " << count1 << "  " << count2 << endl;

    cout << "One: " << a << " / " << b << endl;


Try it with and without the & and look at the value of count1.
Hello!
Still confused little bit.

So ,lets have this:

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
#include<iostream>
using namespace std;
int main()
{
    #include<iostream>
using namespace std;
int main()
{
    int b = 2*2*2*3*3*3*3*3*5*5;
    int a = 5*2*2;

    //--- Save values for second version
    int numer = a;
    int denom = b;

    //--- Debugged version of "enemy" code
    int &e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
        while ((a%i==0) && (b%i==0))
        {
            a /= i;
            cout <<b<<endl;
            b /= i;
            cout <<b<<endl;
        }

    cout << "One: " << a << " / " << b << endl;

return 0;
}


Output:

48600
24300
24300
12150
12150
2430
One: 1 / 2430

Question: can someone tell me, step by step, what is acutally happenng in the loop!!

Why is the second number written twice?
Many thanks!

Last edited on
You've two cout statements, at lines 23 and 25.

48600  - line 23
24300  - line 25
24300  - line 23
12150  - line 25
12150  - line 23
2430   - line 25
One: 1 / 2430
I am trying to figure out why are the (23) and (25) different- meaning, what makes them look just like they are..
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
#include<iostream>
using namespace std;
int main()
{
    int b = 1000;
    int a = 25;

    //--- Save values for second version
    int numer = a;
    int denom = b;

    //--- Debugged version of "enemy" code
    int &e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
        while ((a%i==0) && (b%i==0))
        {
            a /= i;
            cout <<b<<endl;
            b /= i;
            cout <<b<<endl;
        }

    cout << "One: " << a << " / " << b << endl;

return 0;
}



Output:

1000
200
200
40
One: 1 / 40

i=2 - while is inactivated
i=3 while is inactivated
i=4 while is inactivated
i=5 while starts to act:

1. b=1000 because it is still not coming to the next line sayig that b is gonna change.

2. b=200 this is now the first dividing with i=5, but, IT IS STILL LINE (23) isn't it?

otherwise, why should it be doubled?
Try adding some extra text so that you can identify which line the message is coming from.
Example:
17
18
19
20
21
22
        {
            a /= i;
            cout << "before: " << b << endl;
            b /= i;
            cout << "after:  " << b << endl;
        }

Output:
before: 1000
after:  200
before: 200
after:  40
One: 1 / 40
Why am I getting b=400 in the second line?

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
 #include<iostream>
using namespace std;
int main()
{
    int b = 2000;
    int a = 25;

    //--- Save values for second version
    int numer = a;
    int denom = b;

    //--- Debugged version of "enemy" code
    int &e = (a < b) ? a : b;

    for (int i=2; i<=e; i++)
        while ((a%i==0) && (b%i==0))
        {
            a /= i;
            cout <<b<<endl;
            b /= i;
            
        }

    cout << "One: " << a << " / " << b << endl;

return 0;
}



Output:




2000
400
One: 1 / 80

Pages: 12