Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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
int func(int a, int b) { return (a
float theRealQuestion (int x, float y) { int z;  if (x > 15)   z = x;  else   z = y;  return z;  }  Code the function prototype for the given function.
Consider the following function: void fun_with_recursion(int x) { printf("%i\n", x); fun_with_recursion(x + 1); } What will happen when this function is called by passing it the value 0?
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education