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,...
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.
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...
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?
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 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!
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
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.
Answer to the following questions. 1.What is Generalization and Specialization 2. What is a Recursive Relationship...
Answer to the following questions. 1.What is Generalization and Specialization 2. What is a Recursive Relationship 3. What is the purpose of a query and What is a stored procedure 4. What is an index  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT