Concept explainers
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)”...
- Here, the value of “n” is not less than or not equal to “1”. So, performs “else” statement.
Want to see the full answer?
Check out a sample textbook solutionChapter 14 Solutions
Problem Solving with C++ (10th Edition)
- C PROGRAM Reverse + Random Formula In the code editor, there's already an initial code that contains the function declaration of the function, computeReverseNumber(int n) and an empty main() function. For this problem, you are task to implement the computeReverseNumber function. This function will take in an integer n as its parameter. This will get the reverse of the passed integer and will then compute and return the value: result = (original_number + reverse_of_the_number) / 3 In the main() function, ask the user to input the value of n, call the function computeReverseNumber and pass the inputted value of n, and print the result in the main() with two (2) decimal places. SAMPLE: Input n: 20 Output: 7.33 Input n: 123 Output: 148.00arrow_forwardFind any errors in the following function definition: void fun (int, x) { return; } // funarrow_forwardint sum = 0; for (int i 0; i < 5; i++){ sum += i; } cout << sum;arrow_forward
- #include using namespace std; (а) class Fraction{ int x, y; public: Fraction (int a, int b) {x=a; y=b;}; int getx(){return x;} int gety() {return y;} } ; int main() Fraction n(3, 7); ++n; cout << "x/y: " <« n.getx()<< "/" « n.gety()<arrow_forwardQ1: Complete the following program by writing its functions: static void Main(....) { } double z; long P=1; int a, b, c=-4; int[] = {1, 3, 9, 10, 12, 7, 8}; f int gabs (c); Prime (f, out b); Find (g, out z); Power (a [1], a [2], ref p); // where P= a[1]a[²] double d-fact (g) ; Max (f); C# // return absolute value of c // b=the average of Prime numbers in f // z = 1! + 2! + ... + g! // return the factorial of x using // recursion principle // print the maximum value of f Console.Write("{0}: {1}{2}: {3}: {4}",g,b,z,p,d);arrow_forwardint f(int &k){k++;return k * 2;}int main(){int i = 1, j = -1;int a, b, c;a = f(i) + i/2;b = j + f(j) + f(j);c = 2 * f(j);return 0;} What are the values of a, b and c id the operands in the expressions are evaluated from left to right and then what are the values when its evaluated right to left?arrow_forwardProblem Statement: The mathematician Conway imagined a game, called game of life, which considered cells that are susceptible to reproduce, disappear, or survive when they obey certain rules. These cells are represented by elements on a grid of squares, where a grid has an arbitrary size. Thus, each cell (except those on the boundaries of the grid) is surrounded by eight squares that contain other cells. The rules are stated as follows: 1. Survival: Each cell that has two or three adjacent cells survives until the next generation.2. Death: Each cell that has at least four adjacent cells disappears (or dies) by overpopulation. Also, each cell that has at most one adjacent cell dies by isolation.3. Birth: Each empty square (i.e., dead cell) that is adjacent to exactly three cells gives birthto a new cell for the next generation.It is worth noting that all births and deaths occur at the same time during a generation. Write a program that simulates this game and displays all successive…arrow_forward#include<stdio.h> #include<stdarg.h> void fun1(int num, ...); void fun2(int num, ...); int main() { fun1(1, "Apple", "Boys", "Cats", "Dogs"); fun2(2, 12, 13, 14); return 0; } void fun1(int num, ...) { char *str; va_list ptr; va_start(ptr, num); str = va_arg(ptr, char *); printf("%s ", str); } void fun2(int num, ...) { va_list ptr; va_start(ptr, num); num = va_arg(ptr, int); printf("%d", num); }.arrow_forwardint fun(int k){ return ( ); void main(){ int n; cin >> n; n = n * fun(n); <-- 1 Fill in the appropriate statement and expression in fun, so that when it is called in main, and after the execution of the statement marked 1, the value of n would always be n3.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education