I'm trying to write element of array in reverse order and I came across this example
template <class T>
void reverse(T arr[], int size)
{
if (size>=2)
{
swap(arr[0],arr[size-1]);
reverse(arr+1,size-2);
}
}
I don't get the second line - why are they subtracting the size of the array by 2? If I have 10 elements in the "swap" function by subtracting it by 1 to swap the first element with the last element then subtracting it again by 2 would give me 8 but putting that size again in the "swap" function I would get 7!! shouldn't it be 8 instead of 7?
sizeis 10,size - 2is 8.size - 1doesn't modifysize.