   Rotating array left and right
  I have an array with certain values, as follows:0 1 2 3 4 5 6 Now, based on a certain parameter (in this case a value being positive/negative), I want to rotate this array either to the left, or to the right, as such:1 2 3 4 5 6 0 (rotated to the left)6 0 1 2 3 4 5 (rotated to the right)The way I''m currently doing it is as follows:   if (parameter){  //rotate left  int temp = array[0];  for (int i=0; i < arraysize-1; ++i)    array[i] = array[i+1];  array[arraysize-1] = temp;}else{  // rotate right  int temp = array[arraysize-1];  for (int i=arraysize-1; i > 0; --i)    array[i] = array[i-1];  array[0] = temp;}However, this feels awkward and unwieldy. I''m pretty sure there''s a better, faster, smaller way to do it... I just can''t find one. Anyone have an idea? 
  Here''s an idea: Don''t rotate the array. Just move the index that points to it''s beginning. Like this: const int width = 10;int array[width] = {1,2,3,4,5,6,7,8,9,10};//initialization for array''s "beginning"int beginning = 0;int beginningprev = width - 1;//print the arrayfor(int i = beginning; i != beginningprev; ++i)  cout << array[i] << ",";cout << endl;//rotate left:beginningprev = beginning;beginning++;if (beginning >= width) beginning = 0;//rotate right:beginning = beginningprev;beginningprev--;if (beginningprev < 0) beginningprev = width - 1;If you really need to actually move the elements around, then your method is fast enough. ;  if you want to be small then use std::rotateto rotate 1 to the left:std::rotate(array, array + 1, array + arraysize);to rotate 1 to the right:std::rotate(array, array + arraysize - 1, array + arraysize); ;  Unfortunately, neither the STD method nor moving the index will work; these values -have- to be on their specific memory adresses (they''re in a hardware device''s video memory)... I''m just accessing them in an array form..I guess there''s no real way to go about it then? Too bad... I was hoping to do something about the if statement... Or the way that I have to go back-to-front when rotating right... Oh well.;quote:Original post by Boris Karloff  Unfortunately, neither the STD method nor moving the index will work; these values -have- to be on their specific memory adresses (they''re in a hardware device''s video memory)... I''m just accessing them in an array form..I''m not sure I appreciate why std::rotate will not work for you. As I understand it, it basically does what you are doing but has already been tested. The memory doesn''t get moved around, just the values. ;  What do you mean the STD method won''t work? Did you even try it?char alpha[] = "abcdefghijklmnopqrstuvwxyz";rotate(alpha, alpha + 13, alpha + 26);printf("%s\n", alpha);// The output is nopqrstuvwxyzabcdefghijklmAnyway, it''s slower than your method (judging from STLPort) because it''s more generic (not restricted to move-by-one).quote: Or the way that I have to go back-to-front when rotating right...Don''t you realize that it''s the only way? You really don''t have many choices in how to implement such a simple algorithm. If you really think this code is "awkward and unwieldy", then you must be some sort of programming god who usually solves everything in max. 5 lines.What will you ask next? How to do x + y in a more compact way? ;  People here is for learning programming, not to use things that there are programmed (std:rotate, ...). Usually they want to know how the things works, so don''t tell him "use std:rotate" and tell him how he can solve a easy programming thing that he not knows yet. ;quote:Original post by Anonymous Poster  People here is for learning programming, not to use things that there are programmed (std:rotate, ...). Usually they want to know how the things works, so don''t tell him "use std:rotate" and tell him how he can solve a easy programming thing that he not knows yet.   And if someone came here asking how to allocate memory, you wouldn''t tell them about malloc(), but rather explain how to implement pooled memory management? The OP already knows how to do this (he even posted code) but wanted a more elegant solution. Moreover, since STL source code is freely available, "use std::rotate" tells not only how to accomplish the task at hand, but points to more information on how that function accomplishes the task. In summary, your opinion is wrong.   How appropriate. You fight like a cow. ;  Not knowing anything else about the array, here's what I would do: const unsigned int SIZE = 7;int arrayMem[SIZE*2] = { 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6 };int* arrayPtr = &array_mem[0];// actualArray you set up to point to your hardware addresses...void rotateArrayLeft() {  ++arrayPtr;  if(arrayPtr - arrayMem >= SIZE)    arrayPtr -= SIZE;  memcpy(actualArray, arrayPtr, SIZE);}void rotateArrayRight() {  --arrayPtr;  if(arrayPtr < arrayMem)    arrayPtr += SIZE;  memcpy(actualArray, arrayPtr, SIZE);}Of course you can wrap this up into a class for encapsulation and expanded functionality.  For instance, changing the nth value in an array requires a change in the nth and the (n+SIZE)th value.Regards,JeffEdit:  Added the memcpy and actualArray...   [edited by - rypyr on August 4, 2003 2:02:27 PM];quote:Original post by Anonymous Poster    What do you mean the STD method won't work? Did you even try it?I mean that it doesn't work because the values are already there. I need to access them, so I create an array pointing to the adress of the first value. If I create a vector, it'll free new memory for me. I can't use that. It has to be in -that- particular range of memory adresses. I'm not -filling- an array with values. The values are already there. I use the array to point to them.It's quite simple. A device has memory at adresses 0x06000000 through 0x06004000. The first 32 bytes are filled with values. I need to change the order of those values. I create an array pointing to the adress 0x06000000. Now, I can use my array to acces these values.Let me know of a way to create a STD::vector that points to a specific memory adress -keeping- all the values already stored there, and it'll work. Simply creating a vector -won't-.quote:Original post by Anonymous Poster    Don't you realize that it's the only way?I'm -asking- if it's the only way. Read my post, please! I appreciate the help, but come on.quote:Original post by Anonymous PosterYou really don't have many choices in how to implement such a simple algorithm. If you really think this code is "awkward and unwieldy", then you must be some sort of programming god who usually solves everything in max. 5 lines.No, but I usually manage to solve things without duplicating the same code twice, with only minor variations, which is what I'm doing now. If that makes me a programming God, bow down to me.quote:Original post by Anonymous Poster    What will you ask next? How to do x + y in a more compact way?  Perhaps... If you post another answer without reading this reply.   [edited by - Boris Karloff on August 4, 2003 2:22:59 PM]