This is a c program practice. must solve this problem recursively may only make
ID: 3847510 • Letter: T
Question
This is a c program practice.
must solve this problem recursively
may only make ONE recursive call in the recursive case
For this practice you will be writing a program to calculate the value of the function foo. The value of foo is defined as follows
foo(0)=2
foo(1)=3
foo(2)=5
foo(n)= foo(n1)+ foo(n2)+ foo(n3)
Specifications
The value of n is accepted through the command line
You may only make 1 recursive call in the recursive case
1. If you don't you will fail many of the tests with a timeout issue 3. You must solve the problem recursively
Example
./foo.out 2 foo(2) = 5
./foo.out 10 foo(10) = 697
Explanation / Answer
//Use the below code to calculate the foo value.
//If you are satisfied with the answer please comment and like.
#include <stdio.h>
int foo(int n) {
if(n == 0) {
return 2;
}
else if(n == 1) {
return 3;
}
else if(n == 2) {
return 5;
}
else return foo(n-1)+foo(n-2)+foo(n-3);
}
int main() {
int n;
printf("Please enter foo value ");
scanf("%d",&n);
printf(" foo(%d)=%d",n, foo(n));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.