Concept explainers
Recursive method:
- A method calls itself is as recursive method.
- In recursive function base case will stop recursion and return value instead of calling function.
Program to implement:
- a. Recursive functions for John Napier’s Method to print logarithms.
- b. Modified recursive function to find logarithm of a specific number between “100” and “1000”.
- c. Function to call above function to find logarithm of a number not between “100” and “1000”.
Explanation of Solution
//b. Modified recursive function to find logarithm of a number between 100 and 1000:
//Recursive function to find logarithms of a number between 100 and 1000
void logarithmOf(double first, double second, double firstLog, double secondLog,double num)
{
//If difference between adjacent numbers is greater than tolerance
if (fabs(first - second) > tolerance)
//If number is less than square root of (first*second)
if (num < sqrt(first*second))
//Function calls itself recursively with parameters first,sqrt(first*second),firstLog,(firstLog+secondLog)/2 and num
logarithmOf(first,sqrt(first*second),firstLog,(firstLog+secondLog)/2,num);
//If number is greater than or equal to square root of (first*second)
else
//Function calls itself recursively with parameters sqrt(first*second),second,(firstLog+secondLog)/2,secondLog and num
logarithmOf(sqrt(first*second),second,(firstLog+secondLog)/2,secondLog,num);
//If difference between adjacent numbers is less than or equal to tolerance
else
//Print logarithm of number
cout << "log(" << num << ") = " << firstLog <�...
Explanation of Solution
//c. Function to call modified function to find logarithm of a number not between 100 and 1000:
//Function to find logarithm of number does not between 100 and 1000
void newlogarithm(double first, double second, double firstLog, double secondLog,double num)
{
//If number is less than 10^2 and greater than 10^3
if (num < pow(10.0,2) || num > pow(10.0,3))
//Call function logarithmOf()
logarithmOf(first, second, firstLog, secondLog,num);
//If number is between 100 and 1000
else
//Print error message
cout<<"Input is invalid"<<endl;
}
//Program begins with main() function
int main()
{
//Declare variable
int num;
//Call recursive function logarithm()
cout<<"Result of function logarithm(): "<<endl;
logarithm(2,6,0.3010,0.778);
//Prompt and read number from user
cout<<"Enter number between 100 and 1000: "<<endl;
cin>>num;
//If number is between 100 and 1000
if(num>100 && num<1000...
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
EBK DATA STRUCTURES AND ALGORITHMS IN C
- Write a recursive mathematical definition for computing xn for a positive integer n and a real number x.arrow_forwardA recursive definition for exponentiation on the non-negative integers is partially given as: • expt(k, 0) ::= ? • expt(k, n + 1) ::= k · expt(k, n) What is the value for ?arrow_forwardWrite a recursive implementation of Euclid’s algorithm for finding the greatest common divisor (GCD) of two integers. Descriptions of this algorithm are available in algebra books and on the Web. Write a test program that calls your GCD procedure five times, using the following pairs of integers: (5,20), (24,18), (11,7), (432,226), (26,13). After each procedure call, display the GCD.arrow_forward
- Write a recursive mathematical definition for computing for a positive integer n and a real number x.arrow_forwardThe first examples of recursion are the mathematical functions factorial and fibonacci. These functions are defined for non-negative integers using the following recursive formulas:factorial(0) = 1factorial(N) = N*factorial(N-1) for N > 0fibonacci(0) = 1fibonacci(1) = 1fibonacci(N) = fibonacci(N-1) + fibonacci(N-2) for N > 1Write recursive functions to compute factorial(N) and fibonacci(N) for a given non-negative integer N, and write a main() routine to test your functions.(In fact, factorial and fibonacci are really not very good examples of recursion, since the most natural way to compute them is to use simple for loops. Furthermore, fibonacci is a particularly bad example, since the natural recursive approach to computing this function is extremely inefficient.)arrow_forwardLet F be the function such that F(n) is the sum of the first n positive integers. Give a recursive definition of F(n).arrow_forward
- Give a recursive algorithm for finding the sum of the first n positive integersarrow_forwardGive a recursive algorithm for computing an, where a is a nonzero real number and n is a nonnegative integer.arrow_forwardWrite 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 important problem that arises in many commercial applications. We can efficiently compute the gcd using the following property, which holds for positive integers p and q: If p > q, the gcd of p and q is the same as the gcd of q and p % q.arrow_forward
- Using the recursion three method find the upper and lower bounds for the following recur- rence (if they are the same, find the tight bound). T(n) = T(n/2) + 2T(n/3) + n.arrow_forward9. Ackermann's Function Ackermann's function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a method ackermann (m, n), which solves Ackermann's function. Use the following logic in your method: If m = 0 then return n + 1 If n = 0 then return ackermann (m Otherwise, return ackermann(m 1, 1) 1, ackermann (m, n - 1))arrow_forwardiii. Let K be an integer and suppose F(K) is recursively defined by: F( int k) If (k==0) return 5 else return F(k-1)+2 Find and Trace F(7)arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education