Q:

Write a Ruby program to swap the first and last elements of a given array of integers, length will be at least 1

0

 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now