4
#include<iostream>
using namespace std;

long long int memo[20] = {-1};       //create memo table  and initialise to -1

long long int fibo(long long int n)
{
    if(memo[n]>-1)           //changing this to if(memo[n]>0) works fine
        return memo[n];      //but as such this gives 0 from all my inputs
    if(n<=2)
        return 1;
    memo[n] =  fibo(n-1) + fibo(n-2);    //recurse
    return memo[n];
}

int main()
{
    long long int n;
    cin>>n;
    cout<<fibo(n)<<endl;       //calls the fibo function 
    for(int i=0;i<20;i++)       //this prints my memo table used...
        cout<<memo[i]<<" ";
}

I am calculating the nth Fibonacci number using top-down dp but my memo table is zeroed out. Even at locations which I am not touching, why?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Pavithran Ravichandiran
  • 1,711
  • 1
  • 17
  • 20

5 Answers5

6

When you have

long long int memo[20] = {-1}; 

You are not telling the compiler to initialize the array with all -1. What you are telling it is here is a initializer list and initialize the elements in the array with its contents. Since the list is smaller than the array every missing initializer causes the compiler to zero initialize the corresponding elements.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
4

Because that's how array initialization works in C++. You set the first element of the array memo to -1, and the compiler will value-initialize (before the C++11 standard) or default-initialize (since C++11 and onward) all of of the other elements.

Please read more about aggregate initialization here.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

long long int memo[20] = {-1}; actually sets -1 to the first element only. Thereafter they take the value 0.

In your case, given that the Fibonacci series doesn't contain a zero term, I'd use 0, rather than -1 as the indicator, and write

long long memo[20] = {}; /*new C++ standards allow this, note that C doesn't*/

instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You are just setting the first element with -1 by doing {-1}.

Here is the excerpt from C++ Programming Language Stroustrup book about Array Initializer:


If the initializer supplies too few elements for an array, 0 is used for the rest. For example:

int v5[8] = { 1, 2, 3, 4 };

is equivalent to

int v5[] = { 1, 2, 3, 4 , 0, 0, 0, 0 };

So your case long long int memo[20] = {-1}; comes under supplying too few elements for the array.

Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
1

Because the line

long long int memo[20] = {-1}; 

states that only the first element of memo is initialized as zero.

When you type "memo" only, it refers to a pointer which holds the address of the first element of the array in C++. only when specified by square parentheses -for example memo[3]-, or when explicitly said something like "memo+3" it will point to the indices of the array.

What you would like yo do is to state all indices and initialize them with -1 value.

long long int memo[20] = {[0 ... 20] = -1};
  • *states that only the first element of memo is initialized as zero.* os not correct. `memo` is not a pointer. An arrat name can decay to a pointer but a pointer and a array are two different things. Lastly `long long int memo[20] = {[0 ... 20] = -1};` is not valid C++. – NathanOliver Dec 02 '16 at 14:01