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
- Q.2. Architecture performance [25 marks] Consider two different implementations, M1 and M2, of the same instruction set. M1 has a clock rate of 2 GHz and M2 has a clock rate of 3.3 GHz. There are two classes of instructions with the following CPIs: Class A CPI for M1 CPI for M2 2.f 1.g B 5 3 C 6 4 Note that the dots in 2 fand 1.g indicate decimal points and not multiplication. a) What are the peak MIPS performances for both machines? b) Which implementation is faster, if half the instructions executed in a certain program are from class A, while the rest are divided equally among classes B and C. c) What speedup factor for the execution of class-A instructions would lead to 20% overall speedup? d) What is the maximum possible speedup that can be achieved by only improving the execution of class-A instructions? Explain why. e) What is the clock rate required for microprocessor M1 to be a "1000 MIPS" (not peak MIPS) processor?arrow_forwardPLEASE SOLVE STEP BY STEP WITHOUT ARTIFICIAL INTELLIGENCE OR CHATGPT I don't understand why you use chatgpt, if I wanted to I would do it myself, I need to learn from you, not from being a d amn robot. SOLVE STEP BY STEP I WANT THE DIAGRAM PERFECTLY IN SIMULINKarrow_forwardI need to develop and run a program that prompts the user to enter a positive integer n, and then calculate the value of n factorial n! = multiplication of all integers between 1 and n, and print the value n! on the screen. This is for C*.arrow_forward
- I need to develop and run a C* program to sum up integers from 1 to 100, and print out the sum value on the screen. Can someone help please?arrow_forwardGiven the schema below for the widgetshop, provide a schema diagram. Schema name Attributes Widget-schema Customer-schema (stocknum, manufacturer, description, weight, price, inventory) (custnum, name, address) Purchased-schema (custnum, stocknum, pdate) Requestedby-schema (stocknum, custnum) Newitem-schema (stocknum, manufacturer, description) Employee-schema (ssn, name, address, salary) You can remove the Newitem-schema (red).arrow_forwardTrue or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forward
- Can you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward(use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward
- 1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forwardmake a screen capture showing the StegExpose resultsarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT