Shifting arrays

if my array is 1,2,3,4,5 how can i make it 2,3,4,5,1.

i cant use that. im only allowed to use arrays to solve this.
im only allowed to use arrays

Well using arrays doesn't mean you can't use std::rotate, or std::swap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

template <typename T>
void swap(T& a, T& b)
{
    T temp = a;
    a = b;
    b = temp;
}

template <class ForwardIterator>
  void shift_array (ForwardIterator first, ForwardIterator middle,
               ForwardIterator last)
{
  ForwardIterator next = middle;
  while (first!=next)
  {
    swap (*first++,*next++);
    if (next==last) next=middle;
    else if (first==middle) middle=next;
  }
}

#include <iostream>

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

    shift_array(arr+0, arr+1, arr+5);

    for (int i = 0; i < size; i++)
        std::cout << arr[i] << " ";
}
seems complicated. i was trying to turn this into a for loop. if you ignore the for loop, the code works, but i want it to work with n size array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void shiftright (int myarray[], int size)
{
  
int temp;


for (int i=0; i<(size -1); i++)
{
	      temp = myarray[0];
	myarray[0] = myarray[1];
	myarray[1] = myarray[2];
	myarray[2] = myarray[3];
	myarray[3] = myarray[4];
	myarray[4] = temp;
	
}


}
An important part of programming is seeing similarities between different lines of code, and being able to "factor out" that functionality, like multiplication:
 
3 * x + 3 * y + 3 * z + 3 * w   =   3 * (x + y + z + w)
(Factored out the 3)

So, what you have is
1
2
3
4
5
6
	      temp = myarray[0];
	myarray[0] = myarray[1];
	myarray[1] = myarray[2];
	myarray[2] = myarray[3];
	myarray[3] = myarray[4];
	myarray[4] = temp;


let's look at the middle part,
1
2
3
4
	myarray[0] = myarray[1];
	myarray[1] = myarray[2];
	myarray[2] = myarray[3];
	myarray[3] = myarray[4];

Do you see a pattern?

To me, the pattern is,
myarray[i] = myarray[i+1]
Now, what is i, though? i ranges from 0 to 3, right?

That's where the for loop comes in,
1
2
3
4
5
6
7
int n = 5;
int temp = myarray[0];
for (int i = 0; i < n-1; i++) // i will be in range[0, 4] inclusive
{
    myarray[i] = myarray[i+1];
}
myarray[n-1] = temp;
Last edited on
Or as already suggested you can use std::rotate.

1
2
3
    int array[] = {1,2,3,4,5};
     
    std::rotate(array, array + 1, array + (sizeof(array) / sizeof(int)));


closed account (E0p9LyTq)
Left-shifting an array, manually:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

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

   std::cout << "The original array:\n";
   for (int loop = 0; loop < 5; loop++)
   {
      std::cout << theArray[loop] << ' ';
   }
   std::cout << '\n';

   int temp = theArray[0];
   
   for (int loop = 1; loop < 5; loop++)
   {
      theArray[loop - 1] = theArray[loop];
   }

   theArray[4] = temp;

   std::cout << "\nThe left-shifted array:\n";
   for (int loop = 0; loop < 5; loop++)
   {
      std::cout << theArray[loop] << ' ';
   }
   std::cout << '\n';
}
Topic archived. No new replies allowed.