COMP1410-Lab 2 Algorithm, Recursive Function Solved
Objectives:
- Practice designing/implementing algorithms using recursion
- Practice use of recursive functions
Pre-requisite(s):
In this , you must code and document the following functions using RECURSION only.
As with the last , test the functions by calling them from a simple interactive main() function using a menu, with different values. Overall, you should have one C program containing one main() function and 5 other functions listed in the table below, where the functions are called based on the user input to the menu choices. The program should contain a loop that permits users to enter a new choice of function for each loop, until exit from the loop is explicitly chosen.
1 Summation: ∑𝑛𝑘=1𝑘=1+2+3+⋯+𝑛 ;
n 1; reject with error message otherwise
[Note that this sum is equal to n(n+1)/2. DO NOT program the function – program the series.] 2 Factorial(0) = 1;
Factorial(n) = n * (n-1) * . . . * 2 * 1
Requirement: n = 0; reject with error message otherwise 3 Fibonacci(0) = 0;
Fibonacci(1) = 1;
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);
Requirement: n = 0; reject with error message otherwise 4 gcd (x, y) = x, if y=0
gcd (x, y) = gcd (y, x MOD y), if y 0
Requirement: x and y both 0; reject with error message otherwise 5 Power(a,b) = 𝑎𝑏
Requirement: a 0, b 0, b is an integer; reject with error message otherwise
How to document functions?
/* Objective: Describe the function/its purpose briefly
Input: Describe the input parameters, or the assumptions/requirements for the function.
Output: Describe the output of the function. (What does it return? What does it print, if anything?) */