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
________is one color shade gradually progressing to another shade of the same color or one color progressing to another color.
posledela

Answer:

Gradient fill

Explanation:

A gradient fill is a visual effect that makes a three dimensional color look by mixing one color shade gradually progressing to another shade of the same color or one color progressing to another color. for example a dark red progressively changing to light red or red progressively changing to blue.

8 0
3 years ago
Which of the statements below is false? Question 19 options: a) You can install a 64-bit operating system on a 64-bit machine. b
diamong [38]

you can install a 64 bit operating system on a 32 bit machine.

hope this helps!

5 0
3 years ago
In your own words, what does Hypertext Markup Language [HTML] do?
nexus9112 [7]

Answer:

HTML dictates how the browser presents images and text on a webpage.

Explanation:

3 0
3 years ago
Read 2 more answers
Which of the following translates packets so that the node can understand them once they enter through a port?
podryga [215]
Hi
D)A NICHappy to assist you!
8 0
3 years ago
Read 2 more answers
What did Muhammad do when he encountered opposition from Mecca’s city leaders? He brought more followers to Mecca. He relocated
Rudiy27

Answer:

He relocated from Mecca to the city of Medina.

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Given the commands below, what do they do? for (position = 1 through aList.getLength()/2) { a = aList.getEntry(aList.getLength()
    11·1 answer
  • How does a spreadsheet affect a column of numerals when it sorts the column
    15·2 answers
  • Não basta tornar-se móvel hoje em dia. Ao se organizar para a implantação de tecnologias no contexto da mobilidade, é preciso fi
    6·1 answer
  • Write function d2x() that takes as input a nonnegative integer n (in the standard decimal representation) and an integer x betwe
    14·1 answer
  • I can talk to you! How about we talk through the you know where people comment and say stuff about the question1
    10·2 answers
  • How is the architecture converted into software code? Elaborate the steps its passes through with help of examples / Diagram.
    15·1 answer
  • What is the difference between algorithm and program?
    10·1 answer
  • Why might you use the More button in the Find and Replace dialog box?
    10·1 answer
  • The quickest way to change a word is to double
    11·1 answer
  • Which word describes an important characteristic of good computer
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!