Dynamic Memory

Jul 27, 2010 at 6:49pm
So this is what code I have so far and I am confused as what I need to do for the rest of it. I need a program that generates the fibonacci sequence to the number of calculations the user enters...that works. What I need left is to dynamically allocate memory and build an array around it...
Any help would be greatly appreciated

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
29
#include <iostream>
#include <new>
using namespace std;

long fib(unsigned long n) {
     if (n <=1) {
           return n;
           } else {
                  return fib(n-1)+fib(n-2);
                  }
                  }
                  
                  int main()
                  {
                      int n;
                      cout << "Fibonacci Sequence \n";
                      cout << "How many elements do you want to run?" << endl;
                      cin >> n;
                      int fibonacci[n];
                      for (int i = 0; i < n; i++) {
                          fibonacci[i] = fib(i);
                          }
                          for (int i = 0; i < n; i++) {
                              cout << fibonacci[i] << " ";
                              }
                              cin.get();
                              system("PAUSE");
                              return 0;
                              }
Jul 27, 2010 at 7:05pm
Okay...I have this so far. Is it right?
Thanks!
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
29
30
31
#include <iostream>
#include <new>
using namespace std;

long fib(unsigned long n) {
     if (n <=1) {
           return n;
           } else {
                  return fib(n-1)+fib(n-2);
                  }
                  }
                  
                  int main()
                  {
                      long int * a = NULL;
                      int n;
                      cout << "Fibonacci Sequence \n";
                      cout << "How many elements do you want to run?" << endl;
                      cin >> n;
                      a = new long int[n];
                      int fibonacci[n];
                      for (int i = 0; i < n; i++) {
                          fibonacci[i] = fib(i);
                          }
                          for (int i = 0; i < n; i++) {
                              cout << fibonacci[i] << " ";
                              }
                              cin.get();
                              system("PAUSE");
                              return 0;
                              }
Jul 27, 2010 at 7:30pm
Jeebus, that's the most revolting indentation I've ever seen. Also, don't ask "is it right?". Run it! Now, is it right?
Jul 27, 2010 at 7:32pm
It runs perfectly. However, is there anything in the code that needs to be altered or added to fit with what needs to be done?
Jul 27, 2010 at 7:35pm
There's actually a really simple equation to determine a number in the Fibonacci sequence.

(phi^n-(1-phi)^n)/sqrt(5)

where phi=(1+sqrt(5))/2

1
2
3
4
5
6
#include <cmath>
#define phi 1.6180339887

long fibonacci(unsigned pos){
	return (pow(phi,pos)-pow(1-phi,pos))/sqrt(5);}


So, you'd just use that formula to iterate over the elements you want to show and display them.

To make an array with a size that's determined on runtime, you use the "new" keyword:

1
2
3
4
int array_size=256;
long* myarray=new long[array_size];
my_array[0]=567;
Last edited on Jul 27, 2010 at 7:39pm
Jul 27, 2010 at 7:39pm
Well, the way you're doing is a little odd to me, but from a quick glance, seems to work. If you want to get real fancy, you'd notice that calculating fib(n-1) means calculating fib(n-2). There is a clever way to calculate them faster by storing the results that you'll use again and again.

Edit: Or you can use that guy's solution.
Last edited on Jul 27, 2010 at 7:40pm
Topic archived. No new replies allowed.