Question: C++ Modify so that it creates an array of pointers that point to the values in the original array. Bubble sort the pointers by the

C++

Modify so that it creates an array of pointers that point to the values in the original array. Bubble sort the pointers by the value they point to instead of sorting the array of doubles itself. Print out the values in sorted order (using the pointer array), then reprint the array in its' original order again (using the original array).

#include

using namespace std;

const int ARR_SIZE = 10;

void bSort(double ar[]);

int main()

{

double nums[ARR_SIZE];

for(int i =0; i < ARR_SIZE; i++)

{

cout << "Enter value " << i+1 << " of " << ARR_SIZE << " to be sorted: ";

cin >> nums[i];

cout << endl;

}

cout << endl << endl << "Sorting values." << endl << endl;

bSort(nums);

cout << "Sorted values are: ";

for(int i = 0; i < ARR_SIZE; i++)

{

cout << endl << nums[i];

}

cout << endl << endl;

return 0;

}

void bSort(double ar[]) //bubble sort

{

double temp = 0;

bool flag = false;

do

{

flag = false; //lower flag at start of new pass through array

for(int i = 0; i < ARR_SIZE - 1; i++) //for each pair of elements in array

{

if(ar[i] < ar[i+1]) //if out of order (NOT including equal!)

{

//swap

temp = ar[i];

ar[i] = ar[i+1];

ar[i+1] = temp;

//raise flag

flag = true;

} //close if

} //close for

}while(flag == true); //close do-while (repeat if flag is raised)

}//close function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!