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
Law Incorporation [45]
3 years ago
8

Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10,

20, 30, 40} becomes {40, 20, 30, 10)
#include
/* Your solution goes here
*/ int main(void) {
const int SORT_ARR_SIZE = 4;
int sortArray[SORT_ARR_SIZE];
int i;
int userNum;
for (i = 0; i < SORT_ARR_SIZE; ++i) {
scanf("%d", &sortArray[i]);
}
SwapArrayEnds(sortArray, SORT_ARR_SIZE);
for (i = 0; i < SORT_ARR_SIZE; ++i) {
printf("%d ", sortArray[i]);
}

Computers and Technology
1 answer:
sergey [27]3 years ago
4 0

Answer:

Here is the SwapArrayEnds()

//function with two parameters; the array and array size

void SwapArrayEnds(int sortArray[],int SORT_ARR_SIZE){  

int temp;   //temporary variable to hold the first element of sortArray

if(SORT_ARR_SIZE > 1){   //if sortArray size is greater than 1

temp = sortArray[0];   // set value of temp to the first element of sortArrray

sortArray[0] = sortArray[SORT_ARR_SIZE-1];   // set the value of first element of sortArray to the last element of sortArray

sortArray[SORT_ARR_SIZE-1] = temp;  }  } // set last element  value to temp

Explanation:

Lets understand the working of the above function with the help of an example. Suppose the sortArray has the following elements: {10,20,30,40}. So the size of this array is 4.

if(SORT_ARR_SIZE > 1) has an if statement which checks if the array size is greater than 1. This condition evaluates to true as 4 > 1. So the body of if condition executes.

Next the statement temp = sortArray[0];   sets the value of temp variable to the value of first element of sortArray. As sortArray[0] means the element at 0-th index position of the sortArray. So using the above example the first element of the array is 10. So temp = 10.

Next the statement sortArray[0] = sortArray[SORT_ARR_SIZE-1];   sets the last element of the sortArray to the position of first element of the sortArray.

SORT_ARR_SIZE-1 is the position of last element of the array. As the size of sortArray is 4, so 4-1 = 3 which points to the last element. Now this element is moved to the first position of the array. This means that the value of first element of array becomes 40 as the element at 3-th index of the sortArray is 40.

Next the statement sortArray[SORT_ARR_SIZE-1] = temp moves the value in temp variable to the last position of the sortArray. As we know that the value of temp= 10. So this element 10 of the sortArray is positioned at SORT_ARR_SIZE-1 position of sortArray which is 4-1 = 3-th index of sortArray. This simply means that 10 is moved to the last position of sortArray.

So now the final sortArray becomes: 40,20,30,10 as the first and last element of the array are swapped using above function SwapArrayEnds().

The program along with the output is attached in screenshot.

You might be interested in
. Which of the following are container elements?
lions [1.4K]
It’s c cba to explain
4 0
2 years ago
Read 2 more answers
You can perform numbers___on binary
Masja [62]

Answer:

c) arithmetic operations

3 0
3 years ago
Read 2 more answers
The calculation of GDP is the sum of: Consumption, Investment, Government Spending, and Net Exports. What is the simple formula
12345 [234]
The correct answer is B
3 0
3 years ago
Which of the following statements is false? Question 22 options: a) Companies that use Software-as-a-Service are running applica
gavmur [86]

Answer:

A is your answer I think

6 0
3 years ago
Which type of business is best for Juanita to start? a corporation, because she needs a large investment to get started a sole p
Vedmedyk [2.9K]

A sole proprietorship, because she will work alone from home a franchise

7 0
3 years ago
Read 2 more answers
Other questions:
  • Why are using some special characters (@, #, !, etc.) not a good idea?
    13·2 answers
  • Which of the following function headings arevalid? If they are invalid, explain why.
    12·1 answer
  • Frank develops a questionnaire for his study on Internet dating. One of his questions asks, "How do you feel about Internet dati
    14·2 answers
  • Whoever understands this first and replies will be the brainliest.<br><br><br><br> Road work ahead?
    9·2 answers
  • A large institution, such as a bank, may have thousands of transactions to process in which no user interaction is required; whi
    5·2 answers
  • Consider a system consisting of m resources of the same type, being shared by n processes. Resources can be requested and releas
    13·1 answer
  • Port explorers ​are tools used by both attackers and defenders to identify (or fingerprint) the computers that are active on a
    6·1 answer
  • define the term computer hardware and its various types mentioning 5 examples of IP or devices with one diagram each​
    9·1 answer
  • Explain why agile methods may not work well in organizations that have teams with a wide range of skills and abilities and well-
    7·1 answer
  • Drag each tile to the correct box.
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!