Reducing Fractions

I am working on a homework assignment that is asking me to write a program that reduces fractions to the lowest form. For example 9/12 will be 3/4, 5/10 will be 1/2, etc. We can only use If, else statements and loops.

This is what I have so far, Please ignore "Part 1" that is just the first part of the question that forces a user to have a non-zero denominator. Can someone help me with what I need to do next?

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
47
48
49
#include<iostream>
using namespace std;

	struct Fraction
	{
		int whole;
		int numerator;
		int denominator;
	};

int main()
{

	Fraction first, second;


	cout << "You will be asked to enter a whole number, a numerator and denominator for two different fractions." << endl;
	cout<< "Please enter the whole number portion of the fraction: ";
	cin>>first.whole;
	cout << "\nPlease enter the numerator for the first fraction: ";
	cin >> first.numerator;
	cout << "\nPlease enter the denominator for the first fraction: ";
	cin >> first.denominator;
		
	cout<< "Please enter the whole number portion of the 2nd fraction: ";
	cin>>second.whole;
	cout << "\nPlease enter the numerator for the second fraction: ";
	cin >> second.numerator;
	cout << "\nPlease enter the denominator for the second fraction: ";
	cin >> second.denominator;


	//Part 1

	while(first.denominator == 0)
	{
		cout<<"Please enter a denominator other than 0."<<endl;
		cin >> first.denominator;
	}

	while(second.denominator == 0)
	{
		cout<<"Please enter a denominator other than 0."<<endl;
		cin>>second.denominator;
	}
	//Part 2

	return 0;
}
All you need to do is find the largest number by which you can divide the numerator and denominator, and divide them by it. This is pretty easy to do using a loop, the modulus operator, and some if statements. Give it a shot yourself and post the results.
Without doing all the work for you, I can help you figure it out yourself. Most people use the rand() to get a random number but don't realize what the % is after it.

 
rand() % 6;


What this does is that the rand funcitno generates a 32 bit number that is random based on the seeded number which is generally what people want as time. But the % is what you want to utilize because the % divides the 32 bit number by 6 and gives you the remainder.

Start by seeing if the numerator is larger then the denominator. If so, then use the % to get the new nominator while using divide to get the whole number. Once you have this done, step one is complete.

Step two will be to run a loop to reduce it until it is at its lowest form. Create a loop that will test all numbers from 2 to your denominator to see if the nominator and denominator both have no remainder. if they do, then you can divide it. Then rerun the loop until you can't find anything that goes into it so you know it is in the simplest form.It would honestly be more efficient to run it from the demoninator on down to 2 since if 4 goes in, 2 will always go in twice meaning that 2 runs will be needed to reach 4 and three if 8 goes in, so running the for loop backwards would probably be most efficient.

Now that should get you started, once you have code and need help, post the code and I can help you with that. This way you are also learning while you are working with it.
Thanks for the replies, I already figured I would have to use the modulus operator but I don't know how to start the loop or which loop to use.

Can one of you get the loop started for me? I don't want you to post the actual solution, if you could just get a loop started for me I am sure I can figure out the rest.

Any help is appreciated as I have no idea how to start this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Fraction x;

while(true)
{
     bool found = Reduce(x);
     if(!found)
          break;
}

bool Reduce(Fraction &x)
{
     // Do your work here, return true if you can reduce it, false if you can't
}
Not really sure why you would use rand for this, but if I were to do it my loop would be something like:

1
2
3
4
int divisor = abs( min(numerator, denominator) ); // Could define a min and abs function or 
                                                  // just put the necessary code here
for (int i = divisor / 2; i > 0; i--)
// you do this 


While this is hardly the only way to do it, I THINK it should work.
Last edited on
Look up the greatest common factor/greatest common divisor (GCF / GCD). Also, go on Wikipedia and look for Euclid's algorithm, it should help you understand what you need to do.
After reading everything posted and going over my notes, this is what I have come up with. Am I on the right track?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int a, s;

	if(first.numerator<first.denominator)
		a = first.denominator;
	else
		a = first.numerator;

	for(s=a; s>0; s--)
		{
			if(first.numerator % s == 0 && first.denominator % s ==0)
			{
				first.numerator = first.numerator / s;
				first.denominator = first.denominator / s;
			}

		}


Also, if this is right, how do I display the reduced fraction? meaning what should i use as my cout statement?
Last edited on
If either numerator or denominator are negative, the loop will never end. Set a equal to the minimum of the absolute value of the two numbers.

Second, you should have a break statement in your if statement. As it is, your loop will continue after finding the greatest common divisor, wasting time and perhaps giving you the wrong result. Just put "break;" right before the end of the if statement.
Our teacher told us not to worry about negative numbers yet and assume all fractions will be positive. Based on that will this work once I put "break;" in the code? Also, can you tell me where exactly to put the "break;" and how I can output the result?

I appreciate the help!
Well if you didn't know, break will just end the current loop and not go through any more iterations. It makes sense to use it in the if statement in this case, because the very first time that if statement is true, you will have found the wanted value and any other iterations will just waste time. Like I said, put it at the end of your if block, right before the closing brace.

As for printing out the fraction, it's just as simple as outputting the numerator, then a "/" then the denominator.
1
2
3
4
5
6
if(first.numerator % s == 0 && first.denominator % s ==0)
			{
				first.numerator = first.numerator / s;
			        first.denominator = first.denominator / s;
                                break;
			}


Just pointing out what freddy meant. Dunno if your algorithm is correct but it looks good. ( I could be wrong )
Last edited on
Forget the negative numbers and just change everthing to an unsigned integer. This way there are no negative numbers so that will solve that problem for your grade. Not really the error correcting I would do but since your teacher said no to worry about it, I go that way. Here is what I would do!


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include <conio.h>
using namespace std;

struct Fraction
{
	unsigned int whole;
	unsigned int numerator;
	unsigned int denominator;
};

bool Reduce(Fraction &x)
{
	while(x.numerator >= x.denominator)
	{
		x.numerator -= x.denominator;
		x.whole++;
	}
	for(unsigned int y=x.numerator ; y >= 2 ; y--)
	{
		if(((x.numerator % y) == 0) && ((x.denominator % y) == 0))
		{
			x.numerator/=y;
			x.denominator/=y;
			return true;
		}
	}
	return false;
}

int main()
{

	Fraction first, second;


	cout << "You will be asked to enter a whole number, a numerator and denominator for two different fractions." << endl;
	cout<< "Please enter the whole number portion of the fraction: ";
	cin>>first.whole;
	cout << "\nPlease enter the numerator for the first fraction: ";
	cin >> first.numerator;
	cout << "\nPlease enter the denominator for the first fraction: ";
	cin >> first.denominator;
		
	cout<< "Please enter the whole number portion of the 2nd fraction: ";
	cin>>second.whole;
	cout << "\nPlease enter the numerator for the second fraction: ";
	cin >> second.numerator;
	cout << "\nPlease enter the denominator for the second fraction: ";
	cin >> second.denominator;


	//Part 1

	while(first.denominator == 0)
	{
		cout<<"Please enter a denominator other than 0."<<endl;
		cin >> first.denominator;
	}

	while(second.denominator == 0)
	{
		cout<<"Please enter a denominator other than 0."<<endl;
		cin>>second.denominator;
	}
	//Part 2

	while(true)
	{
		if(!Reduce(first))
			break;
	}
	while(true)
	{
		if(!Reduce(second))
			break;
	}
	cout << first.whole << " " << first.numerator << "/" << first.denominator << endl;
	cout << second.whole << " " << second.numerator << "/" << second.denominator << endl;
	getch();
	return 0;
}


It compiled and worked. I tried entering 2 175 75 0 32 48 and it simplified everything.
Topic archived. No new replies allowed.