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

Respuesta :

Answer:

The C++ code is given below with appropriate comments

Explanation:

#include <iostream>

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;

   }

 

   double *pNums[ARR_SIZE];

 

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

      pNums[i] = &nums[i];

  }

 

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

 

   bSort(pNums);

 

   cout << "Sorted values are: ";

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

   {

       cout << endl << *pNums[i];

   }

 

  cout << "\n\nOriginal 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;

   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

ACCESS MORE