# adding and multiplying fractions

Page 1 of 1

## 2 Replies - 23004 Views - Last Post: 23 February 2007 - 07:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=24323&amp;s=2b8dbe927182ec9e8c318d39766cdbc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 psydoll

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 23-February 07

# adding and multiplying fractions

Posted 23 February 2007 - 01:08 AM

//This is compiling but the adding and the multiplying operation towards the bottom in red is giving values that are totally incorrect including negatives and zeros for whole //numbers
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

using namespace std;
struct Fraction
{
int numerator;
int denominator;
int wholeNumber;
};

void main()
{
Fraction firstFraction;
Fraction secondFraction;
Fraction finalFraction;
Fraction finalFractionNumeratorOne;
Fraction finalFractionNumeratorTwo;
firstFraction.wholeNumber=0;
secondFraction.wholeNumber=0;
finalFraction.wholeNumber=0;
char operation;

cout<<firstFraction.wholeNumber;
cout<<"Enter a numerator for a fraction: "<<endl;
cin>>firstFraction.numerator;
cout<<"Now enter the denominator for that fraction: "<<endl;
cin>>firstFraction.denominator;

//first loop that keeps asking for a denominator until they enter one greater than zero
while(firstFraction.denominator<=0)
{
cout<<"Please enter a number higher than 0 for your denominator: "<<endl;
cin>>firstFraction.denominator;
}
if (firstFraction.numerator<firstFraction.denominator)
cout<<"Your first fraction is:"<<firstFraction.numerator<<" / "<<firstFraction.denominator<<endl;

else
{
do
{
firstFraction.numerator = (firstFraction.numerator-firstFraction.denominator);
firstFraction.wholeNumber = firstFraction.wholeNumber+1;
}
while(firstFraction.numerator >= firstFraction.denominator);

cout<<"Your first fraction is:"<<firstFraction.wholeNumber<<" "<<firstFraction.numerator<<"/"<<firstFraction.denominator<<endl;
}
//end of first fractions loop

cout<<"Enter a numerator for a second fraction: "<<endl;
cin>>secondFraction.numerator;
cout<<"Now enter the denominator for that fraction: "<<endl;
cin>>secondFraction.denominator;

while(secondFraction.denominator<=0)
{
cout<<"Please enter a number higher than 0 for your denominator: "<<endl;
cin>>secondFraction.denominator;
}
if (secondFraction.numerator<secondFraction.denominator)
cout<<"Your first fraction is:"<<secondFraction.numerator<<" / "<<secondFraction.denominator<<endl;

else
{
do
{
secondFraction.numerator = (secondFraction.numerator-secondFraction.denominator);
secondFraction.wholeNumber = secondFraction.wholeNumber+1;
}
while(secondFraction.numerator >= secondFraction.denominator);

cout<<"Your first fraction is:"<<secondFraction.wholeNumber<<" "<<secondFraction.numerator<<"/"<<secondFraction.denominator<<endl;
}
//end of second fraction loop

cout<<"Please enter an arithmetic operation to preform on the two fraction (either a + for addition or a * for multiplication)";
cin>>operation;

//start of the operation loop this is where I am having difficulties
do
{
cout<<"Please enter a + for addition or a * for multiplication";
cin>>operation;
}while (operation!='+' && operation!='*');

if(operation=='+')
{

finalFractionNumeratorOne.numerator=firstFraction.numerator * secondFraction.denominator;

finalFraction.denominator=firstFraction.denominator * secondFraction.denominator;

finalFractionNumeratorTwo.numerator = secondFraction.numerator * firstFraction.denominator;

finalFraction.numerator=finalFractionNumeratorOne.numerator + finalFractionNumeratorTwo.numerator;

finalFraction.wholeNumber = firstFraction.wholeNumber + secondFraction.wholeNumber;

do

{

finalFraction.wholeNumber=finalFraction.wholeNumber+1;

finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;

} while(finalFraction.numerator>=finalFraction.denominator);

if (finalFraction.wholeNumber = 0)

cout<<"the result of adding these two fractions is: " <<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

else

cout<<"the result of adding these two fractions is: " <<finalFraction.wholeNumber<<" "<<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

}

else

{
firstFraction.numerator= firstFraction.numerator + (firstFraction.wholeNumber* firstFraction.numerator);
secondFraction.numerator= secondFraction.numerator + (secondFraction.wholeNumber* secondFraction.numerator);
finalFraction.numerator= firstFraction.numerator * secondFraction.numerator;
finalFraction.denominator= firstFraction.denominator * secondFraction.denominator;
do
{
finalFraction.wholeNumber++;
finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;
}
while(finalFraction.numerator>=finalFraction.denominator);

if (finalFraction.wholeNumber = 0)

cout<<"the result of multiplying these two fractions is: " <<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

else

cout<<"the result of multiplying these two fractions is: " <<finalFraction.wholeNumber<<" "<<finalFraction.numerator<<"/"<<finalFraction.denominator<<endl;

}

getch();
}

Is This A Good Question/Topic? 0

## Replies To: adding and multiplying fractions

### #2 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: adding and multiplying fractions

Posted 23 February 2007 - 02:12 AM

If we say a mixed fraction is W+N/D and we want to convert this to a fraction we multiply W by D and add it to N ---> ((W*D)+N)/D

Your program uses ((W*N)+N)/D.

You use this structure:
do
{
finalFraction.wholeNumber++;
finalFraction.numerator=finalFraction.numerator-finalFraction.denominator;
}
while(finalFraction.numerator>=finalFraction.denominator);

to convert between a fraction and a mixed number. This is not the same thing as you used above in your program when getting the other values. This bit of code seems to be at fault.
Was This Post Helpful? 0

### #3 msg555

• D.I.C Head

Reputation: 9
• Posts: 136
• Joined: 04-May 06

## Re: adding and multiplying fractions

Posted 23 February 2007 - 07:51 AM

Is there any reason you want to convert to whole number + fraction < 1 form? It's much easier to work with them in N/D form.

To add two fractions

N1/D1 + N2/D2 = (N1 * D2 + N2 * D1) / (D1 * D2)

To Multiply two fractions
N1/D1 * N2/D2 = (N1 * N2) / (D1 * D2)

To simplify a fraction

Let gcd be defined as
int gcd(int n, int d)
{
if(n == 0) return d;
return gcd(d % n, n);
}

Then N can be simplified to N/gcd(N, D) and D can be simplified to D/gcd(N, D)
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }