zach101 0 Report post Posted December 9, 2005 Hey guys I could really use some help wiht C++ because My C++ class consists of me three others, and a ton of people learning HTML. So in other words its basically independent study. He dosnt really care how you get the assignments done or when you do... just get them done. Well to say the least I am STRUGLING in this class do to the lack of instruction. Now the latest assignment im sure seems eazy for many of you but for me... well considering this is my first year of programming im still quitea a newbie. So the assignment says right a function named Reduce() that takes the integer numerator and denominator of a fraction and reduces it to lowest terms. and then it says test it with this code: {int Num, Denom;cout << "Enter the numerator: ";cin Num;cout << "Enter Denominator: ";cin Denom;Reduce(Num, Denom);cout << "The reduced fraction is " << Num << "/" << Denom << endl;} Now ive been working with this problem for a little while now and once i get on the right track i can usually get these but this one has me stumped. Any help would be awesome. Share this post Link to post Share on other sites
zach101 0 Report post Posted December 9, 2005 Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there? #include<iostream.h>//------------------------------------------------------------------------------void Reduce(int Num, int Denom)int Div = Num;if (Num % Div == 0 && Div % Div == 0)GFC = int DivelseDiv--;//------------------------------------------------------------------------------int main(){int Num, Denom;cout << "Enter the numerator: ";cin >> Num;cout << "Enter the denominator: ";cin >> Denom;Reduce(Num, Denom);cout << "The reduced fraction is " << Num << "/" << Denom << endl;}return(0);} Share this post Link to post Share on other sites
switch 0 Report post Posted December 11, 2005 firstly, you need curly braces { } around your reduce() function.secondly you are effectively getting the remainder of the numerator divided by itself in both parts of your statement (as Num == Div): if (Num % Div == 0 && Div % Div == 0) each of these modulus statements will return a remainder of 0.you need to find the highest common factor of the numerator and the denominator. you could do this by using a while loop and looping through every positive integer. Then stop when you get to half of the smallest number (as you won't get a highest common factor bigger than that).i'm not giving you the code because there's every chance that you could be cheating on an assignment... seriously, your situation does sound a little dodgy. anyway, as for learning C++ by yourself, buy a book. You could probably pick one up for less than $30 if you know where to look. Share this post Link to post Share on other sites
methane 0 Report post Posted February 19, 2006 Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there? #include<iostream.h>//------------------------------------------------------------------------------void Reduce(int Num, int Denom)int Div = Num;if (Num % Div == 0 && Div % Div == 0)GFC = int DivelseDiv--;//------------------------------------------------------------------------------int main(){int Num, Denom;cout << "Enter the numerator: ";cin >> Num;cout << "Enter the denominator: ";cin >> Denom;Reduce(Num, Denom);cout << "The reduced fraction is " << Num << "/" << Denom << endl;}return(0);} To find out the GCD/GCF of two integers, you can make use of euclidean algorithm. That is, GCD(a, = GCD(a%b, if a>b. You can use a recursive function to find out the answer in fewer steps. The function should look like thisint GCD(int a , int { if (a == { return a; } if (a * b == 0) { return a + b; } return ( a%b , b%a);}Hope it can help. Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there? #include<iostream.h>//------------------------------------------------------------------------------void Reduce(int Num, int Denom)int Div = Num;if (Num % Div == 0 && Div % Div == 0)GFC = int DivelseDiv--;//------------------------------------------------------------------------------int main(){int Num, Denom;cout << "Enter the numerator: ";cin >> Num;cout << "Enter the denominator: ";cin >> Denom;Reduce(Num, Denom);cout << "The reduced fraction is " << Num << "/" << Denom << endl;}return(0);} To find out the GCD/GCF of two integers, you can make use of euclidean algorithm. That is, GCD(a,b ) = GCD(a%b,b ) if a>b. You can use a recursive function to find out the answer in fewer steps. The function should look like thisint GCD(int a , int b){ if (a == b) { return a; } if (a * b == 0) { return a + b; } return ( a%b , b%a);} Hope it can help. Share this post Link to post Share on other sites
bidarshi 0 Report post Posted March 23, 2006 Well I am not an expert in C programming and I think I can address your problem with a logical approach. For reducing a fraction you first check whether the numerator and the denominator are relatively prime to each other. If yes it means the fractions are already reduced. If they are not relatively prime further reduction is possible. Now you find the greatest common factor between the two that is numerator and denominator.Divide both the numerator and denominator with the greatest common factor.The fraction is thus reduced. You check it and meanwhile I will also check whether it is working. But I think there is no bug in this approach. Share this post Link to post Share on other sites
iGuest 3 Report post Posted February 1, 2009 He's right. You need to use a loop to do it. (I myself used a while loop nested in a for loop). I had to do a simple fraction addition assignment, but I figured I would make it reduce fractions. I'm beginning C++ but I actually figured it out! You need to think: numerator and denominators must have no common factors besides 1 in order for them to be considred reduced. Set a variable called common factor or something and divide the numerator and denominator by it if mod = 0. Share this post Link to post Share on other sites
Forte 0 Report post Posted March 18, 2009 Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there?I'd do it a little more like this:#include <iostream> using namespace std; int gcd(int m, int n) { if(n == 0) return m; else gcd(n, m % n); } void reduce(int &num, int &denom) { int divisor = gcd(num, denom); num /= divisor; denom /= divisor; } int main() { int num = 0, denom = 0; cout << "Enter the numerator: "; cin >> num; cout << "Enter the denominator: "; cin >> denom; reduce(num, denom); cout << "The reduced fraction is " << Num << "/" << Denom << endl; return 0; } A few things to note: - you had a few lines without a semicolon that really needed one. - C++ headers don't end in .h, and the line "using namespace std;" should be used afterwards. - functions, if statements, and loops that contain more than one line of code need to have that code contained within braces. {} - it is C++ convention to name your variables in lower case. - the statement "Div % Div == 0" is unnecessary as a number will always evenly divide into itself. - in your original posted code, you had an if statement using =. In C++, this is the assignment operator. The equal to operator is ==. To find out the GCD/GCF of two integers, you can make use of euclidean algorithm. That is, GCD(a,b ) = GCD(a%b,b ) if a>b. You can use a recursive function to find out the answer in fewer steps. The function should look like this int GCD(int a , int b) { if (a == b) { return a; } if (a * b == 0) { return a + b; } return ( a%b , b%a); } Hope it can help. Generally the code can be written much cleaner, not to mention the function you wrote is not recursive. The following code should work just as nicely:int gcd(int m, int n) { if(n == 0) return m; else gcd(n, m % n); } Share this post Link to post Share on other sites