Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
Question
Book Icon
Chapter 13, Problem 1PP
Program Plan Intro

Program plan:

1 . The following variables are used in the program:

  • seq variable is of integer type and used to store the user-entered Fibonacci sequence.
  • nvariable is of integer type and used as a parameter of the function name findFibonacciNumber.

2. The following functions are used in the program:

  • findFibonacciNumber ()function is used to find the Fibonacci number of the user-entered sequence.
  • main () function is used to get the user input, call the function, and display the Fibonacci number returned by the function.

Program description:

The main purpose of the program is to create a function named findFibonacciNumber . This function get the sequence of the Fibonacci series as parameter, then calculate the Fibonacci number forsequence, and return the Fibonacci number.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

//including essential header file
#include <iostream>

//using standard namespace
using namespace std;

//function to find the Fibonacci number of the given sequence
int findFibonacciNumber(int n)
{
//when the fibonacci sequence is equal to 0 or 1
if (n == 0 || n == 1)
    {
//return 1
return 1;
    }

//call the function recursively to find the fibonacci number
return findFibonacciNumber(n-1) + findFibonacciNumber(n-2);
}
//main function
int main() 
{
//variable declaration to store the sequence of Fibonacci number
int seq;
//prompt the user to enter the fibonacci sequence
cout<<"Enter the fibonacci sequence: ";
//get the value from the user
cin>>seq;
//call the function to find and display the fibonacci number of the given sequence
cout<<"The fibonacci number of sequence "<<seq<<" is "<<findFibonacciNumber(seq);
}

Explanation:  

In the above code, a recursive function named findFibonacciNumber is used to find the Fibonacci number of the user-entered sequence. It has a parameter named n of integer type. It gets the sequence of the Fibonacci series from the main () function and stored in the parameter named n. When the value of the Fibonacci sequence is 0 or 1 then this function returns 1. For the other values this function return the Fibonacci number for the given sequence by adding the immediate last two Fibonacci number by calling itself recursivelyand decreasing the value of the parameter by 1 (to get the immediate last value) and 2 (to get the second last value).

In the main function, create a variable named seq of integer type to store the user-entered Fibonacci sequence. Prompt the user to enter the Fibonacci sequence. Store the user-entered Fibonacci sequence in the variable name seq. Call the function named findFibonacciNumber with seq as parameter. Display the function returned value.

Sample output:

Absolute C++, Chapter 13, Problem 1PP , additional homework tip  1

Absolute C++, Chapter 13, Problem 1PP , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
For function log, write the missing base case condition and the recursive call. This function computes the log of n to the base b. As an example: log 8 to the base 2 equals 3 since 8 = 2*2*2. We can find this by dividing 8 by 2 until we reach 1, and we count the number of divisions we make. You should assume that n is exactly b to some integer power. Examples: log(2, 4) -> 2 and  log(10, 100) -> 2   public int log(int b, int n ) {  if <<Missing base case condition>> {    return 0;  } else {    return <<Missing a Recursive case action>>  }}
Implement a recursive C++ function which takes two integers num and den as arguments and returns theinteger quotient that will result when num is divided by den. The prototype of your function should be:int quotient (int num, int den)Hint: Think about repeated subtraction
Write a recursive fibonacci (n) function with an expression body.  The function should return an Int; it will be too slow to deal with inputs whose fibonacci numbers are too large anyway. You do *not* need to use memoization.    Note that: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n, where n is greater than 1) = fibonacci(n-2) + fibonacci(n - 1)

Chapter 13 Solutions

Absolute C++

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning