โ Back to Arrays
Question 109
Swap First and Last
Swap First and Last
An array called nums has already been created for you with the values {10, 20, 30, 40, 50}.
Your task is to swap the first element and the last element of the array, then print all elements on one line separated by spaces.
To swap two values, you need a temporary variable to hold one of them so it doesn't get overwritten. Here's the pattern:
1. Save the first element in a temp variable
2. Set the first element to the value of the last element
3. Set the last element to the value stored in temp
After swapping, the array should be {50, 20, 30, 40, 10}.
Expected output:
50 20 30 40 10 Expected Output:
50 20 30 40 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.