recursive stuff dudes

This is a discussion on recursive stuff dudes within the C++ Programming forums, part of the General Programming Boards category; check out the following piece of code friends Code: #include<iostream> #include<string> int fibonacci(int a); using namespace std; template <class T, ...

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

    recursive stuff dudes

    check out the following piece of code friends
    Code:
    #include<iostream>
    #include<string>
    int fibonacci(int a);
    using namespace std;
    
    
    template <class T, int n>
    class stack
    {
    private: T elt[n]; int counter;
    public:
    void clearstack() { counter = -1;                 }   //INITIALIZE COUNTER
    bool emptystack() { return counter==-1?true:false;} //EVALUATION IF STACK IS EMPTY
    bool fullstack()  { return counter==n-1?true:false; } //EVALUATION IF STACK IS FULL
    void push(T x)    { counter++; elt[counter] = x; } //FUNCTION TO ADD TO STACK
    T pop()           { T x; x= elt[counter];counter--;return x; } //FUNCTION TO POP THE STACK
    };
    
    void main()
    {
    	
    	int d,v;
    	
    	
    	cout<<"ENTER THE NUMBER FOR THE FIBONACCI SERIES: "<<"\n";
    	cin>>v;
    	
    d= fibonacci(v);
    cout<<d;
    }
     
    int fibonacci(int v)
    {
    	stack <int, 15> s;
    	int counter=0;
    	int y=0;
    	int z=0;
    	int res=0;
    	s.clearstack();
    		s.push(1);
    		s.push(1);
    
    if(v==0) return 1;
    else if(v==counter)
    	{
    	y=s.pop();
    	z=s.pop();
    	res=y+z;
    	return res;
    	}
    else if(v>counter)
    	{	
    	
    		y=s.pop();
    		z=s.pop();
    		res=y + z;
    		++counter;
    		return res + fibonacci(res - y);
    		.
    
    	}
    return 0;
    }
    I get a freaky error message that says unhandeled exception every time I attempt to run it. Do any of you see a problem here. Any ideas here

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    counter is a local variable in your fib function. So everytime you enter that function it is always zero. So the check 'v>counter' is usually going to be evaluated to true. That is why it is crushing your stack.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    res is always going to equal 2, and res-y is always going to equal 1. Basically you are calling fibonacci(1) until your stack dies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive or Threads
    By vasanth in forum C++ Programming
    Replies: 5
    Last Post: 01-18-2003, 09:30 PM
  2. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 07:27 PM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  5. Recursive Function
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 03:41 PM

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