I have written a code to rotate an array of integers cyclically. For e.g. Given array - 1 2 3 4 5 6 7 8 Output array- 8 1 2 3 4 5 6 7
The problem what i am facing is, if i use auto keyword in some point then it gives weird result what is beyond my understanding. Would anyone please help me in summarizing the problem ?
#include<iostream>
#include<bits/stdc++.h>
#include <typeinfo>
using namespace std;
void rotate_one_by_one(array<int, 8> &arr)
{
auto temp = arr[arr.size() - 1];
//auto i = arr.size() - 2; // Output : 1 2 3 4 5 6 7 8
int i = arr.size() - 2; // Output : 8 1 2 3 4 5 6 7
for(; i > -1; --i)
{
arr[i+1] = arr[i];
}
arr.at(i+1) = temp;
}
void cyc_rotate(array<int, 8> &arr)
{
rotate_one_by_one(arr);
cout<<"After cyclic rotate\n";
for(auto n : arr)
cout<<n<<" ";
}
int main()
{
array<int, 8> arr = {1,2,3,4,5,6,7,8};
cyc_rotate(arr);
return 0;
}
Please find the commented line in the code where i have mentioned the output for using auto and int type. If anyone still not able to get what i am trying to convey then please let me know.