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

Specifications for sortbypointers This function is compiled by the C++ compiler.

ID: 3576838 • Letter: S

Question

Specifications for sortbypointers

This function is compiled by the C++ compiler. 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 for sort by pointers

#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;
   }
}

Main.c

#include <stdio.h>

int getdata(double[]);
void displayarray(double[], int);
void fillpointerarray(double[], double*[], int);
void showpointer(double*[], int);
void sortbypointers(double*[], int);

int main()
{
double data[10];
double *pdata[10];
printf(" Welcome to Sort by Pointers programmed by Chloe Ho. ");
   int n = getdata(data);


   //display array
   printf(" There values were saved in the data array: ");
   displayarray(data,n);

   //fill pointer array
   printf(" The pointer array will be populated next. ");

   printf("These are the data pointed to by the pointer arrray.: ");

fillpointerarray(data, pdata, n);
showpointer(pdata, n);

//sort by pointers
   printf(" The data have been successfully sorted using the technique of sort by pointer.");
   printf(" Here are the sorted data:");

   sortbypointers(pdata, n);
  

printf(" The main function will now terminate. Have a nice evening. ");
return 0;
}

I don't know what's going on with the sort. When I compile it, it appeared:
Segmentation fault (core dumped) and stopped at the sort.

Get data: Main: C Display array Fillpointerarra Sortbypointers: Showdataviapointers. C++

Explanation / Answer


Segmentation fault (core dumped) is a specific kind of error caused when memory which you are trying to access is not available or does not belong to you.When you are getting this error you are doing something wrong with the memory for example you are trying to access the variable that has already been freed or you are trying to write to a read-only portion of the memory.

Here, in this case try to pass the parameters properly and you should get the proper output for sure.

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