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

C++ This assignment will consist of writing several functions that manipulate ar

ID: 3734508 • Letter: C

Question

C++ This assignment will consist of writing several functions that manipulate arrays or access data from arrays, as well as a test program that will allow interactive testing of the array functions

Sort

Write a function called Sort that takes in three parameters:

an integer array

the size of the array

This function should sort the array either ascending or descending, your choice. This function does not return a value. This function should actually sort the array itself, not just print out the contents in order. Sample call:

Explanation / Answer

#include <iostream>

using namespace std;

void sort(int array[], int n, int isAscending) {

int temp;

for (int i = 0; i < n; i++) {

int j = 1;

while (j < (n - i)) {

if(isAscending ) {

if (array[j - 1] > array[j]) {

// swap the elements!

temp = array[j - 1];

array[j - 1] = array[j];

array[j] = temp;

}

} else {

if (array[j - 1] < array[j]) {

// swap the elements!

temp = array[j - 1];

array[j - 1] = array[j];

array[j] = temp;

}

  

}

j++;

}

}

}

int main()

{

int list[5] = {4,3,7,1,2};

cout<<"Array elements before elements: "<<endl;

for(int i=0;i<5;i++) {

cout<<list[i]<<" ";

}

cout<<endl;

sort(list, 5, true);

cout<<"Array elements after sorting in ascending: "<<endl;

for(int i=0;i<5;i++) {

cout<<list[i]<<" ";

}

cout<<endl;

sort(list, 5, false);

cout<<"Array elements after sorting in descending: "<<endl;

for(int i=0;i<5;i++) {

cout<<list[i]<<" ";

}

cout<<endl;

return 0;

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote