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?
#include<iostream>
usingnamespace 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.
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.
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.
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?
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.
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!
#include<iostream>
#include <conio.h>
usingnamespace std;
struct Fraction
{
unsignedint whole;
unsignedint numerator;
unsignedint denominator;
};
bool Reduce(Fraction &x)
{
while(x.numerator >= x.denominator)
{
x.numerator -= x.denominator;
x.whole++;
}
for(unsignedint y=x.numerator ; y >= 2 ; y--)
{
if(((x.numerator % y) == 0) && ((x.denominator % y) == 0))
{
x.numerator/=y;
x.denominator/=y;
returntrue;
}
}
returnfalse;
}
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.