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
3. Write a program that prompts the user to input an integer that represents cents. The program will then calculate the smallest
miss Akunina [59]

Answer:

The program in Python is as follows:

cents = int(input("Cents: "))

qtr = int(cents/25)

cents = cents -qtr * 25

nkl = int(cents/5)

pny = cents -nkl * 5

print(qtr,"quarters,",nkl,"nickels,",pny,"pennies")

Explanation:

This gets input for cents

cents = int(input("Cents: "))

This calculates the quarters in cents

qtr = int(cents/25)

This gets the remaining cents

cents = cents -qtr * 25

This calculates the nickel in remaining cents

nkl = int(cents/5)

This calculates the pennies in remaining cents

pny = cents -nkl * 5

This prints the required output

print(qtr,"quarters,",nkl,"nickels,",pny,"pennies")

6 0
3 years ago
What would be the output of the following program?
Natalka [10]

Answer:

Output: 2004 2008 2058  

Explanation:

In the first printf command it will print the address of the variables num, msg1, msg2. And in the second printf command it will print the values of the variables num, msg1, msg2. As the address of the structure is 2004 And the size of the integer is 4 byte so size will increase with 4 bytes and the size of character is 1 byte so it will increase by 1*50= 50 bytes.

Hence, the output is 2004 2008 2058  

8 0
3 years ago
Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:
Lemur [1.5K]

Answer:

B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

Explanation:

To call a superclass constructor, the user must use super(). This is necessary unless default constructors are used. Also, it is vital to make sure if their are appropriate argument to be used while invoking the superclass constructor.

3 0
3 years ago
Face-to-face human contact has lost its importance as a means of communication because of the growing prevalence of e-mail, web-
chubhunter [2.5K]

Answer:

b

Explanation:

7 0
3 years ago
Which access database object is best to use for the basis of a report when specific criteria must be applied to the report?
Anna007 [38]
The answer is Query.

A query helps a user retrieve specific items from a MS Access database. If you have an item that matches with all the criteria you enter, it shows in the query results. A query is sent to the data source to retrieve the data.




8 0
3 years ago
Read 2 more answers
Other questions:
  • Read the spreadsheet formula below, then answer the question.
    14·2 answers
  • What tool do you use to secure remote access by users who utilize the internet??
    7·1 answer
  • 2. Student organizations sometimes require transportation for off-campus activities, and school policy requires students to be o
    11·1 answer
  • Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is
    9·1 answer
  • Which of the following is the Boolean logical operator for OR in C#?
    12·2 answers
  • Word Bank:
    13·1 answer
  • Explain the difference between file and folder (in your own words)
    6·1 answer
  • Whose task it is to ensure that the product flows logically from one step to another?
    14·1 answer
  • When a ____________ file is opened, it appears full-screen, in slideshow mode, rather than in edit mode.
    6·1 answer
  • PlanthelogicforLungi’sapplicationusingpseudocode.Thelogicneedstosatisfythefollowingneeds:TheapplicationwillneedtoallowLungitoen
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!