3 Replies - 1846 Views - Last Post: 23 July 2008 - 11:12 PM Rate Topic: -----

#1 mapes479  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-February 08

Fibonacci.h recursive method

Post icon  Posted 23 July 2008 - 07:39 PM

I'm having problems finishing up my Fibonacci.h program. The assignment is to do the following:

Write a header file which that you will call Fibonacci.h. The file contains a recursive method Fib with one parameter of type long and returns the Fibonacci value. Remember to use long as a data type for the returned fibonacci result by the method Fib because the number could be very large.

Here's the coding I have so far. I am new to C++ and can't tell if I'm headed in the right direction or what I'm doing wrong. Could someone please help me with this? I have my .cpp file completed already.

int fibonacci(int num, int num1, int num2)

long fib(unsigned long n)
{
    if (n <= 1) {
        return n;
    } else {
        return fib(n-1)+fib(n-2);
    }
}

int fibonacci(int num, int num1, int num2)
{ 
    int ctr=2,fibo=0;
    while(ctr<num)
        {
            fibo=num1+num2;
            num1=num2;
            num2=fibo;ctr++;
        }
    return fibo;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Fibonacci.h recursive method

#2 penguin2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 22-July 08

Re: Fibonacci.h recursive method

Posted 23 July 2008 - 07:52 PM

I don't believe that you are allowed to say "long fib(....". There is a long int, but no loose long.

This:
int fibonacci(int num, int num1, int num2)
needs a semicolon.
Was This Post Helpful? 0
  • +
  • -

#3 mapes479  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-February 08

Re: Fibonacci.h recursive method

Posted 23 July 2008 - 08:12 PM

View Postpenguin2, on 23 Jul, 2008 - 07:52 PM, said:

I don't believe that you are allowed to say "long fib(....". There is a long int, but no loose long.

This:
int fibonacci(int num, int num1, int num2)
needs a semicolon.



Here is my revised version: I'm getting the following error - how do I fix this?

error C2447: '{' : missing function header (old-style formal list?)

#include <iostream>

using namespace std;

int fibonacci(int num, int num1, int num2);


{
    if (n <= 1) {
        return n;
    } else {
        return fib(n-1)+fib(n-2);
    }
}

int fibonacci(int num, int num1, int num2)
{ 
    int ctr=2,fibo=0;
    while(ctr<num)
        {
            fibo=num1+num2;
            num1=num2;
            num2=fibo;ctr++;
        }
    return fibo;
}

Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Fibonacci.h recursive method

Posted 23 July 2008 - 11:12 PM

Quote

int fibonacci(int num, int num1, int num2);

Remove the semicolon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1