Question

C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

C++

Problem 1

Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:

function gcd(a, b)

                while b ≠ 0

                t := b

         b := a mod b

         a := t

    return a

Problem 2

Write a program that works with fractions. Your program should be able to add, subtract, multiply, and divide two fractions. Specifically, your program must request two fractions from the user by getting the numerator and denominator separately for each fraction. It must also get the operation to perform (add, subtract, multiply, or divide) as a single character. That is, the user should enter '+', '-', '*', or '/' for the operation. Be sure to validate the input for the operation. Your program will then compute the resulting fraction, keeping the numerator and denominator separate, and output the result. The resulting fraction must be simplified. For example, (4 / 6) should be displayed as (2 / 3), and (8 / 8) should be displayed as (1 / 1).

You will need to use the GCD function from Problem 1 to simplify the answer, so be sure to test it thoroughly before using it here. You must also write a separate function for each fraction operation. That is, you must write a function that adds two fractions, another that subtracts two fractions, a third that multiplies two fractions, and a fourth that divides two fractions. All input and output should happen in main ( ). These functions are only for handling fractional math. Note that each of these functions should have six arguments: two for the first fraction, two for the second fraction, and two for the answer. The resulting fraction must therefore be passed into and out of the function using call by reference arguments.

You must compute the resulting fraction using fraction-based math (working with numerators and denominators). Do not convert the fractions to double values (like 1.5), do the math, and convert back to a fraction. There are numerous resources on the Internet if you need a refresher on how to add, subtract, multiply, and divide fractions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>

using namespace std;

int gcd(int a, int b)
{
while(b!=0)
{
int t = b;
b = a % b;
a = t;
}

return a;
}

//simplify
void simplify(int &n, int &d)
{
   int i, j, k;

   j = abs(n);
   k = abs(d);

   i = gcd(j, k);

   n = n/i;
   d = d/i;

   if((n<0 && d<0) || d<0)
   {
       n = -n;
       d = -d;
   }
}


//addition
void add(int n1, int d1, int n2, int d2, int& n3, int& d3)
{
d3 = d2 * d1;
n3 = n2 * d1 + d2 * n1;
simplify(n3, d3);
}

//addition
void subtract(int n1, int d1, int n2, int d2, int& n3, int& d3)
{
d3 = d2 * d1;
n3 = n2 * d1 - d2 * n1;
simplify(n3, d3);
}

//multiplication
void multiply(int n1, int d1, int n2, int d2, int& n3, int& d3)
{
d3 = d2 * d1;
n3 = n2 * n1;
simplify(n3, d3);
}

//division
void divide(int n1, int d1, int n2, int d2, int& n3, int& d3)
{
d3 = n2 * d1;
n3 = d2 * n1;
simplify(n3, d3);
}


//main function
int main()
{
int n1, d1, n2, d2, n3, d3;
char c;

cout<<"Enter the numerator and denominator: ";
cin>>n1>>d1;

cout<<"Enter the numerator and denominator: ";
cin>>n2>>d2;

cout<<"Enter the operation (+ , -, * or /): ";
cin>>c;

switch(c)
{
case '+': add(n1, d1, n2, d2, n3, d3);
cout<<n1<<"/"<<d1<<" + "<<n2<<"/"<<d2<<" = ";
cout<<n3<<"/"<<d3<<endl;
break;
case '-': subtract(n1, d1, n2, d2, n3, d3);
cout<<n1<<"/"<<d1<<" - "<<n2<<"/"<<d2<<" = ";
cout<<n3<<"/"<<d3<<endl;
break;
case '*': multiply(n1, d1, n2, d2, n3, d3);
cout<<n1<<"/"<<d1<<" * "<<n2<<"/"<<d2<<" = ";
cout<<n3<<"/"<<d3<<endl;
break;
case '/': divide(n1, d1, n2, d2, n3, d3);
cout<<n1<<"/"<<d1<<" / "<<n2<<"/"<<d2<<" = ";
cout<<n3<<"/"<<d3<<endl;
break;
default:
cout<<"Wrong input!!"<<endl;
}

return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • Must write in Java - ignore the Junit tests Write a program that works with fractions....

    Must write in Java - ignore the Junit tests Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-element array...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

  • coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers...

    coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers (of which at least one is nonzero) is the largest positive integer that divides the numbers. Write a C function ged that accepts two integers and returns 1 if both the integers are zero, otherwise it returns their GCD. Write a C program (that includes the function ged) which accepts two integers and prints their GCD. Sample output: Enter two integers: 0 0 At...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT