views:

153

answers:

5

My professor has asked us to write a program that uses recursion to solve a fibonacci sequence. This is all pretty normal, but he's asked us to make our function return void. I've been working at this for a few days now and can't find a way to do this.

I have:

void fibonacci(double *n,double *x,double *y,double *result) {
     if(*n == 1)
          *result = 0;
     else if(*n == 2)
          *result = 1;
     else
          fibonacci(--n,n,(n-1),(n+(n-1))); }

Is what I'm doing right? I have never had to use parameters in such ways before and I'm not sure if I'm on the right track. For some reason it's not compiling at the recursive call to fibonacci, stating invalid pointer addition. Thanks!

+5  A: 

Hint: problem is there: fibonacci(--n,n,(n-1),(n+(n-1))); or even just there --n. You're working with pointers

Wernight
+1 for just the hint and not to *spoil* that student with the entire code.. :)
liaK
+4  A: 

The compiler is right. You need to dereference the pointers in the call, if you use pointers.

But the simpler solution would be to use this prototype instead (and match all code to it) :

void fibonacci(int n, int *result).
  • I've replaced double by int, because I don't see why you'd use double to store integers.
  • I've removed x and y which you don't use in your function.
jv42
He said our test cases would have really large numbers, he wants us to use double because of that. I think making n an integer and result a double*, it's the result that will get ridiculously big. Thanks for your help!
blakejc70
Double is not suited for storing very large integers... You can use long long or something like that.
jv42
Oh and I think your program's stack will explode before hitting the maximum value of integers (unsigned or signed).
jv42
@jv42: that's not true. For example, F(100) will not threaten the stack on any system bigger than your thumbnail, but the result doesn't fit in a 64bit integer. `double` will at least approximate the result. In fact, though, if all you need is an approximate `double` result then there's not much point calculating it recursively.
Steve Jessop
OK, I was thinking about the parameters, not the result, my bad.
jv42
+1  A: 

no it is not. 1st of all you are subtracting pointers to float (at --n) which might easily (even if you compile it and run it) produce access violation. It correctly complains though about types. The types that the function accepts are pointers and I bet you are passing floats.

frag
+1  A: 

Use this for a start:

void fibonacci(double n, double & result) {
    if(n == 1)
        result = 0;
    else if(n == 2)
        result = 1;
    else {
        // gotta figure that part out yourself
    }
}

By declaring result as a reference, your modification will change the value of the actual parameter passed. Since this is C++ references should be preferred. You can still declare n as a normal value, because you do not want to modify it. The recursive call is your homework now :)

Space_C0wb0y
It's more complicated solution with result equals 0/1. Won't tell more since I can't provide full code to homework question, but try yourself :)
raceCh-
thanks for help, yes I'd prefer to figure it out. I was asking because my parameters were starting to get crazy and I think I'm over complicating this. Thanks guys!
blakejc70
+1  A: 

Since this is a homework, I won't provide working code, although a few points here:

  1. Using a reference is simpler than using pointers
  2. You really need to increase the result, not set it to 0 or 1. Therefore you need to pass to first function call by reference an int with assigned value of 0.
  3. Consider the formula: f(n) = f(n-1) + f(n-2) for all n > 2; f(n) = 0 for n=1 and f(n) = 1 for n=2.
raceCh-
You are SOOO right, took me a long time to see it but the incremental way you're talking about is the best way to do this, thanks a lot for your help and insight. Now i've got to calculate it's running time and growth rate, fun fun!
blakejc70
Glad to help, thanks for accepting :)
raceCh-