Question
Write a C++ program that implements Algorithm 5.1.1 page 241 using the steps and data structures shown. ak = k / k + 1 for
Write a C++ program that implements Algorithm 5.1.1 page 241 using the steps and data structures shown.
ak = k / k + 1 for all integers k 1,
bi = i 1 / i for all integers i 2.
Solution a1 = 1 / 1 + 1 = 1 / 2 b2 = 2 1 / 2 = 1/2 a2 = 2 / 2 + 1= 2 / 3 b3 = 3 1 / 3 = 2 / 3
a3 = 3 / 3 + 1 = 3 / 4 b4 = 4 1 / 4 = 3 / 4 a4 = 4 / 4 + 1 = 4 / 5 b5 = 5 1 / 5 = 4 / 5
a5 = 5 / 5 + 1= 5/6
Use the division algorithm shown in Epp, rather than integer or division operators in C++.
You can use the code from last week for the division algorithm. Write the code to prompt the user for a decimal number and print out the binary result. The program should be set up to repeat the process 4 times. The program should respond gracefully (but not end) if an incorrect input occurs. The program should print out the binary result with space every 4 bits as in our homework.
This is my old code
#include
#include
#include
#include
#include
using namespace std;
// Function to return gcd of a and b using
// Euclidean algorithm
int gcd(int long long a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
// Driver program to test above function
int main()
{
//long long: -2^63+1 to +2^63-1
long long x;
int y;
// have a check that 1 <= x <= 2^63
// check that x is in the limit of long long
// Keeping it under while makes it ask for the valid value once gain
while (std::cout << "Enter the value of x in range -2^63+1 to +2^63-1 " << " " && !(std::cin >> x))
{
cout << "Bad value!" << " ";
cin.clear();
cin.ignore(numeric_limits
};
do {
cout << "Enter the value of Y less than X and greater than 1" << " ";
} while (!(cin >> y && y < x && y >= 1));
cout << "Gcd of x , y " << x << "," << y << " is " << gcd(x, y);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started