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

Recursion. Question 1 1.1 What are the 2 main components of a recursive function? 1.2 What...
Recursion. Question 1 1.1 What are the 2 main components of a recursive function? 1.2 What is more efficient: an explicit loop structure or a recursive function? Explain your answer. 1.3 In what scenarios should a recursive solution be preferred to an iterative solution?
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.
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive binary search(lst, target) which returns True if target is in lst; otherwise False. You must use recursion to solve this problem (i.e. do not use any loops). Part B - Where is target? Write a function advanced recursive binary search(lst, target, lo, hi) which returns the index of an instance of target in lst between lo and hi, or None if target is not...
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?
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show...
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show Base case, and recursive case.
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT