2

A array contains element 10,20,30,40,50 What i wanna rotate the complete array so as it will cout will stream elements 50,40,30,20,10

I want to solve this problem using rotate function

i tried to write rotate(arr,arr+4,arr+1);

  #include<iostream>
  #include<algorithm>
  using namespace std;
  int main()
  {
      int arr[]={10,20,30,40,50};

      rotate(arr,arr+4,arr+1);
      int i;

      for(i=0; i<5; ++i)
      {
       cout<<arr[i]<<"  ";
      }
   }

by running above program i getting output 50 10 20 30 40 which is wrong the actual output is 50 40 30 20 10

aman_49
  • 116
  • 10

3 Answers3

3

The issue is that you picked the wrong algorithm (quotes from https://en.cppreference.com/w/, emphasis mine):

Specifically, std::rotate swaps the elements in the range [first, last) in such a way that the element n_first becomes the first element of the new range and n_first - 1 becomes the last element.

What you need is std::reverse(first, last), which

Reverses the order of the elements in the range [first, last) Behaves as if applying std::iter_swap to every pair of iterators first+i, (last-i) - 1 for each non-negative i < (last-first)/2

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • thank u so much Bob_ for replying i already referred https://en.cppreference.com/w/cpp/header in which i found using vector.h file Can i write function without vector type – aman_49 Aug 02 '19 at 09:16
  • @AlexStacy Not sure to understand your comment, but you can use either `std::reverse(std::begin(arr), std::end(arr));` or `std::reverse(arr, arr + 5);` with your array. – Bob__ Aug 02 '19 at 09:35
1

std::rotate: "Rotates the order of the elements in the range (first,last), in such a way that the element pointed by middle becomes the new first element."

Your std::rotate middle points to "arr+4" i.e. the 5th element: 50.

enter image description here

You expect "50 40 30 20 10" i.e. to reverse the array; not to rotate it. So, you should use std::reverse:

#include <array>
#include <algorithm>
#include <iostream>

int main()
{
    std::array<int, 5> arr { 10,20,30,40,50 };

    std::reverse(arr.begin(), arr.end());

    for (auto i : arr)
        std::cout << i << "  ";

    return 0;
}
Amit G.
  • 2,546
  • 2
  • 22
  • 30
  • can u please tell me why did u take std::array arr{10,20,30,40,50,}; instead of vector in more details – aman_49 Aug 02 '19 at 09:30
  • @AlexStacy From [std::array](https://en.cppreference.com/w/cpp/container/array) reference: "std::array is a container that encapsulates **fixed size arrays**. ... The struct combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size, supporting assignment, random access iterators, etc." – Amit G. Aug 02 '19 at 09:45
  • can u please tell me difference between std::array and std::vector – aman_49 Aug 02 '19 at 09:55
  • @AlexStacy , you can read [here](https://stackoverflow.com/questions/4424579/stdvector-versus-stdarray-in-c). – Amit G. Aug 02 '19 at 10:02
0

This is not a good idea, but I tried to solve this with using rotate()...

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int main()
{
    vector<int> v{10,20,30,40,50};
    int i;

    for(i=0; i<5; ++i)
    {
       cout<<v[i]<<"  ";
    }
    cout << endl ;

     for(i=0; i<5; ++i)
     {
        rotate(v.rbegin(),v.rbegin()+1,v.rend());
        cout<<v[0]<<"  ";
     }
     cout << endl ;
}

g++ rotatev.cpp --std=c++11

./a.out
10  20  30  40  50  
50  40  30  20  10  
Junhee Shin
  • 748
  • 6
  • 8