interleaving 2 arrays - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: interleaving 2 arrays (/thread-925.html) |
interleaving 2 arrays - Mango - 05-23-2003 I don't know if I should post this as a "challenge" or in the "programming help" section...anyway...here it is. What I want to do is to take 2 arrays and interleave them, so that the values from each array are (approxamately) evenly distributed in a third array. I know how to do this, but want to know if anyone has a more elegant way. My method is to create a new array sized the sum of size of the 2 original arrays. Then, this array is filled with alternating chuncks from each array. The size of the chunk is determined by dividing the larger array by the smaller then inserting the whole number followed by a single element from the smaller array. if, for example, array1 contained 3 elements elements: x,x,x and arrat2 contained 11 elements: a,a,a,a,a,a,a,a,a,a,a My method produces a third array containing: aaaxaaaaxaaaax I would like to have the elements more evenly distributed, for example: aaxaaaxaaaxaaa Does anyone see an efficient way to do this? Thanks. The following snippet should get us started on the same page... Code: 'INPUT "Number of elements in Array 1";s1 Thanks Thanks for the response, guys... - Mango - 05-30-2003 since the mixing isn't critical, I decided to just use xxaaaaaxx to mix aaaaa with xxxx thanks again interleaving 2 arrays - Agamemnus - 05-30-2003 errr if it turns out to be critical... say that second array is smaller than the first array. Goes something like this (untested and prolly faulty): Code: ratio% = len.first.array% / len.second.array% interleaving 2 arrays - Mango - 05-31-2003 Quote:errr if it turns out to be critical... Thanks for the response. It turns out that your code idea had some...um... problems :roll: however, it got me thinking. I was hoping there was an easier way than what I ended up using, since interleaving the arrays was just a very small part of a larger program...anyway...the following sub will do the trick with any 2 arrays. The example shown uses text arrays to make the demo-display easy to understand. However, the array mixer works fine on any datatype, as long as it's correctly specified in the call. Code: DEFINT A-Z |