1. a) Given a recursive algorithm as below: int F(int n) { if (n <= 1) return 1; else if (n % 2 == 0) return F(n-1) +n; else return F(n/2) - 1; } Illustrate how you will find out the value of F(10) using the above algorithm. You need to show all the steps. b) Given a function as below: int recFx(int a, int b) { If (a

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
1. a) Given a recursive algorithm as below:
int F(int n)
{
if (n <= 1) return 1;
else if (n % 2 == 0) return F(n-1) + n;
else return F(n/2) - 1;
}
Illustrate how you will find out the value of F(10) using the above algorithm. You need
to show all the steps.
b) Given a function as below:
int recFx(int a, int b)
{
If (a < b) return a +b;
else
return recFx(a - b, b + 1);
}
Determine the values of:
recFx(-10, 8)
recFx(148, 78)
(11)
Must show all the steps.
Transcribed Image Text:1. a) Given a recursive algorithm as below: int F(int n) { if (n <= 1) return 1; else if (n % 2 == 0) return F(n-1) + n; else return F(n/2) - 1; } Illustrate how you will find out the value of F(10) using the above algorithm. You need to show all the steps. b) Given a function as below: int recFx(int a, int b) { If (a < b) return a +b; else return recFx(a - b, b + 1); } Determine the values of: recFx(-10, 8) recFx(148, 78) (11) Must show all the steps.
Expert Solution
Part a

To find F(10)

10%2==0, so it is F(9)+10

This will be F(4)-1+10

This will be F(3)+4-1+10

This will be F(1)-1+4-1+10

This will be 1-1+4-1+10

Hence, F(10)=13

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning