Concept explainers
Explanation of Solution
The given program states that define a function name “MysteryPrint()” that takes “N” as an argument and then checks the “if” or “else” condition. This program has recursive function which calls the function directly by itself and then repeat the operation until the function get terminated or failed.
To ensure check the given program in the Python language,
# Function definition
def MysteryPrint(N):
# Check Condition N is greater than 0
if(N > 0):
# Condition is true, display N
print(N)
# Call MysteryPrint with argument N-2
MysteryPrint(N - 2)
# Otherwise
else:
# Display N
print(N)
# Check condition N is greater than -1
if(N > -1):
# Call MysteryPrint with argument N+1
MysteryPrint(N + 1)
# Call MysteryPrint with argument 2
MysteryPrint(2)
Explanation:
- Initially, the “MysteryPrint()” function takes value “2” as an argument, since it is given that call the function with the value “2”.
- In the next step, it checks the “if” condition which says if the value of “N” is greater than “0”, then execute the block of statement under the “if” condition and since, the value “2” is greater than “0”, so it executes the instruction under “if” condition and print the value “2”...
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- Write a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, the pattern generated is as follows: **** *** ** * * ** *** **** Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the preceding pattern.arrow_forwardWrite a program in python with a function repeatPhrase(phrase, n), which prints the given phrase n times, alternating between lowercase and uppercase. For example, repeat('The sky is yellow', 5) would print: the sky is yellow THE SKY IS YELLOW the sky is yellow THE SKY IS YELLOW the sky is yellow Use the function in a program where you ask the user for the phrase and the value of narrow_forward1.The following is the C code that you need to implement for this lab: uint8_ t f(uint8_tn) return(n<2)?(n):(f(n-1)+f(n-2)); The main function can be assumed as follows: int main() uint8_ t x; x=f(???); return 0; Obviously, " ? ? ? " is representing a value used to test the algorithm! Is "f"recursive? a.No answer text provided. b.Yes c.no d.depends on whether it is in for main 2.First, make the code in the previous question an actual C program so that it can be compiled and it runs. Play with it so that you feel comfortable with the logic of the code. Then implement the code in TTPASM. Note that you need to preserve the actual C code structure, this means you cannot it into a non-recursive subroutine. Furthermoreall conventions discussed in class regarding subroutines must be followed. The idea is that I should be able to substitute f with my own code, and main should work. Or, I can substitute main with my own, and f should work. Attach the source code of your assembly…arrow_forward
- what could go wrong with the function show on the right? (tip: syntax error is not the answer) void goo(unsigned int n) { if (n==0) return; cout << n; goo(n-2); }arrow_forwardConsider the following recursive function:void recEx11(int x){if (x > 0)if (x % 2 == 0){recEx11((x - 3) / 2);cout << x << " ";}else{recEx11((x - 4) / 2);cout << x << " ";}}What is the output of the following statements? a. recEx11(28); b. recEx11(148); c. recEx11(98); d. recEx11(-30)arrow_forwardWrite a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, then the pattern generated is:********************Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the above pattern.arrow_forward
- Explain the functionality of below recursive functions. static void fun1(int n) { int i = 0; if (n > 1) fun1(n - 1); for (i = 0; i<n; i++) System.out.print(" *"); } 2. int LIMIT = 1000; void fun2(int n) { if (n<=0)return; if (n>LIMIT)return; System.out.print( String.format("%d", n)); fun2(2*n); System.out.print( String.format("%d", n)) }arrow_forwardWrite a recursive function in Java that accepts an integer as input and returns 1 + 1/2 + 3 + 1/4 + ...(n or 1/n). The answer is the sum of the odd integers from 1 to n plus the sum of the reciprocals of the even integers.arrow_forwardWrite a python program that contains a function called reveal_recursive() that takes a word (string) and the length of the word (int) as input and has the following functionality:1. prints the word where all characters are replaced by underscores2. continue to print the word revealing one character at a time. i.e., the second line printed should print the first character followed by “_”’s representing the rest of the word. (see example below)3. the function should end after printing the entire word once.4. This function should be recursive Example: #the word is kangaroo________k_______ka______kan_____kang____kanga___kangar__kangaro_kangarooarrow_forward
- Write a Java function that counts the number of 'x's in the string. Any 'x' that is followed by another 'x' should count double (e.g. "axxbxc" -> 4) /** * Count the number of 'x's in the string. Any 'x' that is followed by * another 'x' should count double (e.g. "axxbxc" -> 4) * * @param x a string. * @return the count of x's. */ public static int countXWithDubs(String s){ return -1; }arrow_forwardConsider the following declarations. In each case, write the English name of the type of x.Example: double *x;Answer: pointer-to-double (a) int *x[];(b) int (*x)[];(c) double **x;(d) unsigned long int (*x[])[n];arrow_forwardWrite a C++ program where the user inputs two positive integers, A and B, where 5≤ A ≤10 and 20≤ B ≤30. (use user defined functions) A function to output AB (where A is multiplied by itself B times) do not use the built in “pow” function, use your own function. A function that returns the area of a rectangle of height A and width B. A function that returns the sum of odd numbers between A and B. A function to calculate AB recursively. Knowing that AB = AB-1 * A with base case A0 =1arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning