## Fractions

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### Fractions

received lab 5 other day.. Teacher supplied us with2 fractions and told us to write a function to get the common denominator. I think the logic is whats confusing. I dont want problem solved for me just asking for some help on logic part.

#include <iostream>
using namespace std;

// Prototype Declaration
void GetCd( int&, int&);

void main()
{

int fraction1num = 5;
int fraction1den = 10;

int fraction2num; = 5
int fraction2den = 20;

}

// Get Common Den.
void GetCd(int& fraction1den , int& fraction2den)
{

}
rbran1974

Posts: 15
Joined: Thu Oct 02, 2003 10:27 am

Google for euclid and gcd.

jgbauman

Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

That won't really help too much. GCD of an int, sure, but the GCD of a fraction is infinite
Do you want any common denominator, or the lowest?
If it's just any denominator, then just multiply the two given denominators together.
ie, fractions 5/10 and 5/20, a common denominator is (10*20) = 200

Somehow I don't think this is what the problem is supposed to be though. More than likely, it's asking for a method which, given a numerator and a denominator, returns the reduced fraction. ie, 5/10 becomes 1/2 and 5/20 becomes 1/4.
If I'm right, and it is the second case, here's a clever algorithm http://www2.math.uic.edu/~burgiel/Mtht420/13/handoutA.html. It's intended for logo, but you should be able to figure it out. If implemented properly, then it will return the greatest common divisor of two numbers. ie, if passed 5/10, it will return 5. Then divide both numbers by 5 to get 1/2. See?

Wizard
Site Admin

Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

i wrote this to solve quadratics with, it uses the gcd i might be of some help

Code: Select all
`#include <iostream>#include <conio.h>#include <cmath>#include <ctime>using namespace std;int gcd (int d, int e) { // defining the greatest common factor functionint temp;while (e != 0) {temp = d % e; d = e;e = temp;}return d;}int main () {time_t mytime;struct tm *times;times = localtime(&mytime);time (&mytime);cout << "Current date and time is: " << ctime(&mytime) << endl;cout<<""<<endl;cout<<"This program returns the factors for any quadratic equation whose discriminant is a perfect square"<<endl;cout<<""<<endl;cout<<""<<endl;cout<<"press any key to continue........";getch();int a=0,b=0,c=0;int p=0,i=0,r=0,x=0;char key='Y';while (key=='Y' || key=='y' ){int root=0;system("cls");cout<<"Enter coefficients a , b , c for ax^2 + bx + c"<<endl;cout<<""<<endl;cout<<"a = ";cin>>a;cout<<"b = ";cin>>b;cout<<"c = ";cin>>c;cout<<""<<endl;if(b*b-4*a*c<0){ // condition for imaginary rootscout<<"The roots are undefined"<<endl; cout<<""<<endl;cout<<"Do you want to try again y/n ?"<<endl;cout<<""<<endl;cin>>key;}else{root = (b*b-4*a*c);if(root==4){root=root/2;} if(root==9){root=root/3;}if(root==16){root=root/4;}if(root==25) {root=root/5;}if(root==36) {root=root/6;} if(root==49) { root=root/7;}if(root==64) {root=root/8;} if(root==81) { root=root/9;} if(root==100) {root=root/10;}if(root==121){ root=root/11;}if(root==144) {root=root/12;}if(root==169) {root=root/13;}if(root==196) {root=root/14;} if(root==225) { root=root/15;} if(root==256) {root=root/16;}if(root==289) {root=root/17;}if(root==324) {root=root/18;}if(root==361) {root=root/19;}if(root==400) {root=root/20;}if(root==441) {root=root/21;}if(root==484) {root=root/22;}if(root==529) {root=root/23;}if(root==576) {root=root/24;}if(root==625) {root=root/25;}if(root==676) {root=root/26;}if(root==729) {root=root/27;}if(root==784) {root=root/28;}if(root==841) {root=root/29;}if(root==900) {root=root/30;}p = (2*a);i = -(-b-((root)));r = -(-b+((root)));if (i>0 && r>0){cout<<"The factors are ("<<p/gcd((i),p)<<"x + "<<i/gcd(i,p)<<")"<<"("<<p/gcd(r,p)<<"x + "<<r/gcd(r,p)<<")"<<endl;cout<<""<<endl;}else if(i<0 && r<0){cout<<"The factors are ("<<p/gcd((i),p)<<"x "<<i/gcd(i,p)<<")"<<"("<<p/gcd(r,p)<<"x "<<r/gcd(r,p)<<")"<<endl;cout<<""<<endl; }else if(i<0 && r>0){cout<<"The factors are ("<<p/gcd((i),p)<<"x "<<i/gcd(i,p)<<")"<<"("<<p/gcd(r,p)<<"x + "<<r/gcd(r,p)<<")"<<endl;cout<<""<<endl; }else if(i>0 && r<0){cout<<"The factors are ("<<p/gcd((i),p)<<"x + "<<i/gcd(i,p)<<")"<<"("<<p/gcd(r,p)<<"x + "<<r/gcd(r,p)<<")"<<endl;cout<<""<<endl; cout<<i<<" "<<r;} cout<<""<<endl; cout<<"Do you want to try again y/n ?"<<endl;cout<<""<<endl;cin>>key; }}return 0;}`
slicknet

Posts: 18
Joined: Sun Oct 05, 2003 4:33 pm

Sorry, didn't bother to comment on anything but this:
slicknet wrote:if(root==4){root=root/2;} if(root==9){root=root/3;}if(root==16){root=root/4;}
...

This doesn't look too beautiful. I'm not sure what the main point here is, but standard library actually has a square root function. In <cmath>, function sqrt.

raimo

Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

raimo wrote:Sorry, didn't bother to comment on anything but this:
slicknet wrote:if(root==4){root=root/2;} if(root==9){root=root/3;}if(root==16){root=root/4;}
...

This doesn't look too beautiful. I'm not sure what the main point here is, but standard library actually has a square root function. In <cmath>, function sqrt.

yeah not as simple as that. If you use sqrt() then it return an error saying something like cannot mix ints and doubles.
slicknet

Posts: 18
Joined: Sun Oct 05, 2003 4:33 pm

you could always type cast it... w/ rounding maybe... still pretty inaccurate though

Jimbo

Posts: 601
Joined: Thu Sep 25, 2003 6:48 pm
Location: Seattle

i Mean my programme was only meant for prefect square quadratics. And i was just learning at the time. Ive since giving up...lol just keep up now and again.
slicknet

Posts: 18
Joined: Sun Oct 05, 2003 4:33 pm

Jimbo wrote:you could always type cast it... w/ rounding maybe... still pretty inaccurate though

I went thugh all the rounding procudure . It got so complex that i gave up. Bottom line was that it wouldnt run with sqrt becuase of the gcd, it didnt know i only wanted prefct square quads. So it would work out that there is no way of finding the GCD or 2 rational numbers.
slicknet

Posts: 18
Joined: Sun Oct 05, 2003 4:33 pm

You are right, counting integer square roots using a floated point function is an overkill(but the efficiency doesn't make any difference in this case). Casting isn't hard, that's why you have static casts.

Code: Select all
`/* O(log n) Perfect Square function. ERRORS: endless loop on a non-perfect square*/int intsqrt(const int n) {        int min=1, max=n, x;        while((x=(max+min)>>1)&&(x*x==n?0:x*x>n?max=x:min=x));        return x;}`

Just wrote it, doesn't it look nice?

raimo

Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

add a min < max condition to the while loop, and it'll return the closest integer sqrt regardless if the input is a perfect square or not. And max doesn't need to be n, it can start at n/2.

Wizard
Site Admin

Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

If max was n/2(or n>>1), then it wouldn't work for n=1.
I just wanted to keep it short. :P

raimo

Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

Return to Homeworks

### Who is online

Users browsing this forum: No registered users and 1 guest