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

Could you show me how to sort by pointers in C++? This function receives exactly

ID: 3579482 • Letter: C

Question

Could you show me how to sort by pointers in C++?

This function receives exactly two parameters; the array of pointers and an integer. The programmer may select his or her favorite sort algorithm You, the programmer, must understand well your selected sort algorithm. Make this fundamental modification: “compare data items but swap pointers”. This function does not modify the data array. The data array is not passed to this function. This function does modify the pointer array. This function does not output any information: no text, no messages, no numbers.

Here is my code sortbypointers.cpp

#include <iostream>
using namespace std;
extern "C" void sortbypointers(double *pdata[], int size);

void sortbypointers(double *pdata[], int size)
{
   for (int i = 0; i < size; i++)
   {
       for (int j = i+1; i < size; j++)
           {
               if (!(*pdata[i] < *pdata[j]))
               {  
                   int temp;
                   temp = *pdata[j];
                   *pdata[j] = *pdata[i];
                   *pdata[i] = temp;
               }  
           }
   }
   for (int i = 0; i < size; i++)
   {
   cout << *pdata[i];
   cout << endl;
   }
}

I don't know what's wrong. Thanks

Explanation / Answer

#include <iostream> // header files
#include <iomanip>
using namespace std;
using std::setw;
void selectionSort( int * const, const int ); // prototype
void swap( int * const, int * const ); // prototype
int main(){ // main function
const int arraySize = 8; // const variable
int a[ arraySize ] = { 3, 0, -5, 9, 22, 1, 14, 7 }; // taking array as i/p
selectionSort( a, arraySize ); // sort the array
cout << "Integers in ascending order ";
for ( int j = 0; j < arraySize; j++ )
cout << setw( 4 ) << a[ j ];
cout << endl;
return 0; // indicates successful termination
} // end main
void selectionSort( int * const array, const int size ) // function to sort an array
{
int smallest; // index of smallest element
for ( int i = 0; i < size - 1; i++ ){
smallest = i; // first index of remaining array
for ( int index = i + 1; index < size; index++ ) // loop to find index of smallest element
if ( array[ index ] < array[ smallest ] )
smallest = index;
swap( &array[ i ], &array[ smallest ] );
}
}
void swap( int * const x, int * const y ){ // swap values at memory locations to which x and y point
int temp = *x;
*x = *y;
*y = temp;
}

OUTPUT:

Integers in ascending order

-5 0 13 7 9 14 22