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
klio [65]
3 years ago
15

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}. the array's size may differ from 4.
Computers and Technology
1 answer:
Alexus [3.1K]3 years ago
3 0
In the C programming language, you can't determine the array size from the parameter, so you have to pass it in as an extra parameter. The solution could be:

#include <stdio.h>

void swaparrayends(int arr[], int nrElements)
{
int temp = arr[0];
arr[0] = arr[nrElements - 1];
arr[nrElements - 1] = temp;
}


void main()
{
int i;
int myArray[] = { 1,2,3,4,5 };
int nrElements = sizeof(myArray) / sizeof(myArray[0]);
swaparrayends(myArray, nrElements);

for (i = 0; i < nrElements; i++)
{
printf("%d ", myArray[i]);
}

getchar();
}

In higher languages like C# it becomes much simpler:

        static void Main(string[] args)
        {
            int[] myArray = {1, 2, 3, 4, 5};
            swaparrayends(myArray);
            foreach (var el in myArray)
            {
                Console.Write(el + " ");
            }

            Console.ReadLine();
        }

        static void swaparrayends(int[] arr)
        {
            int temp = arr[0];
            arr[0] = arr.Last();
            arr[arr.Length - 1] = temp;
        }

You might be interested in
To change lowercase letters to uppercase letters in a smaller font size, which of the following should be done?
sergeinik [125]

Explanation:

Format the text in Small caps. Manually replace the lowercase letters with uppercase letters.

7 0
3 years ago
Comment if u wanna text me on behance
kow [346]

Answer:

what is behance? :)

Explanation:

4 0
3 years ago
What animal is perry the platypus ​
valkas [14]

Answer: Agent P or simply Perry

Explanation:

4 0
3 years ago
Read 2 more answers
Which of the following tasks requires you to use the Tabs option?
Monica [59]
Indenting a paragraph :)
7 0
3 years ago
When code is compiled it
lesantik [10]

Answer:

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file. In short, it's A or D.

5 0
3 years ago
Read 2 more answers
Other questions:
  • Software that helps run the computer's hardware devices and coordinates instructions between applications is called
    10·1 answer
  • Users can make notes or highlight areas of a website and then use the ________ feature of Microsoft Edge to send the highlighted
    13·1 answer
  • Does learning swift is hard? more than android?
    13·1 answer
  • Traffic that is encrypted will typically pass by an intrusion prevention system untouched.a. Trueb. False
    10·1 answer
  • Refer to the exhibit. If a hacker on the outside network sends an IP packet with source address 172.30.1.50, destination address
    11·1 answer
  • What type of engineer is interested in designing, developing, and building different machines, devices, and tools? A.aerospace
    8·2 answers
  • Which term describes a visual object such as a picture, a table, or a text box?
    14·2 answers
  • Please tell fast plzzzzzzz​
    11·1 answer
  • Which line correctly starts the definition of a class named "team"?
    7·1 answer
  • The price of an item you want to buy is given in dollars and cents. You pay for it in cash by giving the clerk d dollars and c c
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!