tags:

views:

96

answers:

7

How can I move elements in an array to the next element

eg: x[5] = { 5, 4, 3, 2, 1 }; // initial values
    x[0] = 6; // new values to be shifted
    x[5] = { 6, 5, 4, 3, 2 }; // shifted array, it need to be shifted, 
                              // not just increment the values.

This what I've done so far. It's wrong, that's why I need help here. Thanks in advance.

#include <iostream>

using namespace std;

int main() 
{
  int x[5] = { 5, 4, 3, 2, 1 };

  int array_size = sizeof(x) / sizeof(x[0]);

  x[0] = 6;

  int m = 1;

  for(int j = 0; j < array_size; j++) {
    x[m+j] = x[j];
    cout << x[j] << endl;
  }

  return 0;
}
+3  A: 

First of all, you should shift the old values in the array before you write the new value. But instead of a loop, you are better of using memmove(). Or even better with std::vector instead of an array - it handles all these low-level issues for you, including automatically resizing the array when needed.

Péter Török
+1 for `memmove`
Alexandre C.
But I guess this is homework, so loops and index manipulation is required I guess.
Cedric H.
+7  A: 

To "move rightwards" you have to iterate from the end of array:

for(int j = array_size - 2; j >= 0; j--) {
   x[m+j] = x[j];
   cout << x[j] << endl;
}   

otherwise you just overwrite all the elements with the 0th element.

Please note array_size - 2 - otherwise you have "off by one" trying to access the element beyond the array end and that's undefined behavior.

sharptooth
+1 good catch, missed by everyone else (including myself :-)
Péter Török
doesn't work. output become 2,3,4,6
azam
@azam: I only pointed out the major problem in your code. You still have to put the value into the [0] element yourself and you'll have to iterate over the array for the second time to get the output.
sharptooth
@sharptooth. I see. Thank you for your input.
azam
A: 

In the general case where you need to shift m elements (where 0 <= m <n): Start from the end of the array. If you start at the begining (index 0) then you overwrite and then move that overridden value.

Studying the source code of std::memmove may be instructive as well.

dirkgently
+3  A: 
#include <iostream>
int
main ()
{
  int x[5] = { 5, 4, 3, 2, 1 };

  int array_size = sizeof (x) / sizeof (x[0]);

  for (int j = array_size - 1; j > 0; j--)
    {
      x[j] = x[j - 1];
    }

  x[0] = 6;

  for (int j = 0; j < array_size; j++)
    {
      std::cout << x[j];
    }

  return 0;
}
Cedric H.
@Cedric H. Thank you so much.
azam
A: 

You can start from the end of the array. You copy the

  • element in 2nd last position to the last position,
  • element in 3rd last position to the 2nd last position,
  • ....
  • element in first position(index 0) to the 2nd position and finally
  • copy the new number in the first position. .

.

for(j = array_size-1; j >0; j--) {
 x[j] = x[j-1];
}
x[0] = 6;
codaddict
+1  A: 
#include<algorithm>

// ...
std::rotate(x, x+4, x+5);
x[0] = 6;
wilhelmtell
@wilhelmtell. sorry. i don't want to use algo.
azam
+1 From me for actually providing a C++ solution.
FredOverflow
A: 
    #include <iostream>

    using namespace std;

    int main() 
    {
       int x[5] = { 5, 4, 3, 2, 1 };

        int array_size = sizeof(x) / sizeof(x[0]);

        int m = 1;

        for(int j = array_size-1; j > 0; j--) {
           x[j] = x[j-m];
           cout << x[j] << endl;
        }

       x[0] = 6;
       return 0;
    }
Anil Vishnoi
your output is 2,3,4,5
azam