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
Code Should Be In C++
What does the function f do? struct Point2D { double x; double y; struct Triangle { Point2D v1; Point2D v2; Point2D v3; }; void f(Triangle&t) { } int temp = 12.5; temp = t.v1.x; t.v1.x = t.v1.y; t.v1.y = temp; } int main () { Triangle mytri; mytri.v1.x = 1.0; mytri.v1.y = 22.5; f (mytri); Swaps values of x and y in vertex 1 of an argument of type Triangle Initializes value of x in vertex 1 of an argument of type Triangle Sets all x,y values in all vertices of an argument of type Triangle Swaps value of x in vertex 1 with value of x in vertex 2, for an argument of type
None
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