I need to do a hwk using recursion concept. You should use only C. Description:
ID: 3849510 • Letter: I
Question
I need to do a hwk using recursion concept.
You should use only C.
Description:
You will find that recursion is used a lot for mathematical problems. While it’s not the only use for it, it’s the easiest way to learn recursion. Now, most of these problems are google-able. I assure you that the lab this week you cannot find on google, so if you do cheat and look up the prelab, you aren’t helping yourself learn this.
Part 1:
A factorial, denoted X!, is all numbers from 1-X inclusive multiplied together. For example:
5! = 5*4*3*2*1 = 120
Write a recursive function that calculates the factorial of a number. You should use the following prototype:
int factorial(int num);
Where it takes a number you want the factorial for, and returns the final answer.
Part 2:
Calculating the cost of a task that takes many identical steps can be expressed as so:
f(x) = f(x-1) + g(x),
Where f(x) is the cost of doing a task with x steps, and g(x) is the cost of doing that particular step.
Say you have such a task with the following cost function:
f(x) = f(x-1) + 4x, where f(0) = 2 and f(1) = 5
Write a recursive function that calculates the total cost of such a task. Use the following prototype:
int costFunction(int num);
Where it takes a number of steps in the task and returns the final cost.