Question

In: Computer Science

Write a program in C++ that calculates the sum of two fractions. The program should ask...

Write a program in C++ that calculates the sum of two fractions. The program should ask for the numerators and denominators of two fractions and then output the sum of the two fractions. You will need to write four functions for this program, one to read the inputted data, one to calculate the sum of the two fractions, one to find the Greatest Common Divider (GCD) between the numerator and denominator and a function that will display the end result. The program should execute in a loop, asking the use if they wish to continue. The output should look similar as that below. Assume the user will only enter positive numbers, and neither denominator will ever be zero.
This program adds two fractions and displays the answer in reduced form.
Enter numerator 1: 1
Enter denominator 1: 3
Enter numerator 2: 1
Enter denominator 2: 6
1/3 + 1/6 = 1/2
Would you like to continue? (y/n)
After using the above formula, then reduce the answer by dividing the numerator and denominator by their GCD.
The prototypes for the four functions you will need to write are as follows:
void readTwoFractions(int &n1, int &d1, int &n2, int &d2);
void addTwoFractions(int n1, int d1, int n2, int d2, int &num, int &den);
int greatestCommonDivider(int top, int bottom);
void displaySumOfFractions(int n1, int d1, int n2, int d2, int top, int bottom);

Solutions

Expert Solution

Thanks for the question, Here is the complete program in C++

========================================================================

#include<iostream>

using namespace std;

void readTwoFractions(int &n1, int &d1, int &n2, int &d2){

cout<<"Enter numerator 1: "; cin>>n1;

cout<<"Enter denominator 1: "; cin>>d1;

cout<<"Enter numerator 2: "; cin>>n2;

cout<<"Enter denominator 2: "; cin>>d2;

}

void addTwoFractions(int n1, int d1, int n2, int d2, int &num, int &den){

num = n1*d2 + n2*d1;

den = d1*d2;

}

int greatestCommonDivider(int top, int bottom){

if(bottom==0)return top;

else

return greatestCommonDivider(bottom,top%bottom);

}

void displaySumOfFractions(int n1, int d1, int n2, int d2, int top, int bottom){

cout<<n1<<"/"<<d1<<" + "<<n2<<"/"<<d2<<" = "<<top<<"/"<<bottom<<endl;

}

int main(){

int n1, d1, n2, d2, num , den;

    char calculate_again;
               do{
               readTwoFractions(n1,d1,n2,d2);
addTwoFractions(n1,d1,n2,d2, num,den);
int greatest_divisor = greatestCommonDivider(num,den);
num/=greatest_divisor;
den/=greatest_divisor;
displaySumOfFractions(n1,d1,n2,d2, num,den);
               cout<<"\nWould you like to continue? (y/n) ";cin>>calculate_again;
              
               }while(calculate_again!='n');


}

============================================================================


Related Solutions

Write a program that calculates the area and circumference of a circle. It should ask the...
Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159 Sample Output: Enter the number...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two file names. You will read the characters from each file and put them in separate queues. Then, you will read each character from each queue and compare it. If every character from both queues is the same, you will print “The files are identical.” Otherwise, you will print “The files are not identical.” Step-by-Step Instructions Create a character queue (named queue1) using the Standard...
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $ 0.35 per mile. Your program should interact with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading: 13505.2 Enter ending odometer reading     : 13810.6 You traveled 305.40 miles. At $ 0.35 per mile, your reimbursement is $ 106.89. The values in red color are inputs for the program. The values in blue color are results of expressions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT