fibonnaci in variable sized array

Dear forum,
I'm trying to make a fibonacci numbers generator with variable size of Array. The code is as follow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


 int main()  
{
    int n = 1; //the range of Array

    cout<<"enter the number of Fibonacci digits: "<<endl;
    cin>>n;

    int fib[n];

    fib[0] = 1;
    fib[1] = 1;

    for(int i=2; i<=n; i++)
    {

    fib[i] = fib[i-1] + fib[i-2]; //fibonacci formula

    }

    for(int i=0; i<=n; i++)
    {
        cout<<fib[i]<<", "<<endl;
    }
    return 0;
}


And it works well, untill I set the n variable larger than 45. The 46th position in fibonacci sequence is negative. Is it only my compiler error or is it general. What can be the cause of it?

Cheers
closed account (SECMoG1T)
Well did your code compile even?

1
2
 cin>> n;
 int fib[n]; ///should be an error. 


mybe you could try use "long long int ";
I was able to compile it. Do you suggest that I can't use a variable in a array declaration?
The "long long int" gives the same error.
Do you suggest that I can't use a variable in a array declaration?

Yes. The compiler does, during compilation, create code for the function that reserves stack memory for local (aka automatic) variables. The array is such. The compiler does not know what the user will write.

You do need a dynamically allocated array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#include <iostream>

int main() {
  size_t n = 1; // unsigned
  std::cin >> n;
  std::vector<unsigned long long> fib( n ); // <- this is dynamic

  fib[0] = 1;
  fib[1] = 1;
  for ( int i=2; i < n; ++i ) { // Look at the end condition, there is no fib[n]
    fib[i] = fib[i-1] + fib[i-2];
  }
  return 0;
}


Fibonacci gets big quite quickly. The built-in types can store only so many digits. Look at the Boost library for "big int" types.
Well, previously i misplaced the long long type. I did it again and now it do well. Thanks Andy. So it seems like it work even without dynamic type?
it seems like it work

Your current compiler probably supports non-C++-standard feature: variable length arrays.

However, relying on non-standard features has down-sides.
(1) The code might not compile in any other environment.
(2) You don't learn to use the features that the standard language and library do provide.
gcc compiles variable length arrays, visual C++ does not.
Or could you not just create a function that does just about what you need?

1
2
3
4
5
6
7
8
9
int* ARRAY(int number)
{
	const int temp = number;
	return new int[temp];
}

// used like
int foo = 10;
int * bar = ARRAY(foo); // creates a 10 spot array 
@Spikerocks101:
What you wrote does not need a separate function:
1
2
3
int foo;
std::cin >> foo;
int * bar = new int[foo];

However, now you have two additional problems:
1. Did the allocation succeed?
2. How to deallocate properly?

If you insist making a pointer, make it managed:
1
2
3
int foo;
std::cin >> foo;
std::unique_ptr<int[]> bar = std::make_unique<int[]>( foo );


Yet, trust me, this has more benefits:
1
2
3
int foo;
std::cin >> foo;
std::vector<int> bar( foo );
Topic archived. No new replies allowed.