Question: Reversing an array is a common task. Here we present a modified version of this problem. Given an array of even length reverse the second
Reversing an array is a common task. Here we present a modified version of this problem. Given an array of even length reverse the second half of the army. For example, if the given array is of length 8, then reverse the last 4 elements of the given array is of length 10, then reverse the last 5 elements Write a function that reads in an integer representing the length of the array on the first line. Then on the second line of input, read in the rest of the integers into an array of the given length with a space between each of the integers. Reverse the second half of the given array Print out the final modified array with a space after each element, as seen below If the input is 259186 Output is If the input is -342 12 13 1.45 2.0 Hints Use this approach Initialize two variables with the start and end index of the second half of the array, Swap the start and end elements, then increment start index and decrement the end index Stop when you reach the middle of the second half of the array, otherwise, you'll reverse the same pair of values twice. Think about the case when the number of elements in the second half of the array are even and when odd. Make sure your code handles both cases, (Think about the property of integer division)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
