Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1:47 PM 10% .oooo H20 ehacc hacc.edu Fractions Task: Add several pairs of fracti

ID: 3798509 • Letter: 1

Question

1:47 PM 10% .oooo H20 ehacc hacc.edu Fractions Task: Add several pairs of fractions and sim the resulting sums. plity The program should be able to manage signed numbers, like or unlike denominators, and input values ofzero in either a numerator or a denominator. Azero denominator should be handled by printing an appropriate error message in the output file and without causing a program abend. The final sums should be reduced to lowest terms, and improper fractions should be converted to mixed numbers. Use the Euclidean algorithm to find the GCF to simplify fractions. Organize the program with appropriate functions. The program will be submitted in C++, with pseudocode. Input: For each pair of fractions, accept input values for numerator 1, denominator 1, numerator 2, and denominator 2. The input data file is: P5Fractions.txt. All the input values are integers. Here are the fractions that will be added: Output: Print an appropriate header in the output file, and then print each pair of addend fractions and their sum. Note that negative signs should not be displayed in a denominator. If a mixed number is negative, print only one minus sign (i.e., print 1 1/4 rather than 1-1/4). If the final result is an integer, no fraction portion should be displayed. If the final result is only a fraction with no whole number part, do not print a whole number of o. Sample output 3/8 1/6 13/24 9/2 7/2 8/9 11/3 4 5/9 1/6 5/6 2/3 Suggestions: You might want to simplify the answer after combining the fractions. The following formula provides one way to add fractions: di di .Be particularly careful calculating the signs of the answers since the Euclidian algorithm may return a negative GCF.

Explanation / Answer

program:--

#include <iostream>
#include <fstream>
#include <math.h>
int n1,n2,d1,d2,sn,sd;

int min()
{
   return (abs(sn) >= abs(sd)) ? abs(sd) : abs(sn);   //abs() used for avoiding -ve values while finding GCD
}
  
int max()
{
   return (abs(sn) >= abs(sd)) ? abs(sn) : abs(sd);   //abs() used for avoiding -ve values while finding GCD
}

int gcd() // returns the greatest common divisor of num and denom
{
   int mn = min(); // min of num and denom
    int mx = max(); // mX of num and denom
   for (int x = mn; x > 0; x--) // find the greatest common divisor
       if (mx % x == 0 && mn % x == 0)
          return x;
   return 1;
}

void reduce() // simplify the Fraction number
{
   int tmp = gcd();
    sn /= tmp;
    sd /= tmp;
}

int main(int argc,char *argv[])
{
    std::fstream myfile("data.txt", std::ios_base::in);
    std::fstream myfile1("data1.txt", std::ios_base::out);
  
    while (myfile >> n1 >> d1 >> n2 >> d2)       //read from file
    {
        myfile1<<n1<<"/"<<d1<<" + "<<n2<<"/"<<d2<<" = ";
        sn=n1*d2+n2*d1;
        sd=d1*d2;
        reduce();
        if(sd==0)
           myfile1<<"Error ! ";
        else if(sn==0)
           myfile1<<"0 ";
        else if(sd<0&&sn>0)
           myfile1<<"-"<<sn<<"/"<<abs(sd)<<" ";
        else if(sd<0&&sn<0)
           myfile1<<abs(sn)<<"/"<<abs(sd)<<" ";
        else
           myfile1<<sn<<"/"<<sd<<" ";
    }
    std::cout<<"Operation completed go to correct folder and open data1.txt file for out put";
    myfile.close();
    myfile1.close();
   getchar();
   return 0;
}

output:--

input file is       data.txt

5 17 2 17
3 8 1 6
9 2 7 2
8 9 11 3
-25 1 1 2
1 6 -5 6
1 4 -1 4
-1 3 -7 8
0 2 -3 -4
-9 11 -10 11
3 7 4 0
-3 -5 -15 -7

data1.txt file is

5/17 + 2/17 = 7/17
3/8 + 1/6 = 13/24
9/2 + 7/2 = 8/1
8/9 + 11/3 = 41/9
-25/1 + 1/2 = -49/2
1/6 + -5/6 = -2/3
1/4 + -1/4 = 0
-1/3 + -7/8 = -29/24
0/2 + -3/-4 = 3/4
-9/11 + -10/11 = -19/11
3/7 + 4/0 = Error !
-3/-5 + -15/-7 = 96/35

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote