Please Help me with the following C++ Problems. 1. For the following recursive f
ID: 644920 • Letter: P
Question
Please Help me with the following C++ Problems.
1. For the following recursive function, find f (5):
int f(int n)
{
if (n==0)
return 0;
else
return n+ f(n-1);
}
2. For the function in question 1, find f(0).
3. For the function in question 1, suppose + is changed to * in the inductive case. Find f(5).
For the next exercise assume ASCII representation of characters and the following function f():
void f(char ch)
{
if (('A' <= ch) && (ch <= 'H'))
{
f(ch-1);
cout<
}
else
cout<
}
Tell what output will be produced by the function call
4. f('C')
5. f('G')
6. f('3')
7. f('C') if ch-1 is replaced by ch+1 in the function
8. f('C') if the output statement and the recursive call to f() are interchanged
9. f('C') if a copy of the output statement is inserted before the recursive call of f()
Explanation / Answer
1)if n=5 answer =15
5+4+3+2+1==15
your program is sum of numbers fron 1 to n
but fibonacci program was also sinilar to this function except
return n * f(n-1)
2)if n=0 then first if statement executes and returns 0
3) I already mentioned about replacing * to +
then it becomes factorial program...so n=5 then 5*4*3*2*1==120
4) if we pass C then it prints A
5) if we pass G then prints A
6) f('3')...3 has ascii value 49 and A has 65 so it prints else statement
7)then prints H
8)if we pass C then it prints C to A...that is C B A