Click here to Skip to main content
15,907,396 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: HELP: source code of TIFF viewer, thanks Pin
Anonymous18-Jun-03 7:44
Anonymous18-Jun-03 7:44 
GeneralRe: HELP: source code of TIFF viewer, thanks Pin
John M. Drescher18-Jun-03 7:08
John M. Drescher18-Jun-03 7:08 
GeneralRe: HELP: source code of TIFF viewer, thanks Pin
Anonymous18-Jun-03 7:32
Anonymous18-Jun-03 7:32 
GeneralRe: HELP: source code of TIFF viewer, thanks Pin
John M. Drescher18-Jun-03 8:00
John M. Drescher18-Jun-03 8:00 
GeneralHELP: source code of TIFF viewer Pin
Anonymous18-Jun-03 5:48
Anonymous18-Jun-03 5:48 
GeneralAuto-Hide Views Pin
Mark Donkers18-Jun-03 5:29
Mark Donkers18-Jun-03 5:29 
GeneralRe: Auto-Hide Views Pin
nfactorial18-Jun-03 6:46
nfactorial18-Jun-03 6:46 
GeneralImplementing a Fisher-Yates Algorithm in C++ Pin
John Aldrich18-Jun-03 5:07
John Aldrich18-Jun-03 5:07 
I have some old perl code that I found in the book "Perl Database Programming" by Brent Michalski that I need to port to C++ for use with another project that needs in place array shuffling. I've analyzed the perl code back to front and am still not sure how implement the same thing in C++. For inspection, I have the full source for the perl subroutine with line by line comments verbatim from the book for those who don't have an understanding of perl following the source code:

<code>
1: sub fisher_yates_shuffle {
2:     my $array = shift;      
3:     my $i;             
4:     for($i = @$array; --$i) {
5:        my $j = int rand ($i+1);
6:        next if $i == $j;
7:        @$array[$i,$j] = @$array[$j,$i];
8:     }
9: } 
</code>


Line 1 begins the fisher_yates_shuffle subroutine. This subroutine is designed to take a reference to an array as the input, and it will randomize the items in the array in place. This means that we pass a reference to an array and the array gets randomized. This is a commonly used method for randomizing arrays, the "Fisher-Yates shuffle," named after Sir Ronald A. Fisher and Frank Yates, who introduced the algorithm in example 12 of their 1938 book Statistical Tables.

Line 2 declares a scalar variable named $array and shifts the value passed to the subroutine into it. This value should be a reference to an array.

Line 3 declares a scalar variable named $i

Line 4 begins a for loop that sets $i to the current value each time through the loop and also decrements $i. This has the effect of setting $i to the number of elements in the array @$array and then counting down one by one.

Line 5 declares a scalar variable named $j and sets it to a random integer between 1 and $i

Line 6 causes the for loop to skip to the next iteration if $i and $j are equal

Line 7 swaps the values at $array[$i] and $array[$j]

Line 8 ends the for loop that we began on Line 6

Line 9 ends this subroutine.


As I've said, I've been over this thing a dozen times, and a solution in C++ just isn't coming to me, so I'd appreciate any help that anyone woudl be will to provide. I think that the perl source I've included gives enough of a general idea of what needs to happen along with the source comments that a solution would be apparent to any seasoned programmer ( which when it comes to C I'm definately not Poke tongue | ;-P ) Thanks in advance for any help that you folks can provide.



It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now...

[Shog9 on Kid Programmers]
GeneralRe: Implementing a Fisher-Yates Algorithm in C++ Pin
AlexO18-Jun-03 5:33
AlexO18-Jun-03 5:33 
GeneralRe: Implementing a Fisher-Yates Algorithm in C++ Pin
Ryan Binns18-Jun-03 15:19
Ryan Binns18-Jun-03 15:19 
GeneralRe: Implementing a Fisher-Yates Algorithm in C++ Pin
Ryan Binns18-Jun-03 15:24
Ryan Binns18-Jun-03 15:24 
QuestionLBS_OWNERDRAWFIXED error? Pin
Jim D18-Jun-03 4:59
Jim D18-Jun-03 4:59 
AnswerRe: LBS_OWNERDRAWFIXED error? Pin
Maximilien18-Jun-03 5:07
Maximilien18-Jun-03 5:07 
GeneralRe: LBS_OWNERDRAWFIXED error? Pin
Jim D18-Jun-03 7:49
Jim D18-Jun-03 7:49 
GeneralUNC Pin
RChin18-Jun-03 4:53
RChin18-Jun-03 4:53 
GeneralModeless Property Sheet Pin
Jay Hova18-Jun-03 4:49
Jay Hova18-Jun-03 4:49 
GeneralRe: Modeless Property Sheet Pin
Joan M18-Jun-03 5:18
professionalJoan M18-Jun-03 5:18 
GeneralRe: Modeless Property Sheet Pin
Jay Hova18-Jun-03 8:16
Jay Hova18-Jun-03 8:16 
GeneralRe: Modeless Property Sheet Pin
Ryan Binns18-Jun-03 15:26
Ryan Binns18-Jun-03 15:26 
GeneralRe: Modeless Property Sheet Pin
Jay Hova19-Jun-03 1:39
Jay Hova19-Jun-03 1:39 
GeneralRe: Modeless Property Sheet Pin
Ryan Binns19-Jun-03 1:51
Ryan Binns19-Jun-03 1:51 
Generaldesign question Pin
monrobot1318-Jun-03 3:36
monrobot1318-Jun-03 3:36 
GeneralRe: design question Pin
valikac18-Jun-03 6:54
valikac18-Jun-03 6:54 
GeneralSerial communication problem Pin
justin22318-Jun-03 2:31
justin22318-Jun-03 2:31 
GeneralRe: Serial communication problem Pin
justin22318-Jun-03 3:17
justin22318-Jun-03 3:17 

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.