# Adding a series of fractions

 Patricia Ghann · January 6, 2015 Hello Bucky, Happy New Year. Please I want to write a program in c++ to add a series of fractions and am finding it difficult so I need your help please. I want to add for example 1/3+3/5+5/7+7/9+9/11+...............+97/99.

## Replies

- page 1

 Patricia Ghann · January 8, 2015 Thanks a lot guys for your help. c student · January 8, 2015 doesn't matter to me.  i only do this so i can learn and if they end up learning too, then that's fine with me.  if they end up failing to provide at a given time, well that's just too bad. c student · January 7, 2015 here it is again, this time with simplified fraction result using euclid's gcd algorithm.  i apologize if the algorithm is hard to understand, i had just pulled it out of a piece of code i wrote last year.``#include #include // define a struct for a fraction type to hold// both numerator and denominatortypedef struct {    int num;    int den;} fraction;fraction fracAdd (fraction, fraction);fraction simplify (fraction frac);int main (void) {    // request amount of fractions    puts ("Please enter the amount of fractions");    // acquire number of fractions to be calculated    int amount = getchar()-48;    // declare array of fraction types according to the previous line    fraction frac[amount];    // declare a resultant fraction    fraction result;    // loop to enter in each fraction    for (int n = 0; n < amount; n++) {        // request numerator for fraction n        printf ("Please enter in the numerator for fraction %d\n", n+1);        // input into fraction array n for numerator member        scanf ("%d", &frac[n].num);        // request denominator for fraction n        printf ("Please enter in the denominator for fraction %d\n", n+1);        // input into fraction array n for denominator member        scanf ("%d", &frac[n].den);        if (frac[n].den == 0) {            fprintf (stderr, "\nZero is not a valid denominator value\n");            exit (1);        }        // this is just to place the first fraction into result        if (n == 0) {            result = frac[n];        // then for the rest of the fractions, add them onto the result        } else {            result = fracAdd (result, frac[n]);        }    } // simplify result with GCD    result = simplify (result);    // print result with numerator and denominator    printf ("Result: %d/%d\n", result.num, result.den);    return 0;}// function to add fractionsfraction fracAdd (fraction a, fraction b) {    // declare resultant fraction    fraction result;    // adding numerators    result.num = a.num*b.den+b.num*a.den;    // adding common denominator    result.den = a.den*b.den;    // return resultant    return result;}// Euclidean algorithm for GCDfraction simplify (fraction frac) {    int qot, rmd, m, n;    int i = 0;    int j = 0;    n = frac.num;    m = frac.den;        do {        if (n >= m) {            j = 1;            qot = n/m;            rmd = n % m;            if (rmd == 0) {                break;            }            if (m == 0) {                break;            }            n = m;            m = rmd;        } else if (m > n) {            j = 2;            qot = m/n;            rmd = m % n;            if (rmd == 0) {                break;            }            if (n == 0) {                break;            }            m = n;            n = rmd;        }        i++;    } while (i < 10);    if (j == 1) {        frac.num /= m;        frac.den /= m;    } else if (j == 2) {        frac.num /= n;        frac.den /= n;    }    return frac;}``note: have not tested for all cases, however should work nicely Number Double07 · January 7, 2015 Nice, that works!  Thanks man. Number Double07 · January 7, 2015 Oh, I see what's happening in my code.  It's adding up the -1 inputs for my numerator and denominator before it ends the while loop.  So every answer I get is 1 greater than the actual answer.  I could just subtract 1 from the sum I guess when I cout it.  However, is there another way to prevent the loop from adding that "trip" values of -1? Number Double07 · January 7, 2015 ``#includeusing namespace std;int main(){float num = 0;float den = 0;int numberOfFractionEntered = 0;float fraction = 0;float sum = 0;while (num !=-1){cout << "Enter numerator or enter -1 to quit: " <> num;cout << "Enter denominator or enter -1 to quit " <> den;numberOfFractionEntered ++;fraction = (num / den);sum = fraction + sum;}cout << "The sum of of the fractions entered is " << sum  << endl;return 0;}``It keeps giving me an answer more than what I want.  Like, I enter .5 + .5 +.5, and I get 2.5. Number Double07 · January 7, 2015 I'm trying to work it out but I'm having a problem.  It's giving me an answer more than the answer I want. c student · January 7, 2015 if it's not too late, here is my functioning code written in C since i dont code in c++ however should be easy to translate across.  i have added comments to show psuedo code for better understanding:``#include #include // define a struct for a fraction type to hold// both numerator and denominatortypedef struct {    int num;    int den;} fraction;fraction fracAdd (fraction, fraction);int main (void) {    // request amount of fractions    puts ("Please enter the amount of fractions");    // acquire number of fractions to be calculated    int amount = getchar()-48;    // declare array of fraction types according to the previous line    fraction frac[amount];    // declare a resultant fraction    fraction result;    // loop to enter in each fraction    for (int n = 0; n < amount; n++) {        // request numerator for fraction n        printf ("Please enter in the numerator for fraction %d\n", n+1);        // input into fraction array n for numerator member        scanf ("%d", &frac[n].num);        // request denominator for fraction n        printf ("Please enter in the denominator for fraction %d\n", n+1);        // input into fraction array n for denominator member        scanf ("%d", &frac[n].den); if (frac[n].den == 0) { fprintf (stderr, "Zero is not a valid denominator value"); exit (1); }        // this is just to place the first fraction into result        if (n == 0) {            result = frac[n];        // then for the rest of the fractions, add them onto the result        } else {            result = fracAdd (result, frac[n]);        }    }    // print result with numerator and denominator    printf ("Result: %d/%d\n", result.num, result.den);    return 0;}// function to add fractionsfraction fracAdd (fraction a, fraction b) {    // declare resultant fraction    fraction result;    // adding numerators    result.num = a.num*b.den+b.num*a.den;    // adding common denominator    result.den = a.den*b.den;    // return resultant    return result;}``good lucknote: does not simplify fractions.  if you wish to simplify the fractions, just make a function to calculate gcd (i think). Number Double07 · January 7, 2015 It would be easier if you requested the user to input the numerator, then the denominator.  I tried the fraction input method using your code, but I'm not sure what is going on there myself.  It's more complicated, it can probably be done, but I don't know how to do it.  Also, both requests (cout statements of numerator and denominator)  need to be inside the while loop so it knows to end when it user inputs the -1 value. c student · January 7, 2015 you may want to declare a struct fraction which contains members num and den.  when you request input, you could ask how many fractions they may want to enter in which sets the amount of loops.  you will need to input the numerator and denominator separately for each fraction. once that's done, you may want to create a loop with a function containing a fraction-adding algorithm.  > fractionResult.num = fraction1.num*fraction2.den + fraction2.num*fraction1.den> fractionResult.den = fraction1,den*fraction2.denan example: if you want to calculate 1/3 + 3/5:> fractionResult.num = 1*5+3*3 = 5+9 = 14> fractionResult.den = 3*5 = 15resulting in a fraction result struct representing 14/15, save the value and add on the next input fraction
• 1
• 2

## C++

129,731 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
 Bucky Roberts Administrator