Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 14.2, Problem 13STE

Explanation of Solution

Given code:

//Header file

#include <iostream>

//For standard input and output

using namespace std;

//Function declaration for "rose" function

int rose(int n);

//Precondition: n >= 0.

//Main function

int main()

{

  /* Call function "rose()" with argument "4" and display it */

      cout << rose(4);

      return 0;

}

/* Function definition for rose() function with parameter "n" */

int rose(int n)

{

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

      if (n <= 0)

            //Returns "1"

            return 1;

      //Otherwise

      else

     /* Recursively call the function "rose()" with decrement the value of "n" by "1" and multiply by "n" */

            return (rose(n - 1) * n);

}

Explanation:

The given code is used to display the value after calling the function “rose()”.

  • First, declare the function for “rose()”.
  • Define main function
    • Call “rose()” function with argument “4” and display the given value.
  • Define “rose()” function with parameter “n”.
    • If “n” is less than or equal to “1”, then returns “1”.
    • Otherwise, recursively call the function “rose()” with decrement the value of “n” by “1” and then multiply with “n”.

Reasons for displaying given output:

  • In main function, call “rose()” function with argument “4”.
  • So, in the function “rose()”, first check the value of “n”.
    • Here, the value of “n” is not less than or not equal to “1”. So, performs “else” statement.
      • In “else” statement, return “(rose(4 - 1) * 4)” implies “(rose(3) * 4)”...

Blurred answer
Students have asked these similar questions
Consider the following recursive function:   void recurse(int num) {   if (num == 0)     cout << “ Zero” << endl;   else {     cout << " " << num << " " << endl;     recurse(num + 1)   } }   Is recurse (0) a valid call? If a valid call, what does the function output? If not a valid call, explain why it is not a valid call.
Can you help me with this program?In C++Write a program that finds all the ways in which the mouse can move to the cheese, and if any, display it with ‘*’.An example diagram of the task is shown in the figure:   Operating instructions:  Compose a recursive function path (int i, int j), receiving as parameters the coordinates of the current position of the mouse (initially 0.0, and then run with parameters (i-1, j), (i + 1, j), (i, j-1) or (i, j + 1)).
Using Haskell.)   One common misconception is that prime numbers are those that are not divisible by 2, 3, 5, 7 and 9. To dispel this myth, write a function fakePrimes that returns all numbers that are not divisible by 2, 3, 5, 7 and 9, and are not prime. For example, taking the first number out of this function we get [121].
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
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