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
Suppose you buy an electronic device that you operate continuously. The device costs you $300 and carries a one-year warranty. The warranty states that if the device fails during its first year of use, you get a new device for no cost, and this new device carries exactly the same warranty. However, if it fails after the first year of use, the warranty is of no value. You plan to use this device for the next six years. Therefore, any time the device fails outside its warranty period, you will pay $300 for another device of the same kind. (We assume the price does not increase during the six-year period.) The time until failure for a device is gamma distributed with parameters α = 2 and β = 0.5. (This implies a mean of one year.) Use @RISK to simulate the six-year period. Include as outputs (1) your total cost, (2) the number of failures during the warranty period, and (3) the number of devices you own during the six-year period. Your expected total cost to the nearest $100 is _________,…
Which one of the 4 Entities mention in the diagram can have a recursive relationship?    If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type (ID or Non-ID, also Justify why)?
Please answer the JAVA OOP Programming Assignment scenario below: Patriot Ships is a new cruise line company which has a fleet of 10 cruise ships, each with a capacity of 300 passengers. To manage its operations efficiently, the company is looking for a program that can help track its fleet, manage bookings, and calculate revenue for each cruise. Each cruise is tracked by a Cruise Identifier (must be 5 characters long), cruise route (e.g. Miami to Nassau), and ticket price. The program should also track how many tickets have been sold for each cruise. Create an object-oriented solution with a menu that allows a user to select one of the following options: 1. Create Cruise – This option allows a user to create a new cruise by entering all necessary details (Cruise ID, route, ticket price). If the maximum number of cruises has already been created, display an error message. 2. Search Cruise – This option allows to search a cruise by the user provided cruise ID. 3. Remove Cruise – This op…

Chapter 13 Solutions

Absolute C++

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning