Complete the code to initialize the base case for Fibonacci DP array.
int fib[100]; fib[0] = [1];
The first Fibonacci number is 0, so fib[0] should be set to 0.
Complete the code to initialize the second base case for Fibonacci DP array.
fib[1] = [1];
The second Fibonacci number is 1, so fib[1] should be set to 1.
Fix the error in the loop condition to correctly compute Fibonacci numbers using DP.
for (int i = 2; i [1] n; i++) { fib[i] = fib[i-1] + fib[i-2]; }
The loop should run while i is less than n to fill fib array up to n-1 index.
Fill both blanks to complete the function header and return statement for Fibonacci DP.
int fibonacci([1] n) { int fib[100]; fib[0] = 0; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } return fib[[2]]; }
The function takes an integer n and returns fib[n-1] because array indexing starts at 0.
Fill all three blanks to complete the main function that reads input, calls Fibonacci DP, and prints result.
#include <iostream> using namespace std; int fibonacci(int n) { int fib[100]; fib[0] = 0; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } return fib[n-1]; } int main() { int [1]; cin >> [2]; int result = fibonacci([3]); cout << result << endl; return 0; }
The variable name for input and function argument should be consistent, here 'num'.
