Please help! For this assignment you will be writing a program to calculate the
ID: 3778498 • Letter: P
Question
Please help!
For this assignment 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
1. The value of n is accepted through the command line
2. 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
1. ./foo.out 2
foo(2) = 5
2. ./foo.out 10
foo(10) = 697
Explanation / Answer
#include <stdio.h>
int foo(int n){
//base cases
if(n==0)
return 2;
else if(n==1)
return 3;
else if(n==2)
return 5;
else
//calling recursively
return (foo(n-1)+foo(n-2)+foo(n-3));
}
int main(int argc, char *argv[])
{ int n=atoi(argv[1]); //converting string to integer
int foo_output=foo(n);
printf("foo(%d",n);
printf(") : %d",foo_output);
return 0;
}
/*****OUTPUT*******
sh-4.2$ main 2
foo(2) : 5
sh-4.2$ main 10
foo(10) : 697sh-4.2
*******OUTPUT*******/
/* i am doing main 2 as my output file is main */