23.5k views
2 votes
Given the function definition below, what is the effect of thecall:

change(ar, 0, n-1);
where n is the size of array ar?
void change(int ar[], int low, inthigh)
{
inttemp;

if(low< high)
{
temp= ar[low];
ar[low]= ar[high];
ar[high]= temp;
change(ar,low + 1, high - 1);
}
}
A. Sort the first n elements of ar inascending order
B. Reverse the first n elements of ar
C. Switch the first and last elements ofar
D. Sort the first and last elements ofar
E. None of the above

2 Answers

2 votes

Answer:

The correct answer is E.

Step-by-step explanation:

User Sravanti
by
5.5k points
6 votes

Answer:

Hi!

The correct answer is E.

Step-by-step explanation:

void change(int ar[], int low, inthigh) {

int temp;

if(low< high) { // here ask if the positions low and high of the array are the same.

temp= ar[low]; // first, saves the element on ar[low] in temp.

ar[low]= ar[high]; // second, the element on ar[high] in ar[low]. First switch.

ar[high]= temp; // third, saves the element on temp in ar[high]. Complete switch.

change(ar,low + 1, high - 1); // Recursive call, adding one position to low, and subtracting one position to high. Important: When low and high have the same value, the recursive call will finish.

}

}

Result: Switch the lower half of elements in the array with the upper half.

User Steve Alexander
by
5.8k points