473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

comparing multiple values...

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

Jul 23 '05 #1
2 3617
On 14 Jun 2005 18:49:13 -0700, "Chaz" <ch********@aol.com> 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/~a...FAQ-acllc.html
Jul 23 '05 #2
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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: twdo | last post by:
Let me see if I can explain my situation clearly. I have a table with the columns: answer_id, question_id, member_id, answer - answer_id is the primary key for the table. - question_id...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
5
by: Frank | last post by:
Hello All, I am working on a vb.net app where I need to compare to 2 datatables and determine if a string exists in one or both. The first dt is filled from the db. A form is loaded and the...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
2
by: Yash | last post by:
Hi, We are in the process of tuning the performance of our stored procs in SQL 2000 and are looking for a tool that would help us in comparing the result sets of an old SP and a modified SP. The...
0
by: DomiNeug | last post by:
Hello, Since a while i have to find a way of comparing "Sets" (multiple int Values) and so to find equal sets. There simply 3 tables ValueList with: ID int ValueListHasValue: ID int, ValueListID...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.