Write a Ruby program to swap the first and last elements of a given array of integers, length will be at least 1. Return the modified array.
def check_array(nums) temp = nums[0]; nums[0] = nums[nums.length-1]; nums[nums.length-1] = temp; return nums; end print check_array([1]),"\n" print check_array([1, 3]),"\n" print check_array([1, 3, 5]),"\n" print check_array([11, 3, 5, 21])
Output:
[1] [3, 1] [5, 3, 1] [21, 3, 5, 11]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer