Thread: too many variables defined

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    too many variables defined

    This is what I have:
    Code:
     
    #include <iostream.h>
    
    //f(1)=2, f(2)=2, f(n)=2*f(n-1) + f(n-2)
    
     int f(int n) 
     {
    
     	if (n <= 2) 
    	{
     		return 2;
    	}
    
    	return (2 * f(n - 1) + f(n - 2));
    
     }
     
     
     // This part was to test my code.
     int main() 
    { 
       for ( int i = 0; i < 4; ++i ) 
       { 
          std::cout << "f(" << i << ") = " << f(i) << std::endl; 
       } 
       return 0; 
    }
    I am supposed to just compute for f. This defines too many values, it should just define for n >=1, so do I change my 2 to a 1? Not sure here. Thanks!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I don't see a problem

    Compiles OK and produces
    f(0) = 2
    f(1) = 2
    f(2) = 2
    f(3) = 6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    Yes, I know....but I am not supposed to define that many variables, just n>=1. Any ideas?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are no variables "defined" inside the function f. The only variable "defined" is i in your test loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps campermama, you should describe the problem, rather than try to explain a solution.

    I get the feeling that f(x) is supposed to produce a different sequence than 2,2,2,6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    I think the problem was I was showing a result for f(0) and that was not part of this recursive,
    Code:
     
    f(1)=2, f(2)=2, f(n)=2*f(n-1) + f(n-2)
    So I changed my code to just show f(1), f(2), and f(3).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Creating a user defined number of variables
    By beanroaster in forum C++ Programming
    Replies: 10
    Last Post: 09-14-2005, 03:53 PM
  3. Nasty linker problem; multiply defined
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2005, 03:06 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM