You've encountered the reason for the existence of what we know as Dynamic Array or STL-Vectors.
The reason why compiler throws that error is due to that fact that in a standard array implementation the array size must be exactly specified during declaration.
float a[100] <- correct
float a[] <- incorrect
It is correct because the compiler exactly knows how much memory of a particular data type must be assigned to this array collection.
Now let's move onto the Dynamic Array - Raw pointers and STL Vector which gives you the privilege to decide the Array Size in runtime.
Method 1: Raw pointers
#include <cstddef> // std::size_t
int evenFibonacciSum(int max)
{
int *fib=NULL;
fib = new int[max+2];
fib[0] = 0;
fib[1] = 1;
for (std::size_t i = 2; i <= max; i++)
fib[i] = fib[i - 1] + fib[i - 2];
int result = fib[max];
delete [] fib;
return result;
}
Method 2: STL Vectors
#include <cstddef>
#include <vector>
int evenFibonacciSum(int max)
{
std::vector<int> fib(max+2);
fib[0] = 0;
fib[1] = 1;
for (std::size_t i = 2; i <= max; i++)
fib[i] = fib[i - 1] + fib[i - 2];
return fib[max];
}
I would suggest you use Method 2: STL Vectors since it automatically takes care of the memory management issues that arises with such dynamic allocation.
Have a great day!
Arul Verman