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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started