Click here to Skip to main content
15,891,184 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionSolved - Two-Dimensional Array not reading right [modified] Pin
Frank Robertson15-May-10 16:20
Frank Robertson15-May-10 16:20 
AnswerRe: Two-Dimensional Array not reading right PinPopular
Luc Pattyn15-May-10 17:36
sitebuilderLuc Pattyn15-May-10 17:36 
GeneralRe: Two-Dimensional Array not reading right Pin
Frank Robertson15-May-10 19:30
Frank Robertson15-May-10 19:30 
GeneralRe: Two-Dimensional Array not reading right Pin
Luc Pattyn16-May-10 1:07
sitebuilderLuc Pattyn16-May-10 1:07 
GeneralRe: Two-Dimensional Array not reading right Pin
Frank Robertson17-May-10 13:00
Frank Robertson17-May-10 13:00 
GeneralCongrats Pin
Luc Pattyn17-May-10 13:11
sitebuilderLuc Pattyn17-May-10 13:11 
GeneralRe: Two-Dimensional Array not reading right Pin
Iain Clarke, Warrior Programmer16-May-10 20:49
Iain Clarke, Warrior Programmer16-May-10 20:49 
AnswerRe: Two-Dimensional Array not reading right Pin
Aescleal15-May-10 22:42
Aescleal15-May-10 22:42 
Hi Frank,

Just to expand a bit on Luc's comment above, there's usually a simpler way of doing things when you see repeated code. Have a look at the facilities provided by the <algorithm> header, you'll find plenty of ways to simplify loops. In your case you're transforming the contents of one collection into the contents of another which is exactly what std::transform does.

So taking Luc's comments one stage further you can do most of what you were trying to do with your initialisation loops in two lines:

std::transform( hand, hand + 5, handColumn, []( int n ){ return n / 13; } );
std::transform( hand, hand + 5, handRow,    []( int n ){ return n % 13; } );


The arguments to std::transform are a bit weird looking if you've never seen them before:
- the first two describe where you want to get the data to transform from
- the third describes where you want to put the transformed data
- the fourth describes how you want to transform it

So in both cases you're wanting to operate on data from hand[ 0 ] to hand[ 5 ] (not including hand[ 5 ]) and stick the transformed data into handRow or handColumn starting at the beginning. The last parameter is a lambda (it starts with empty square brackets)that says what you want the tranformation to be division by 13 in the first case and modulo by 13 in the second case.

(lambda's are inline objects that behave like function calls. They're part of the C++0x standard - VC++ 2010 and gcc 4.4 support them though so it's easy to get a free compiler that knows about them.)

Going a bit further... even with these changes it takes a bit of looking at the code to work out what's going on. As far as I can tell you're taking an integer representation of a playing card and decoding it into an integer representation of a suit and a card face. Both aren't that obvious - it looks like you're missing a few abstractions in your code. If you had these abstractions you wouldn't be worrying about decoding the card's suit and face value from an integer, it'd just happen.

Anyway, I can witter on a bit more if you want - otherwise the important message to take from this is handcrafted loops are often not as effective as taking an algorithm from <algorithm> and applying it.

Cheers,

Ash
GeneralRe: Two-Dimensional Array not reading right Pin
Frank Robertson16-May-10 0:51
Frank Robertson16-May-10 0:51 
QuestionLinux commands Pin
TTPadmin15-May-10 12:07
TTPadmin15-May-10 12:07 
AnswerRe: Linux commands Pin
markkuk15-May-10 12:17
markkuk15-May-10 12:17 
GeneralRe: Linux commands Pin
TTPadmin15-May-10 12:29
TTPadmin15-May-10 12:29 
GeneralRe: Linux commands Pin
CPallini15-May-10 12:39
mveCPallini15-May-10 12:39 
AnswerRe: Linux commands Pin
Paul M Watt15-May-10 20:24
mentorPaul M Watt15-May-10 20:24 
GeneralRe: Linux commands Pin
TTPadmin17-May-10 12:12
TTPadmin17-May-10 12:12 
GeneralRe: Linux commands Pin
TTPadmin19-May-10 12:19
TTPadmin19-May-10 12:19 
QuestionInteracting with a 3rd party application dialog window Pin
Still learning how to code15-May-10 11:55
Still learning how to code15-May-10 11:55 
AnswerRe: Interacting with a 3rd party application dialog window Pin
Paul M Watt15-May-10 20:37
mentorPaul M Watt15-May-10 20:37 
GeneralRe: Interacting with a 3rd party application dialog window - ANSWERED ! Pin
Still learning how to code15-May-10 21:28
Still learning how to code15-May-10 21:28 
Questionfunction doesn't reply Pin
hasani200715-May-10 9:51
hasani200715-May-10 9:51 
AnswerRe: function doesn't reply PinPopular
Code-o-mat15-May-10 10:14
Code-o-mat15-May-10 10:14 
AnswerRe: function doesn't reply Pin
Dr.Walt Fair, PE15-May-10 10:28
professionalDr.Walt Fair, PE15-May-10 10:28 
AnswerRe: function doesn't reply Pin
Software_Developer15-May-10 10:37
Software_Developer15-May-10 10:37 
GeneralRe: function doesn't reply Pin
Member 392263917-May-10 2:36
Member 392263917-May-10 2:36 
GeneralRe: Thanks Pin
Software_Developer17-May-10 5:13
Software_Developer17-May-10 5:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.