samedi 23 avril 2016

Shuffling 2 elements in an Array

Have been struggling for a couple of hours.. due to a mind blowing problem I'm just a beginner in C but unfortunately, my professor expects us to do more than what we're actually capable of.

let's say sequence a is {17,9,5,23,21}

and put the sequence into shuffle. when i=1 and j=rand(1,5), swap ai and aj.

if j=3, then the result would be {5,9,17,23,21} after shuffling.

Next, i=2 if j=rand(2,5)=5, the result would be {5,21,17,23,9}

Now, i=3 if j=rand(3,5)=3, the sequence doesn't change

and then i=4. if j=rand(4,5)=5, the result would be {5,21,17,9,23}

'note that the result varies depending on random choices'

I really have no idea where to start... All my friends have been struggling too. Answers would be greatly appreciated. thank you for saving me :)




Aucun commentaire:

Enregistrer un commentaire