comparing multiple values...

Discussion in 'C++' started by Chaz, Jun 15, 2005.

  1. Chaz

    Chaz Guest

    Hello, I hope someone can help me out. I am going to be taking the
    third step in a programming class soon(I took the previous two a while
    ago at a different school) and in an effort to get back up to speed, I
    bought the Dietel book How To Program 5th ed. I am going through some
    examples trying to work them out, and have been doing pretty good, but
    came across one exercise that has me stumped. It's a card shuffling
    program that deals 5 cards, and stores the face value and suit in
    arrays respectively. I have pasted the code which I hope shows that.

    // to deal only 5 cards for a poker hand
    for ( int card = 1; card <= 5; card++ )
    {
    // loop through rows of deck
    for ( int row = 0; row <= 3; row++ )
    {
    // loop through columns of deck for current row
    for ( int column = 0; column <= 12; column++ )
    {
    // if slot contains current card, display card
    if ( deck[ row ][ column ] == card )
    {
    cout << setw( 5 ) << right << face[ column ]
    << " of " << setw( 8 ) << left << suit[ row ]
    << ( card % 2 == 0 ? '\n' : '\t' );
    } // end if
    } // end innermost for
    } // end inner for
    } // end outer for

    What this example asks to do is to modify the program so that it can
    determine if the hand has a pair. (and three of a kind and so on)
    Considering the "cards" are stored in arrays, how would you compare all
    5 face values for example and be able to tell their are two of a kind?

    Thanks for taking the time to look at this and any help you can
    provide.

    Chaz
    Chaz, Jun 15, 2005
    #1
    1. Advertising

  2. Chaz

    Jack Klein Guest

    On 14 Jun 2005 18:49:13 -0700, "Chaz" <> wrote in
    comp.lang.c++:

    > Hello, I hope someone can help me out. I am going to be taking the
    > third step in a programming class soon(I took the previous two a while
    > ago at a different school) and in an effort to get back up to speed, I
    > bought the Dietel book How To Program 5th ed. I am going through some
    > examples trying to work them out, and have been doing pretty good, but
    > came across one exercise that has me stumped. It's a card shuffling
    > program that deals 5 cards, and stores the face value and suit in
    > arrays respectively. I have pasted the code which I hope shows that.
    >
    > // to deal only 5 cards for a poker hand
    > for ( int card = 1; card <= 5; card++ )
    > {
    > // loop through rows of deck
    > for ( int row = 0; row <= 3; row++ )
    > {
    > // loop through columns of deck for current row
    > for ( int column = 0; column <= 12; column++ )
    > {
    > // if slot contains current card, display card
    > if ( deck[ row ][ column ] == card )
    > {
    > cout << setw( 5 ) << right << face[ column ]
    > << " of " << setw( 8 ) << left << suit[ row ]
    > << ( card % 2 == 0 ? '\n' : '\t' );
    > } // end if
    > } // end innermost for
    > } // end inner for
    > } // end outer for
    >
    > What this example asks to do is to modify the program so that it can
    > determine if the hand has a pair. (and three of a kind and so on)
    > Considering the "cards" are stored in arrays, how would you compare all
    > 5 face values for example and be able to tell their are two of a kind?
    >
    > Thanks for taking the time to look at this and any help you can
    > provide.
    >
    > Chaz


    I'll give you a suggestion, not the code.

    Create an array of 13 ints, one for each face value, and initialize
    all the elements of this array to 0. Decide on a mapping between the
    13 different card faces, if the program does not provide one already.

    Now look at each of the 5 cards in the hand. For each card, increment
    the element of the array that corresponds to its face value.

    Final step, loop through the 13 elements of the array. If any element
    has the value 2, you have a pair of the corresponding card face. If
    any element has 3, you have three of a kind, a 4 means four of a kind,
    and a 5 means you are playing with a crooked deck.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 15, 2005
    #2
    1. Advertising

  3. Chaz

    Chaz Guest

    Jack,

    Thanks, I think I follow.

    The program maps the cards in a 2-d array, 4x13. Normally, it shuffles
    all 52 cards, and randomly selects a number between 1-52 and puts it
    into an element on the array. I changed it so that instead of it
    randomly selecting 52 cards, it only selects 5 as to select 5 cards(I
    would post the code but I am at work and don't have it with me).

    I will work on it tonight and see what I come up with.

    Thanks again.

    Chaz
    Chaz, Jun 15, 2005
    #3
    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. Jorgen Gustafsson
    Replies:
    4
    Views:
    570
    Jorgen Gustafsson
    Dec 12, 2003
  2. darrel
    Replies:
    2
    Views:
    5,048
    darrel
    Dec 23, 2005
  3. Kristof Bastiaensen

    RCR: Comparing multiple values

    Kristof Bastiaensen, Apr 25, 2004, in forum: Ruby
    Replies:
    5
    Views:
    101
    Kristof Bastiaensen
    Apr 26, 2004
  4. Greg Lazarev

    Comparing variable to multiple values

    Greg Lazarev, Nov 11, 2008, in forum: Ruby
    Replies:
    8
    Views:
    117
    Brian Adkins
    Nov 11, 2008
  5. Jason
    Replies:
    8
    Views:
    128
    Dr.Ruud
    Jul 27, 2006
Loading...

Share This Page