95.4k views
1 vote
Write a program to swap the 1 " integer element of an array with the last. Program takes in user input to populate array

1 Answer

3 votes

Final answer:

To swap the first and last element of an array in a high school level programming assignment, one can use a tuple assignment in Python after prompting the user to populate the array.

Step-by-step explanation:

The question pertains to programming and involves writing a code to swap the first integer element of an array with the last. This task is usually taught in a high school computer science class. The code takes in user input to populate the array before proceeding with the swap operation.

To achieve this in Python, for example, one could use the following code:

array = []
n = int(input('Enter number of elements: '))
for i in range(n):
element = int(input('Enter array element: '))
array.append(element)

# Swapping first and last element
array[0], array[-1] = array[-1], array[0]

print('Array after swapping:', array)

Here, the program first creates an empty list called array. It then asks the user for the number of elements and each element to populate the array. Finally, it swaps the first and last elements using a tuple assignment, and prints the modified array.

User Serhii Mamontov
by
7.4k points