Here are the prototypes void fillpointerarraydouble double
Here are the prototypes:
void fillpointerarray(double[], double* [], int );
void sortby pointers(double*[],int);
void showdataviapaointers(doulbe*[],int);
Specifications for fillpointerarray
This function is compiled with the C++ compiler. The function receives two arrays which we call the data array and the pointer array. Additionally, the function receives an integer. For each valid member of the data array this function will assign a pointer to the corresponding cell of the pointer array. For example, if the kth member of the data array is data[k] then the kth member of the pointer array will point to data[k]. This function does not output anything to the monitor nor to any file nor to any device. There are requirements: there are no work-arounds.
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.
Specifications for showdatabypointers
This function is compiled by the C compiler. The function receives the pointer array and an integer. For each element in the pointer array the function outputs the data value pointed to by that element. This function outputs one data value per line. This function does not output any text or headers or messages or anything else.
Sample run
This is a requirement; this shows how your output must appear.
Welcome to Sort by Pointers programmed by <your name>
Enter doubles, one per line, and press CNTL and D to terminate.
16.8
3.6
-4.5
10.2
0.0
6.3
4.1
Cntl and D were entered here; nothing appears as output.
7 doubles were inputted.
There values were saved in the data array:
16.8
3.6
-4.5
10.2
0.0
6.3
4.1
The pointer array will be populated next.
These are the data pointed to by the pointer arrray.:
16.8
3.6
-4.5
10.2
0.0
6.3
4.1
The data have been successfully sorted using the technique of sort by pointer.
Here are the sorted data:
-4.5
0.0
3.6
4.1
6.3
10.2
16.8
Here is the original unchanged data array:
16.8
3.6
-4.5
10.2
0.0
6.3
4.1
The main function will now terminate. Have a nice evening.
Get data: Main: C Display array Fillpointerarra Sortbypointers: Showdataviapointers. C++Solution
#include <stdio.h>
/* function declaration */
//double getAverage(int *arr, int size);
void fillpointerarray(double[], double* [], int );
void sortbypointers(double*[],int);
void showdataviapaointers(double[],int);
const int MAX = 5;
int main ()
{
//varible declarations
double data[MAX];
double original[MAX] ;
double *ptr[MAX],*pt[MAX];
int i;
//reading input MAX number of double values
printf(\"Enter %d doubles, one per line\ \",MAX);
for ( i = 0; i < MAX; i++) {
scanf(\"%lf\",&data[i]);
original[i]=data[i];
}
printf(\"There values were saved in the data array:\ \");
for ( i = 0; i < MAX; i++)
{
printf(\"%lf\ \",data[i]);
}
//function calls
fillpointerarray(data,ptr,MAX);
sortbypointers(ptr,MAX);
showdataviapaointers(original,MAX);
return 0;
}
//function defiantion to define pointer array with data array
void fillpointerarray(double data[] , double* ptr[], int x )
{
int i;
printf(\"The pointer array will be populated next.\ These are the data pointed to by the pointer arrray.:\ \");
for ( i = 0; i < x; i++)
{
ptr[i] = &data[i]; /* assign the address of integer. */
printf(\"%lf\ \",*ptr[i]);
}
}
//function to sort pointer array values
void sortbypointers(double* ptr[],int x)
{ int i,j;
double *a;
for (i = 0; i < x; ++i)
{
for (j = i + 1; j < x; ++j)
{
if (*ptr[i] > *ptr[j])
{
a = ptr[i];
ptr[i] = ptr[j];
ptr[j] = a;
}
}
}
printf(\"The data have been successfully sorted using the technique of sort by pointer.\ Here are the sorted data:\ \");
for ( i = 0; i < x; i++)
{
printf(\"%lf\ \",*ptr[i]);
}
}
//function to display original values
void showdataviapaointers(double ptr[],int x)
{
int i;
printf(\"Here is the original unchanged data array:\ \");
for ( i = 0; i < x; i++)
{
printf(\"%lf\ \",ptr[i]);
}
}
Output :
Enter 5 doubles, one per line
4.5
9.2
-3.5
1.0
4.2
There values were saved in the data array:
4.500000
9.200000
-3.500000
1.000000
4.200000
The pointer array will be populated next.
These are the data pointed to by the pointer arrray.:
4.500000
9.200000
-3.500000
1.000000
4.200000
The data have been successfully sorted using the technique of sort by pointer.
Here are the sorted data:
-3.500000
1.000000
4.200000
4.500000
9.200000
Here is the original unchanged data array:
4.500000
9.200000
-3.500000
1.000000
4.200000