Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the sort procedure from Assignment 18 as a void function. Pass the add

ID: 3818745 • Letter: I

Question

Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function.


As in Assignment 18, print out the array before and after sorting. Use the same test data.


All output should be in main(), not in the function.

REFERENCE ONLY ASSIGNMENT 18:

Consider the following sort procedure written in pseudo code:

Exchange Sort of Array X[]

For I = 0 to (number of array elements - 2), increment by 1
For J = (I + 1) to (number of array elements - 1), increment by

If X[I] is greater than X[J] then

temp = X[I]

X[I] = X[J]

X[J] = temp

Next J

Next I

Implement the sort procedure in main() without using pointers. Declare the array as follows using the test data shown:

int x[] = { 28, 87, -3, 45, 19 };
Use a for loop to print out the array before sorting and again after sorting. Thereshould be no output within the sort procedure.

Explanation / Answer

#include <iostream>

using namespace std;


void Sort(int *x,int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++) //index starts from 0
    {
       for(j=i+1;j<n;j++)
       {

             if(*(x+i) > *(x+j))
             {

                      temp = *(x+i);

                     *(x+i) = *(x+j);

                      *(x+j) = temp;
             }

       }
    }
}

int main()
{

int x[] = { 28, 87, -3, 45, 19 };

int i;
for(i=0;i<5;i++)
{
    cout<<x[i]<<" ";
}
Sort(&x[0],5);//first array element

cout<<" After Sorting ";
for(i=0;i<5;i++)
{
    cout<<x[i]<<" ";
}

return 0;
}

output: