Question

In: Computer Science

1.what is a base condition of recursive function? 2. why is the base condition of recursive...

1.what is a base condition of recursive function?

2. why is the base condition of recursive function important?

Solutions

Expert Solution

Here is an example first and answers later:

int factorial(int n){

if (n==1){

return 1;

}

return n*factorial(n-1);

}

The above function is a recursive function because the function is calling it self.

Because the function is calling itself there should be a condition where it needs to stop calling itself.

1) A base condition is a recurrence breaking condition of a recursive function

example: in the above factorial program the base condition is when n=1 we are stopping the recursive call and returning 1.

2) If a base condition was not set properly the function will not stop calling itself forever and stack memory will fill and the program will crash. So we should specify a base condition properly.


Related Solutions

Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
1.) Do you believe that 'affluenza" is a real condition? Why or why not? 2.) Why...
1.) Do you believe that 'affluenza" is a real condition? Why or why not? 2.) Why do you think people tend to distrust wealthy people or companies?
CHALLENGE ACTIVITY 9.4.1: Recursive function: Writing the base case. Add an if branch to complete double_pennies()'s...
CHALLENGE ACTIVITY 9.4.1: Recursive function: Writing the base case. Add an if branch to complete double_pennies()'s base case. Sample output with inputs: 1 10 Number of pennies after 10 days: 1024. the program language is python. please highlight the code for me to copy. thanks  
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the function f(n)? Consider the following recurrence: an= 2an-1 + 3 (where a1 = 1). Compute the values of an for n ≤ 5. Find a solution for the recurrence definition and validate its correctness. Consider the following recurrence: an=2an-1 +an-1an-2 (where a1 = 1). Compute the values of an for n ≤ 5.
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Air enters a food dehydration system at condition 1 and exits at condition 2. What is...
Air enters a food dehydration system at condition 1 and exits at condition 2. What is the change in enthalpy (kJ/kg dry air) of the air as it passes through the system: Condition 1: Tdb = 30C; Twb = 15C. Condition 2: Tdb = 100C; Twb = 40C. Select the closest answer.
For the following processes, compute autocovariance function. (a) AR(2) - use the recursive method; (b) ARMA(1,...
For the following processes, compute autocovariance function. (a) AR(2) - use the recursive method; (b) ARMA(1, 1) - you may use the linear representation from lectures; (c) ARMA(1, 2) - derive a linear representation first!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT