Question: A vector variable vi holding a collection of integer values needs to be shuffled so that the resulting vector vi will hold a shuffled order
A vector variable vi holding a collection of integer values needs to be "shuffled" so that the resulting vector vi will hold a shuffled order of their original values. This can be done easily by swapping two values that are chosen from a position i of the vector vi and another position j (i and j may be same) in which i and j are generated at random. Suppose you have vi holding 20 values from 1 through 20 from the front element to the back element. Apply 20 random swaps to produce a shuffled result. See below for the example of the required execution session.
1, 8, 13, 7, 5, 17, 9, 3, 2, 18, 11, 10, 14, 19, 15, 16, 12, 6, 4, 20 Bye...
****C++
gives the output of 8, 5, 19, 4, 14, 20, 13, 3, 10, 2, 6, 9, 11, 7, 16, 12, 17, 18, 1, 15
Bye...****************
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
