c++ - How do I change pattern from one array to another and then back -
i have original array in c++ containing numbers , change in length, example following simple content:
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; what want change order of array splitting array in blocks of dynamic size like, let 3 (could else in size).
int len = 10; int blocks = 10/3; and use these values split array in blocks 3 numbers:
0,1,2 | 3,4,5 | 6,7,8 | 9 and take first value first block , next value next block like:
0,3, 1,4, 2,5, and jump 2 blocks , add next part like:
6,9, 7, 8 7 , 8 alone since there no more parts in last block these 2 numbers. complete result should like:
0,3,1,4,2,5,6,9,7,8 how solve problem algorithm when last block might not contain complete set of numbers?
and how after create algorithm reverse result array was?
and no isn't assignment, i'm going use shuffle soundfile around make hard others copy file , use want.
edit: here last test attempt got stuck on:
static int test[] = { 0 ,1 ,2, 3, 4, 5, 6, 7, 8, 9}; static int testto[] = { -1 ,-1 ,-1, -1, -1, -1, -1, -1, -1, -1}; int _tmain(int argc, _tchar* argv[]) { int len = 10; int block = len/3.0; (int i=0;i<len;i=i+block*2) { (int y=0;y<block&&i+(y*2)+1<len-1;y++) { testto[i+(y*2)] = test[i+y]; testto[i+(y*2)+1] = test[(i+block)+y]; } } return 0; }
the simplest fix separating out testto counter, makes not have worry complicated code work end of array - can happily generate out of bounds indices, ignore them checking if we've gone out of bounds.
i thought i'd simplify code little.
this came with:
int test[] = { 0 ,1 ,2, 3, 4, 5, 6, 7, 8, 9}; int testto[] = { -1 ,-1 ,-1, -1, -1, -1, -1, -1, -1, -1}; int len = 10; int block = len/3.0; int counter = 0; (int = 0; < len; = + block*2) (int y = 0; y < block; y++) (int z = 0; z < 2; z++) // made loop rather 2 statements { int index = + y + z*block; if (index < len) testto[counter++] = test[index]; } the end of array poses quite bit more of problem when trying convert data back.
would possible pad last pairs of blocks unused value (say 0)? we'd get:
0,1,2 | 3,4,5 | 6,7,8 | 9,0,0 which give output of:
0,3,1,4,2,5,6,9,7,0,8,0 which easier convert back.
alternatively, if you're stuck structure, or feel braving code anyway:
when dealing last pairs of blocks, base need on number of elements remaining in array. take note of fact have 4 elements (6,9,7,8) in our last pairs of blocks, note data in form firstblock secondblock firstblock firstblock - alternate between blocks once, run out of elements in second block, pick first block. more generally, number of times alternate (and number of items in second block) elementsremaining - blocksize, 4-3=1 in case.
Comments
Post a Comment