Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 14, Problem 1P

Explanation of Solution

Recursive function for nth Fibonacci numbers:

The recursive function for nth Fibonacci number is shown below:

/* Function definition for compute nth Fibonacci number */

int recursiveFibFunction(int n)

{

       /* If "n" is less than or equal to "1", then */

       if (n <= 1)

              //Return value of "n"

              return n;

  /* Otherwise Recursively call the function "recursiveFibFunction" */

  return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);

}

Explanation:

The above function is used to compute the nth Fibonacci numbers.

  • In this function, first check the value of “n”. If the value of “n” is less than or equal to “1” that is value of “n” is either “1” or “0”, then returns the given “n” value.
  • Otherwise, recursively call the function “recursiveFibFunction”.

Complete executable code:

The complete code is implemented for Fibonacci number is shown below:

//Header file

#include<iostream>

//For standard input and output

using namespace std;

//Function declaration for "recursiveFibFunction" function

int recursiveFibFunction(int n);

//Main function

int main ()

{

       //Initializes the number to "8"

       int number = 8;

  /* Dispay fibonacci number by calling the function "recursiveFibFunction" */

  cout << number << "th Fibonacci number is: "<< recursiveFibFunction(number) << endl;

       return 0;

}

/* Function definition for compute nth Fibonacci number */

int recursiveFibFunction(int n)

{

       /* If "n" is less than or equal to "1", then */

       if (n <= 1)

              //Return value of "n"

              return n;

  /* Otherwise Recursively call the function "recursiveFibFunction" */

  return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);

}

Expert Solution & Answer
Check Mark
Sample Output

8th Fibonacci number is: 21

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
Just wanted to know, if you had a scene graph, how do you get multiple components from a specific scene node within a scene graph? Like if I wanted to get a component from wheel from the scene graph, does that require traversing still?   Like if a physics component requires a transform component and these two component are part of the same scene node. How does the physics component knows how to get the scene object's transform it is attached to, this being in a scene graph?
How to develop a C program that receives the message sent by the provided program and displays the name and email included in the message on the screen?Here is the code of the program that sends the message for reference: typedef struct {    long tipo;    struct {        char nome[50];        char email[40];    } dados;} MsgStruct; int main() {    int msg_id, status;    msg_id = msgget(1000, 0600 | IPC_CREAT);    exit_on_error(msg_id, "Creation/Connection");    MsgStruct msg;    msg.tipo = 5;    strcpy(msg.dados.nome, "Pedro Silva");    strcpy(msg.dados.email, "pedro@sapo.pt");    status = msgsnd(msg_id, &msg, sizeof(msg.dados), 0);    exit_on_error(status, "Send");    printf("Message sent!\n");}
9. Let L₁=L(ab*aa), L₂=L(a*bba*). Find a regular expression for (L₁ UL2)*L2. 10. Show that the language is not regular. L= {a":n≥1} 11. Show a derivation tree for the string aabbbb with the grammar S→ABλ, A→aB, B→Sb. Give a verbal description of the language generated by this grammar.
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