Imagine that an array is Ice cube tray that you divided up into 5 slots .
int arr[] = {1, 2, 3, 4, 5};
It is up to you on what you fill in your ice cube tray and the number of slots you want in it.
Now Imagine that you indexed /numbered each slot with numbers 0,1,2,3 and 4 .
So now you have a Ice cube tray all divided and indexed and ready to be filled up.
Lets fill it up with chocolates in the shape of numbers
In slot 0 we place chocolate 1.
In slot 1 we place chocolate 2.
In slot 2 we place chocolate 3.
In slot 3 we place chocolate 4.
In slot 4 we place chocolate 5.
So you want to know which chocolate is first in the ice cube tray ?
That's easy because in an array the Index keeps track of what's first.
So Slot 0 is always first , Slot 1 is second and so on ...
Okay so far so good but now your friend comes over and decides that he wants to unpack all the ice cubes
you have in the tray and to repack them back but in descending order.
So the first thing your friend does is he first figures out which Ice cube tray (array) and how many ice cubes are in it (Size)
int size = sizeof(arr)/sizeof(arr[0]);
The main reason your friend wants to know the size of the array is to understand how many slots are in it aka whats the last slot since he already knows:
1) That the 1st slot is 0
2) and that all the other slots are in numerical order aka 0,1,2,3
So once he figures out the size of the array (ice cube tray) he can than attempt to unpack it and keep track of things during swap.
So in technical terms :
The line int arr[] = {1, 2, 3, 4, 5}; initializes an integer array arr with 5 elements containing the values 1, 2, 3, 4, 5.
The line int size = sizeof(arr)/sizeof(arr[0]); calculates the size of the array arr by dividing the total number of bytes occupied by the array (which is given by sizeof(arr)) by the number of bytes occupied by a single element of the array (which is given by sizeof(arr[0])).
Okay so now that you understand all that it's finally time to do the reverse .
Note now I am giving you an example for array arr of integers: {1, 2, 3, 4, 5} that is to be reversed.
First :
The function reverse_list takes an integer array arr and its size size as inputs
Than it declares two local variables temp and i.
Than it initializes the loop with i = 0 and continues until i < size/2.
Inside the loop, it stores the value of arr[i] in the temporary variable temp.
Now for the Actual Swap :
It assigns the value of arr[size-i-1] to arr[i] to swap the values.
It assigns the value of temp to arr[size-i-1] to complete the swap.
Finally The loop continues to the next iteration until it has swapped all the necessary elements.
So this is how it Works for the swap :
Iteration 1:
i=0
arr[i]=1
arr[size-1-i]=5
temp=1
arr[i]=5
arr[size-1-i]=1
Iteration 2:
i=1
arr[i]=2
arr[size-1-i]=4
temp=2
arr[i]=4
arr[size-1-i]=2
And so on ...
Explanation :
The loop starts with i=0.
The value of the current element arr[i] is 1.
The corresponding element to swap with is arr[size-1-i], which is arr[4] or 5.
The current element arr[i] is assigned the value of arr[4] or 5.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is 5.
The temporary variable temp is assigned the value of the original arr[i], which was 1.
The loop continues with i=1.
The value of the current element arr[i] is 2.
The corresponding element to swap with is arr[size-1-i], which is arr[3] or 4.
The current element arr[i] is assigned the value of arr[3] or 4.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is 4.
The temporary variable temp is assigned the value of the original arr[i], which was 2.
The loop continues with i=2.
The value of the current element arr[i] is 3.
The corresponding element to swap with is arr[size-1-i], which is also arr[2] or 3.
The current element arr[i] is assigned the value of arr[2] or 3.
The corresponding element arr[size-1-i] is assigned the value of arr[i], which is also arr[2] or 3.
The temporary variable temp is assigned the value of the original arr[i], which was 3.
The loop exits because i=3 and size/2=2.
After the loop, the array arr has been reversed in place, and its values are now {5, 4, 3, 2, 1}.