2 Replies - 2429 Views - Last Post: 13 December 2012 - 09:53 PM

#1 DeeViLiSh   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Add & Reduce 2 fractions

Posted 28 August 2006 - 10:41 PM

Description: Use this like you want, no credits neededInput two fractions, program outputs them added and reduced
#include<iostream.h>

//Inputs the fractions
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)

{
        cout << "Enter the numerator for the first fraction: ";
        cin >> Num;
        cout << "Enter the denominator for the first fraction: ";
        cin >> Denom;
        cout << endl;
        cout << "Enter the numerator for the second fraction: ";
        cin >> Num2;
        cout << "Enter the denominator for the second fraction: ";
        cin >> Denom2;
        cout << endl;
        
}



void Reduce(int &Num, int &Denom, int &Num2, int &Denom2)

{
        int a, b, c, d, i, j = 0;

        a = Denom;
        b = Num;
        c = Denom2;
        d = Num2;


        for (i = a * b; i > 1; i--)
        {
                if ((a % i == 0) && (b % i == 0))
                {
                        a /= i;
                        b /= i;
                }
        }
        
        for (j = 50; j > 1; j--)
        {
                if ((c % j == 0) && (d % j == 0))
                {
                        c /= j;
                        d /= j;
                }
        }
        
        Denom = a;
        Num = b;
        Denom2 = c;
        Num2 = d;

}



void Reduce(int &Num, int &Denom)
{
        int a = 0;
        int b = 0;
        int i = 0;


        a = Denom;
        b = Num;


        for (i = 50; i > 1; i--)
        {
                if ((a % i == 0) && (b % i == 0))
                {
                        a /= i;
                        b /= i;
                }
        }


        Denom = a;
        Num = b;
}




//Adds the fractions
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
        if (Denom != Denom2)
        {


                Num = Num * Denom2;
                Num2 = Num2 * Denom;
                Denom = Denom * Denom2;
                Denom2 = Denom2 * Denom;
                Num = Num + Num2;
        }
        else
        {
                Num = Num + Num2;
        }

        Reduce(Num, Denom);
}




//Outputs added & reduced fractions
void DisplayFraction(int &Num, int &Denom)
{
        cout << "The reduced and added fraction is " << Num << "/" << Denom << endl;
}





int main()
{
        char an;


        do
        {
                int Num, Denom, Num2, Denom2 = 0;

                ReadFraction(Num, Denom,Num2,Denom2);
                Reduce(Num, Denom, Num2, Denom2);
                AddFraction(Num, Denom, Num2, Denom2);
                DisplayFraction(Num, Denom);
                cout << endl;

                cout <<"Would you like to do another fraction? (Y for yes) ";
                cin >> an;
                cout << endl;
        }while ((an == 'y') || (an == 'Y')); //Continues or not


        return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Add & Reduce 2 fractions

#2 chandravir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 16-December 08

Re: Add & Reduce 2 fractions

Posted 16 December 2008 - 07:23 AM

it's verygood
Was This Post Helpful? 0
  • +
  • -

#3 antolyevich   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 12
  • Joined: 02-September 12

Re: Add & Reduce 2 fractions

Posted 13 December 2012 - 09:53 PM

Using a counter i for reduction is a really inefficient way to work with larger fraction, maybe array sort the factors and binary search for common factors, bubble sort and take the lowest
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1