# Qbasicnews.com

## QbasicNews.Com => Challenges => Topic started by: Mango on May 22, 2003, 04:25:01 PM

 Title: interleaving 2 arrays Post by: Mango on May 22, 2003, 04:25:01 PM 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,xand arrat2 contained 11 elements: a,a,a,a,a,a,a,a,a,a,aMy method produces a third array containing:aaaxaaaaxaaaaxI would like to have the elements more evenly distributed, for example: aaxaaaxaaaxaaaDoes 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'Input "Range for values in array 1 (0 to ?)";r1'Input "Number of elements in Array 2";s2'input "range for values in array 2 (0 to ?)";r2s1=100   'array1...100 elements between 0 and 5r1=5s2=20    'array2...20 elements between 0 and 30r2=30dim array1(1 to s1) as integerdim array2(1 to s2) as integerfor x=1 to s1array1(x)=rnd*r1   'fill array1 with valuesnext xfor x=1 to s2       'fill array2 with valuesarray2(x)=rnd*r2next xDIM array3(1 TO (UBOUND(a)-LBOUND(a))+(UBOUND(b)-LBOUND(b))) AS INTEGER'How to fill array3 with interleaved elements of array1 and array2???Thanks Title: Thanks for the response, guys... Post by: Mango on May 29, 2003, 10:05:55 PM since the mixing isn't critical, I decided to just usexxaaaaaxxto mix aaaaa with xxxxthanks again Title: interleaving 2 arrays Post by: Agamemnus on May 30, 2003, 01:00:36 PM 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%len.third.array% = len.first.array%+len.second.array%extra% = (len.first.array% - ratio% * len.first.array%) / 2if extra% > 0 thenextra1% = extra%/2extra2% = extra%-extra1%for i% = 1 to extra1%third.array%(I%) = first.array%(I%)next i%for i% = len.third.array% - extra2% + 1 to len.third.array%third.array%(I%) = first.array%(I%)next i%end iffor I% = extra%+1 to len.third.array% - extra%if j%< ratio% then j% = j% + 1 else j% = 0if j%>0 then third.array%(i%) = first.array%(i%) else third.array%(i%) = second.array%(i%)next i% Title: interleaving 2 arrays Post by: Mango on May 31, 2003, 02:23:50 AM Quote from: "Agamemnus"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):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-ZDECLARE SUB mix (small() AS STRING, large() AS STRING, combined() AS STRING)CLSDO  DO   INPUT "size of array1"; sa1   INPUT "size of array2"; sa2  LOOP UNTIL sa1 > 0 AND sa2 > 0  combosize = sa1 + sa2  REDIM array1(1 TO sa1) AS STRING  REDIM array2(1 TO sa2) AS STRING  REDIM combo(1 TO combosize) AS STRING  FOR x = 1 TO sa1    array1(x) = "x"    PRINT array1(x);  NEXT x  PRINT  FOR x = 1 TO sa2    array2(x) = "a"    PRINT array2(x);  NEXT x  PRINT  IF UBOUND(array1) > UBOUND(array2) THEN  'put small array first in argument list    CALL mix(array2(), array1(), combo())  ELSE    CALL mix(array1(), array2(), combo())  END IF  FOR x = 1 TO UBOUND(combo)    PRINT combo(x);  NEXT x  PRINTLOOPENDSUB mix (small() AS STRING, large() AS STRING, combined() AS STRING)SAS = UBOUND(small)     'Small Array SizeLAS = UBOUND(large)      'Large Array SizeCAS = UBOUND(combined)  'Combined Array SizeTC = SAS + 1               'Total ChunksSCS = LAS \ (SAS + 1)      'Small Chunk SizeLCS = SCS + 1              'Large Chunk SizeSAP = 1: LAP = 1: CAP = 1  'Small, Large, Combined Array PositionNLC = LAS - (SCS * TC)     'Number of large chunksFOR x = 1 TO NLC                  'interleave starting with large chunks    FOR y = 1 TO LCS      combined(CAP) = large(LAP)      CAP = CAP + 1: LAP = LAP + 1       NEXT y    combined(CAP) = small(SAP)    CAP = CAP + 1: SAP = SAP + 1NEXT xFOR x = 1 TO TC - NLC              'interleave small chunks    FOR y = 1 TO SCS      combined(CAP) = large(LAP)      CAP = CAP + 1: LAP = LAP + 1      NEXT y    IF x = TC - NLC THEN EXIT SUB  'out of data...exit sub    combined(CAP) = small(SAP)    CAP = CAP + 1: SAP = SAP + 1NEXT xEND SUB