Code 911

This is a discussion on Code 911 within the C++ Programming forums, part of the General Programming Boards category; Hello friends here at c programming dot com. I am writing because I'm completely baffled by a question that my ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    Code 911

    Hello friends here at c programming dot com. I am writing because I'm completely baffled by a question that my instructor has posed to us. He asked us to use a recursive function (outside of the main) to figure the values of fibonacci numbers.If you haven't ever heard of them they go something like this 1,1,2,3,5,8,9,17. . . . As you can see the value of each value is the sum of the last two values. Now I know the concept of recursive functions but I would have a clue on where to even begin with this problem. Any ideas

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,260
    You could do a board search, or just look at this thread

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    close

    okay this is the stuff that I came up with as far as code is concerned

    Code:
    #include <iostream>
    
    int fibonacci(int a, int b, int c);
    
    using namespace std; 
    int main()
    {
    	int a, b, c;
    	char j='y';
    	while(j!='n')
    	{
    	
    
    	cout<<"Enter the first number in the fibonacci sequence: "<<"\n";
    	 cin>>a;
    
    	cout<<"Enter the second number in the fibonacci sequence: "<<"\n";
    	 cin>>b;
    	
    	cout<<"Enter what degree you'd like to go to: "<<"\n";
    	 cin>>c;
        
    	cout<<"The fibonacci value calculated is: "<<c<<fibonacci(a,b,c)<<"\n";
    	cout<<"Would you like to continue Y/N"<<endl;
    	 cin>>j;
    	}
    	return 0;
    }
    
    int fibonacci(int a, int b, int c)
    {
    	if(c==1)
    		return a;
    	else if (c==2)
    		return b;
    	else 
    		return fibonacci(a,b,c - 1) + fibonacci(a, b, c - 2);
    }
    I'm only having one issue it keeps giving me bogus output

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its printing out bogus numbers because you print the value c before printing fibonacci(a,b,c).

    Change
    Code:
    	cout<<"The fibonacci value calculated is: "<<c<<fibonacci(a,b,c)<<"\n";
    to
    Code:
    	cout<<"The fibonacci value calculated is: "<<fibonacci(a,b,c)<<"\n";

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Whoa deja vu. There is another thread about this going on as we speak!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21