Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ problem : Euclids method for finding the greatest common divisor (GCD) of two positive integers is given by the following algorithm: a)Divide the larger

C++ problem: Euclids method for finding the greatest common divisor (GCD) of two positive integers is given by the following algorithm: a)Divide the larger number by the smaller and retain the remainder. b)Divide the smaller number by the remainder, again retaining the remainder. c)Continue dividing the prior remainder by the current remainder until the remainder is zero, at which point the last nonzero remainder is the greatest common divisor.

For example, find the GCD of 72 and 114: 114/72 = 1 with remainder 42 72/42 = 1 with remainder 30 42/30 = 1 with remainder 12 30/12 = 2 with remainder 6 12/6 = 2 with remainder 0 So, the GCD of 72 and 114 is 6, the last nonzero remainder.

Using Euclids method, write a C++ function, gcd, that will take in two positive integers, determine the GCD of the integers, and return the result.

Then, modify the fraction calculator problem from problem 3 to include your gcd function and use it to produce reduced fraction results. That is, after you calculate the numerator and denominator of a fraction arithmetic operation, find their GCD and then divide each by the GCD to obtain the reduced fraction numerator and denominator.

A sample run:

Enter first fraction: 2/4 Enter operation: - Enter second fraction: 5/6 Difference = 1/-3 Continue (y or n)? y Enter first fraction: 2/4 Enter operation: + Enter second fraction: 5/6 Sum = 4/3 Continue (y or n)? n

-------------------------------- Process exited after 105.3 seconds with return value 0 Press any key to continue . . .

problem 3 Write a program that implements a four-function (+, , *, /) calculator for fractions. The program should prompt for and read the first fraction, an arithmetic operator, and a second fraction, perform the required calculation, display the result, and then ask if the user wants to continue. The result should be displayed as a fraction - it does not need to be in reduced form.

Program requirements: a)Write a program that does not make use of any floating point data nor variables. Read the numerator and denominator of each fraction into separate integer variables (using the same technique given in the first problem). Then produce the numerator and denominator of the resulting fraction using basic fraction arithmetic. For example, a/b + c/d = (ad+bc)/bd. b)Make use of a switch statement to organize the calculations associated with the different arithmetic operations. c)Use a do loop to control the calculator repetition.

Here is an example of a users interaction with the program:

Enter first fraction: 1/2 Enter operation: + Enter second fraction: 1/3 Sum = 5/6 Continue (y or n)? y Enter first fraction: 1/2 Enter operation: - Enter second fraction: 1/3 Difference = 1/6 Continue (y or n)? y Enter first fraction: 1/2 Enter operation: / Enter second fraction: 1/3 Quotient = 3/2 Continue (y or n)? n

-------------------------------- Process exited after 56.37 seconds with return value 0 Press any key to continue . . .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago