โ Back to Arrays
Question 114
Reverse Array In Place
Reverse Array In Place
An array called nums has already been created for you with the values {10, 20, 30, 40, 50}.
Your task is to reverse the array in place (without creating a second array), then print all elements on one line separated by spaces.
To reverse an array in place, you swap elements from the outside in:
- Swap nums[0] with nums[4]
- Swap nums[1] with nums[3]
- The middle element (nums[2]) stays where it is
You only need to loop through the first half of the array. For each index i, swap nums[i] with nums[4 - i].
Expected output:
50 40 30 20 10 Expected Output:
50 40 30 20 10
Topics:
Arrays
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.