Do it Using CPP Language. Thanks. Objective: Become proficient with recursion by
ID: 3865577 • Letter: D
Question
Do it Using CPP Language. Thanks.
Objective: Become proficient with recursion by implementing the two algorithms below, and manipulating linked lists with recursion. Description of Fibonacci function: The Fibonacci sequence is the series of integers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, .. Note that each element in the series is the sum of the preceding two items. The formula for calculating the nth number of the sequence is given by: fib(n) = {n, if n = 0 or 1 fib (n - 1) + fib (n - 2) n > 1 Description of squareroot function: The following expression defines a function that calculates an approximation of the squareroot of a number, starting with an approximate answer (est), within the specified tolerance (to1). squareroot (n, est, tol) = est, if |est^2 - n| lessthanorequalto tol squareroot (n, est^2 + n/2est, tol) if|est^2 - n| > tol Linked List manipulation using recursion: Perform the following operations on a linked list of words (strings). The words are to be stored using dynamically allocated memory (not an array). Write functions to perform the following tasks. Insertion Length Shortest and Longest word Contains a word (search) Deliverables: A complete program to calculate Fibonacci numbers, calculate squareroot s and manipulate linked lists of words. A program design sheet. Describe all functions necessary to implement your program. A sample calculation sheet. Show the expected values for both the Fibonacci series and the squareroot . Programming Log: - Record the time required to design and implement your program.Explanation / Answer
1. Fibonacci Program
////////////////////////////////////////fibonacci.cpp//////////////////////////////////////
#include <iostream>
using namespace std;
int main()
{
int num1=0,num2=1,num3,i,num;
cout<<"Enter the number of elements to dispaly: ";
cin>>num;
cout<<num1<<" "<<num2<<" "; //printing 0 and 1
for(i=2;i<num;++i) //loop starts from 2 because 0 and 1 are already printed
{
num3=num1+num2;
cout<<num3<<" ";
num1=num2;
num2=num3;
}
return 0;
}
Output :
Enter the number of elements to display: 10
2. Sqaure Root Problem
#include <iostream>
#include <cmath> // std::abs
using namespace std;
// function declaration
int sqrt_new(int n, int est,int tol);
int main () {
// local variable declaration:
int n = 100;
int est = 1;
int tol=10;
int ret;
// calling a function to get sqrt value.
ret = sqrt_new(n, est,tol);
cout << "Sqrt value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int sqrt_new(int n, int est,int tol) {
// local variable declaration
int result,y;
int x= abs(est*est-1);
if(x<=tol)
{
result=est;
}
else
{
y=(est*est+n)/(2*est);
result=sqrt_new(n,y,tol);
}
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.