I am writing a method for a CS assignment that is supposed to take in an array of ints and return the array shuffled. I read elsewhere on these boards where someone explained how to do this but with strings. I tried to follow their code making the appropriate changes for ints but I am having a problem.
the code works fine as long as original doesn't contain 0's but if it does then it results in an infinite loop I was wondering how I could maybe modify the code so that it could avoid this
Joined: Jun 09, 2003
here's a slightly different approach
Joined: Sep 18, 2005
Originally posted by russell lloyd: The code works fine as long as original doesn't contain 0's but if it does then it results in an infinite loop I was wondering how I could maybe modify the code so that it could avoid this
The general problem is that you are using the output array elements to hold two separate pieces of information: a not-set-yet flag, and the new value. To do this, the not-set-flag value has to be a value that won't occur in the input array.
If that isn't possible, the algorithm can't work. (You need an extra data structure for the not-set-yet flags.) If it is possible, you need to pre-initialize the output array with the not-set-yet value. [ September 20, 2005: Message edited by: Kym Thompson ]