1,197 views

2 Answers

Best answer
6 votes
6 votes
f(n) = 1, if n=0
f(n) = 2^(n-1), n>=


foo(0) = 1
foo(1) = 1
foo(2) = 2
foo(3) = 4
foo(4) = 8
foo(5) = 16
foo(6) = 32
foo(7) = 64
foo(8) = 128
foo(9) = 256
foo(10) = 512
selected by
0 votes
0 votes

For foo(x), we need foo(x-i). i is positive, hence for any x we'd need to find the values of foo(<less than x>) to obtain foo(x).

It's best to go with a bottom-up approach, and start with foo(0).

  • foo(0) = 1
  • foo(1) = foo(0) = 1
  • foo(2) = Add all previous values = 2.
  • foo(3) = Add all previous values = 4
  • foo(4) = Add all previous values = 8
  • foo(5) = Add all previous values = 16
  • foo(6) = Add all previous values = 32
  • foo(7) = Add all previous values = 64
  • foo(8) = Add all previous values = 128
  • foo(9) = Add all previous values = 256
  • foo(10) = Add all previous values = 512
Answer:

Related questions

3 votes
3 votes
4 answers
1
Arjun asked Oct 18, 2016
775 views
What will be the output of the following code?#include <stdio.h int main() { char a = 'A', z = 'Z'; printf("%d", z-a); }
2 votes
2 votes
5 answers
2
Arjun asked Oct 18, 2016
1,575 views
The value returned by the following code is _____int foo() { int a[] = { 10, 20, 30, 40, 50, 60 }; int *p = &a , *q = &a[5] ; return q-p; }
12 votes
12 votes
1 answer
3
3 votes
3 votes
1 answer
4
Arjun asked Oct 18, 2016
908 views
What is the number of tokens in the below C code?int foo(int i, int j){ return printf(" I do it correctly", i j); }