-2

for example, this array for example, [1,2,3,4,5] would become this array [5,1,2,3,4]

this is what I have come up with, but it does not work:

 int  *ptr = arr; //initialize to first element
    int  *ptr2 = arr+1; //initialize it to second element

   while (n >0) // keep doing it until size is done with
   {
       *ptr2 = *ptr; 
       ++ptr2;
       ptr ++;
       n--;//
   }
watson
  • 41
  • 1

3 Answers3

3

At *ptr2 = *ptr; you have lost the information about the initial value of the second element.

I hope that gives you an idea why your current approach can't work.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
3

"using only pointers" is a pretty vague requirement. The following, modern solution in a certain way also uses "only pointers", because std::rotate operates on iterators and pointers are iterators:

#include <algorithm>
#include <iostream>

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

    using std::begin;
    using std::end;
    std::rotate(begin(arr), end(arr) - 1, end(arr));

    for (auto&& element : arr)
    {
        std::cout << element << "\n";
    }
}

Yet I feel that the teacher who gave you this assignment would not be happy with it for some reason. As for your solution:

 int  *ptr = arr; //initialize to first element
    int  *ptr2 = arr+1; //initialize it to second element

   while (n >0) // keep doing it until size is done with
   {
       *ptr2 = *ptr; 
       ++ptr2;
       ptr ++;
       n--;//
   }

(I will assume that your n is 4 before the loops begins, because otherwise you will have more than 4 iterations and run into undefined behaviour in the last one.)

In the first iteration of the loop:

  • arr[1] becomes arr[0].
  • ptr2 is made to point to arr[2].
  • ptr is made to point to arr[1].

Array after first iteration: [1, 1, 3, 4, 5]

As you can see, at this point, the element containing value 2 is already lost.

In the second iteration of the loop:

  • arr[2] becomes arr[1].
  • ptr2 is made to point to arr[3].
  • ptr is made to point to arr[2].

Array after second iteration: [1, 1, 1, 4, 5]

Now the element containing value 3 is also lost. The pattern is now clear; you are "losing" all elements and are simply overwriting everything with 1s.

You must find a way to conserve the elements you are overwriting. Or preferably just use std::rotate.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • Why the `&&` in `auto&&`? Shouldn't a plain `auto` suffice? – Emil Laine Oct 11 '15 at 11:39
  • @zenith: To be honest, I'm still in the process of self-discovery with C++11 `auto` best practices. My current opinion is that `auto&&` is best practice in range-based for loops, and that opinion was influenced by SO discussions like this: http://stackoverflow.com/questions/13130708/what-is-the-advantage-of-using-universal-references-in-range-based-for-loops/13130795. But I am really missing the many years of C++11 experience that would IMO be necessary to be really sure. – Christian Hackl Oct 11 '15 at 12:09
  • [This](http://stackoverflow.com/questions/13230480/what-does-auto-tell-us) is also interesting reading. It should be `const auto&&` though. Interestingly enough, while `const auto&` would bind to an rvalue (`const&` won't) you get better performance in certain situations and probably better clarity by using the one intended for rvalues. – Nathan Cooper Oct 11 '15 at 21:02
1

I'm assuming n is the size of the array. If so, you can use the following code:

int tmp = arr[n - 1];          /* Store the last element in `tmp` */

for(int i = n - 1; i > 0; i--) /* Loop backwards */
    arr[i] = arr[i - 1];       /* Move all elements up */

arr[0] = tmp;                  /* Insert the last element at the first location */
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 2
    Note that if `n` is 5, then the OP's original code will have UB due to `ptr2` pointing outside the array's bounds in the last loop iteration. – Christian Hackl Oct 11 '15 at 11:13