GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jan-2009, 13:34
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Simplifying a fraction


Can someone please help me write a piece of c++ code that will simplify a fraction, for example 4/16, the answer would be 1/4, on paper this is easy as sin, but as a begginer programmer im having trouble coding it. PLEASE HELP!

Dan
  #2  
Old 26-Jan-2009, 13:45
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: simplifying a fraction


1. Find the greatest common divisor of the numerator and denominator.
2. Divide both the numerator and denominator by the gcd.

CPP / C++ / C Code:
#include <iostream>

using namespace std;

// greatest common divisor, courtesy of Wikipedia
int gcd(int a, int b)
{
   return (b == 0 ? a : gcd(b, a%b));
}


int main (int argc, char * argv [])
{
  int numerator, denominator;
  cout<<"Enter numerator: ";
  cin>>numerator;
  cout<<"Enter denominator: ";
  cin>>denominator;
  int g = gcd(numerator, denominator);
  cout<<(numerator/g)<<"/"<<(denominator/g)<<endl;
}
__________________
www.blake-foster.com
  #3  
Old 26-Jan-2009, 13:49
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: simplifying a fraction


This is pretty simple. You have two integers....a numerator and a denominator. The goal is to divide both of these numbers by the largest number that will still give a whole number. So, you will need a function that will give you that number to divide by:

CPP / C++ / C Code:
int FindLargestDivisor( int a, int b )
{
}
Then, you can use it like:
CPP / C++ / C Code:
int x = 125;
int y = 375;
int div = FindLargestDivisor(x,y); // this should result in 125
int x1 = 125/div;
int y1 = 375/div;
// now you have it simplified.
  #4  
Old 27-Jan-2009, 13:37
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: Simplifying a fraction


thanks guys i really appreciate that... the code that you provided blake didnt work for me cause i was to use structures... but it definitely helped me.. and it work great with individual values.. but this is what i was after

CPP / C++ / C Code:


//Profs version, well his simplify logic.

#include <iostream>

using namespace std;

struct Fraction {
    int numerator;
    int denominator;
};

struct Fraction simplify (struct Fraction f);

int main(){

    struct Fraction f;
    struct Fraction gcm;

    cout << "Fraction Simplifier" << endl;
    cout << "-------------------" << endl;
    cout << "Numerator: ";
    cin >> f.numerator;
    cout << "Denominator: ";
    cin >> f.denominator;

    gcm = simplify(f);

    cout << f.numerator << "/" << f.denominator << " = " <<
        gcm.numerator << "/" << gcm.denominator << endl;

    return 0;
}

struct Fraction simplify (struct Fraction f){

   int k, i;

   if(f.numerator > f.denominator)
        k = f.denominator;
   else
        k = f.numerator;

   for (i = k; i >= 0; i--){
        if(f.numerator % k == 0 && f.denominator % k == 0){
                f.numerator = f.numerator / k;
                f.denominator = f.denominator / k;
        }
   }
   return f;
}



you guys are the best thanks again....
Daniel

and sorry i cant seem to get the tags to show my code properly.. what i am doing wrong???
Last edited by LuciWiz : 27-Jan-2009 at 13:55. Reason: Corrected code tags
  #5  
Old 27-Jan-2009, 13:59
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Simplifying a fraction


Quote:
Originally Posted by nowocien
and sorry i cant seem to get the tags to show my code properly.. what i am doing wrong???


The [c] and [cpp] tags are not supposed to be used as an enumeration (just choose one). The preferred tag now is [cpp], which can be used for both C and C++ code, as in:

[cpp]
// your code
[/cpp]
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #6  
Old 27-Jan-2009, 14:01
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: Simplifying a fraction


thanks Wiz
  #7  
Old 27-Jan-2009, 14:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Simplifying a fraction


Quote:
Originally Posted by nowocien
...this is what i was after...

Did you test it with 6/8? How about 21/14?

Regards,

Dave
  #8  
Old 28-Jan-2009, 12:09
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: Simplifying a fraction


ohhh shitt! your right Dave.. prof is not gona be happy..

I will repost a working version.

thanks Dave

Dan
  #9  
Old 28-Jan-2009, 15:39
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: Simplifying a fraction


oookk got it.. this one works great. Thanks again Dave... im getting better.

CPP / C++ / C Code:

#include <iostream>

using namespace std;

struct Fraction {
    int numerator;
    int denominator;
};

struct Fraction simplify (struct Fraction f);

int main(){

    struct Fraction f;
    struct Fraction gcm;

    cout << "Fraction Simplifier" << endl;
    cout << "-------------------" << endl;
    cout << "Numerator: ";
    cin >> f.numerator;
    cout << "Denominator: ";
    cin >> f.denominator;

    gcm = simplify(f);

    cout << f.numerator << "/" << f.denominator << " = " <<
        gcm.numerator << "/" << gcm.denominator << endl;

    return 0;
}

struct Fraction simplify (struct Fraction f){

   int k, i;

   if(f.numerator < f.denominator)
        k = f.denominator;
   else
        k = f.numerator;

   for (i = k; i > 0; i--){
        if(f.numerator % i == 0 && f.denominator % i == 0){
                f.numerator = f.numerator / i;
                f.denominator = f.denominator / i;
        }
   }
   return f;
}

 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C#] Method that recieves matrix values as a string Max_Payne .NET Forum 1 17-Jun-2008 07:44
Fraction program SpudNuts C++ Forum 6 25-Sep-2006 04:50
Defining a class earachefl C++ Forum 8 04-May-2006 22:22
fraction class program fevershark C++ Forum 7 31-Jan-2006 12:12
accessing a private member in a public member small_ticket C++ Forum 2 16-Oct-2004 12:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.