I simply could not understand why the following code does not work. What could be the possible reason that the swap operation does not work;
#include <iostream>
using namespace std;
void rotateK(int* arr, int start, int finish) {
int temp;
for(int i=0;i<=finish;i++) {
temp=arr[i];
arr[i]=arr[finish-i];
arr[finish-i]=temp;
}
for(int i=0;i<=finish;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main(){
int arr[5]={1,2,3,4,5};
rotateK(arr,0,4);
return 0;
}
It does work (although not how you want it to work). But swaps the elements twice, that’s why the processed array is identical to the original one.
You probably want:
or even
so that you actually use
start.