1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vladimir2022 [97]
3 years ago
5

Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} b

ecomes {40, 20, 30, 10}.

Computers and Technology
2 answers:
grandymaker [24]3 years ago
8 0

Answer:

Python method swapArrayEnds()

def swapArrayEnds(array):      

   size = len(array)  

   temp = array[0]  

   array[0] = array[size - 1]  

   array[size - 1] = temp  

   return array

         

Explanation:

You can call the method in main program to swap the first and last elements of sortArray as following:

def swapArrayEnds(array):

   size = len(array)  

   temp = array[0]  

   array[0] = array[size - 1]  

   array[size - 1] = temp  

   return array  

     

sortArray = [10, 20, 30, 40]  

 

print(swapArrayEnds(sortArray))

This function works as following:

size stores the length of the array.

temp is a temporary variable that holds the first element of the array

The it swaps the first element of the array with the last element using this statement: array[0] = array[size - 1]  where size-1 contains the last element.

It finally stores the last element of the array in temp and returns the array after this swapping is done.

Then in the main program an array sortArray is given the values 10,20,30,40 and then the method swapArrayEnds()  is called to swap the first and last elements of sortArray.

The complete program with the output is attached in a screenshot.

dybincka [34]3 years ago
4 0

Answer:

The solution code is written in Java.

  1.    public static void swapArrayEnds(int myArray[]){
  2.        int lastIndex = myArray.length-1;
  3.        int temp = myArray[0];
  4.        myArray[0] = myArray[lastIndex];
  5.        myArray[lastIndex ] = temp;
  6.    }

Explanation:

First create the swapArrayEngs method that take one input array parameter (Line 1).

Since we need to swap the first and last element of the array, we need to get the first index and last index of the array. The first index is 0 and the last index can be calculated by subtracting the length of array from 1 (Line 2).

Next, we can create a temp variable to hold the value of the first element (Line 3). Then we use the lastIndex the get the value of last element and assign it to the first element of array (Line 4). Lastly, we assign the temp (holding the  initial first element value) to the last element of array (Line 5).

 

You might be interested in
What does cpu stand for
lozanna [386]
Central processing unit.
7 0
3 years ago
Read 2 more answers
In a five-choice multiple-choice test, which letter is most often the correct
tatiyna

I believe the answer is C.

5 0
3 years ago
Read 2 more answers
100 POINTS
agasfer [191]

Answer:

i'm just a kid ku ku ku ku ku ku ku ku ku ku ku ku

6 0
3 years ago
How can you tell if your car is overheating?
Nitella [24]
A) your temp gauge is moving into red
4 0
3 years ago
Read 2 more answers
Google is an example of a(n):
Andre45 [30]
Well i see your X so i would say you were correct it is a search engine.
7 0
3 years ago
Other questions:
  • End-user development: the process by which an organization's non-it specialists create software applications.
    8·1 answer
  • What is the IEEE standard for the Wi-Fi Protected Access 2 (WPA2) security protocol?
    6·1 answer
  • In addition to the ping command, what other command is useful in displaying network delay and breaks in the path to the destinat
    5·2 answers
  • If the Account Number field in a record always should display the three characters in the account number in uppercase, then the
    14·1 answer
  • Downloading files is safe for most types of files except Select one: a. documents. b. pictures. c. executable files. d. music fi
    7·1 answer
  • How can i turn on my prinfer without getting up
    12·2 answers
  • What is a third variable condition that could create the following correlation?
    10·2 answers
  • Write a C program to calculate and display the coordinates of midpoint - M of a linesegment between two given points - say A and
    7·1 answer
  • What are some of the opportunities that the stem program offers?
    15·1 answer
  • How do I add a Child to my Brainly account
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!