recursive function for the bear game

Discussion in 'C++' started by ChuckB, Nov 8, 2006.

  1. ChuckB

    ChuckB Guest

    Ok, heres the code i've come up with

    #include <iostream>
    using namespace std;
    bool bear (int);

    void main( )
    {
    bear(250); // is true
    bear(42); // is true
    bear(84); // is true
    bear(53); // is false
    bear(41); // is false
    }

    bool bear(int n)
    {
    cout << n;
    if (n == 42)
    {
    cout << "true" << endl;
    return true;
    }
    else
    {
    if (n % 2 == 0)
    {
    n = n/2;
    bear (n);
    }
    else if ((n % 3 == 0)||(n % 4 == 0))
    {
    if ((n % 10 != 0) || (((n%100)/10) != 0))
    {
    int t = ((n%100)/10)*(n % 10);
    n = n - t;
    bear(n);
    }
    }
    else if (n % 5 == 0)
    {
    n = n - 42;
    bear(n);
    }
    else
    {
    cout << "false" << endl;
    return false;
    }
    }
    }

    The problem is of course that bear(250); will not equal true because of
    the order of the commands. I guess i'm going to need to do backtracking
    on this, however I am very confused when i comes to doing backtracking.
    If anyone could help me it would be greatly apprietiated.
     
    ChuckB, Nov 8, 2006
    #1
    1. Advertising

  2. ChuckB

    Daniel T. Guest

    "ChuckB" <> wrote:

    > Ok, heres the code i've come up with


    I'm not sure what "the bear game" is, but if you add the line I indicate
    below, you will see where your problem lies.
    >
    > #include <iostream>
    > using namespace std;
    > bool bear (int);
    >
    > void main( )
    > {
    > bear(250); // is true
    > bear(42); // is true
    > bear(84); // is true
    > bear(53); // is false
    > bear(41); // is false
    > }
    >
    > bool bear(int n)
    > {
    > cout << n;
    > if (n == 42)
    > {
    > cout << "true" << endl;
    > return true;
    > }
    > else
    > {
    > if (n % 2 == 0)
    > {
    > n = n/2;
    > bear (n);
    > }
    > else if ((n % 3 == 0)||(n % 4 == 0))
    > {
    > if ((n % 10 != 0) || (((n%100)/10) != 0))
    > {
    > int t = ((n%100)/10)*(n % 10);
    > n = n - t;
    > bear(n);
    > }
    > }
    > else if (n % 5 == 0)
    > {
    > n = n - 42;
    > bear(n);
    > }
    > else
    > {
    > cout << "false" << endl;
    > return false;
    > }
    > }


    assert( false && "what should happen here?" );
    > }
    >
    > The problem is of course that bear(250); will not equal true because of
    > the order of the commands. I guess i'm going to need to do backtracking
    > on this, however I am very confused when i comes to doing backtracking.
    > If anyone could help me it would be greatly apprietiated.


    --
    To send me email, put "sheltie" in the subject.
     
    Daniel T., Nov 8, 2006
    #2
    1. Advertising

  3. ChuckB

    Daniel T. Guest

    In article <>,
    "ChuckB" <> wrote:

    > Ok, heres the code i've come up with
    >
    > #include <iostream>
    > using namespace std;
    > bool bear (int);
    >
    > void main( )
    > {
    > bear(250); // is true
    > bear(42); // is true
    > bear(84); // is true
    > bear(53); // is false
    > bear(41); // is false
    > }
    >


    Everywhere in the function below where you call "bear(n);" you need to
    change it to "return bear(n);"

    > bool bear(int n)
    > {
    > cout << n;
    > if (n == 42)
    > {
    > cout << "true" << endl;
    > return true;
    > }
    > else
    > {
    > if (n % 2 == 0)
    > {
    > n = n/2;
    > bear (n);
    > }
    > else if ((n % 3 == 0)||(n % 4 == 0))
    > {
    > if ((n % 10 != 0) || (((n%100)/10) != 0))
    > {
    > int t = ((n%100)/10)*(n % 10);
    > n = n - t;
    > bear(n);
    > }
    > }
    > else if (n % 5 == 0)
    > {
    > n = n - 42;
    > bear(n);
    > }
    > else
    > {
    > cout << "false" << endl;
    > return false;
    > }
    > }
    > }
    >
    > The problem is of course that bear(250); will not equal true because of
    > the order of the commands. I guess i'm going to need to do backtracking
    > on this, however I am very confused when i comes to doing backtracking.
    > If anyone could help me it would be greatly apprietiated.


    --
    To send me email, put "sheltie" in the subject.
     
    Daniel T., Nov 8, 2006
    #3
  4. ChuckB

    Mark P Guest

    ChuckB wrote:
    > Ok, heres the code i've come up with
    >

    [snip]
    >
    > The problem is of course that bear(250); will not equal true because of
    > the order of the commands. I guess i'm going to need to do backtracking
    > on this, however I am very confused when i comes to doing backtracking.
    > If anyone could help me it would be greatly apprietiated.
    >


    How can we possibly help you when you haven't explained to any of us
    what you're trying to do?
     
    Mark P, Nov 8, 2006
    #4
  5. ChuckB

    ChuckB Guest

    I guess it would help to explain the problem. Sorry about that... Here
    it is.

    The game starts when I give you some bears.
    You can then give back some bears, but you must follow these rules
    (where n is the number of bears that you have):

    1. If n is even, then you may give back exactly n/2 bears.
    2. If n is divisible by 3 or 4, then you may multiply the last two
    digits of n and give back this many bears. (By the way, the last digit
    of n is n%10, and the next-to-last digit is ((n%100)/10).
    3. If n is divisible by 5, then you may give back exactly 42 bears.

    The goal of the game is to end up with EXACTLY 42 bears.

    For example, suppose that you start with 250 bears. Then you could make
    these moves:
    # --Start with 250 bears.
    # --Since 250 is divisible by 5, you may return 42 of the bears,
    leaving you with 208 bears.
    # --Since 208 is even, you may return half of the bears, leaving you
    with 104 bears.
    # --Since 104 is even, you may return half of the bears, leaving you
    with 52 bears.
    # --Since 52 is divisible by 4, you may multiply the last two digits
    (resulting in 10) and return these 10 bears. This leaves you with 42
    bears.
    # --You have reached the goal!

    Write a recursive function to meet this specification:
     
    ChuckB, Nov 8, 2006
    #5
  6. ChuckB

    Daniel T. Guest

    "ChuckB" <> wrote:

    > I guess it would help to explain the problem. Sorry about that... Here
    > it is.
    >
    > The game starts when I give you some bears.
    > You can then give back some bears, but you must follow these rules
    > (where n is the number of bears that you have):
    >
    > 1. If n is even, then you may give back exactly n/2 bears.
    > 2. If n is divisible by 3 or 4, then you may multiply the last two
    > digits of n and give back this many bears. (By the way, the last digit
    > of n is n%10, and the next-to-last digit is ((n%100)/10).
    > 3. If n is divisible by 5, then you may give back exactly 42 bears.
    >
    > The goal of the game is to end up with EXACTLY 42 bears.
    >
    > For example, suppose that you start with 250 bears. Then you could make
    > these moves:
    > # --Start with 250 bears.
    > # --Since 250 is divisible by 5, you may return 42 of the bears,
    > leaving you with 208 bears.
    > # --Since 208 is even, you may return half of the bears, leaving you
    > with 104 bears.
    > # --Since 104 is even, you may return half of the bears, leaving you
    > with 52 bears.
    > # --Since 52 is divisible by 4, you may multiply the last two digits
    > (resulting in 10) and return these 10 bears. This leaves you with 42
    > bears.
    > # --You have reached the goal!
    >
    > Write a recursive function to meet this specification:


    Your going to have to write a search routine to solve this one, probably
    a depth first search. Read up on them. The code you currently have is
    unsuitable.

    --
    To send me email, put "sheltie" in the subject.
     
    Daniel T., Nov 8, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Swami Tota Ram Shankar
    Replies:
    2
    Views:
    1,080
    Trent Buck
    Dec 5, 2004
  2. Swami Tota Ram Shankar
    Replies:
    2
    Views:
    352
    Trent Buck
    Dec 5, 2004
  3. FSX
    Replies:
    17
    Views:
    569
  4. John Ramsden

    Bear trap in scoping rules

    John Ramsden, Sep 25, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    87
    Michael Carman
    Sep 27, 2003
  5. The Natural Philosopher

    probably OT, but bear me out..firefox javsacsript?

    The Natural Philosopher, Mar 16, 2010, in forum: Javascript
    Replies:
    1
    Views:
    107
Loading...

Share This Page