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

Could you help me on the displayarray.c and sort.cpp. I don\'t know why the outp

ID: 3581050 • Letter: C

Question

Could you help me on the displayarray.c and sort.cpp. I don't know why the output turns wrong with many 0 next to it. And the sorted data was wrong as image below. And The sample run and some inputs for testing are also below. Here is my code

getdata.c

#include <stdio.h>

//Getdata
int getdata(double data[])
{
   int size = 0;
   double number;
   printf(" Enter doubles, one per line, and press Ctrl+D to terminate ");
while ((scanf("%lf",&number) != EOF))
{
   data[size] = number;
   size++;
}
   return size;
}

displayarray.c

#include <stdio.h>

//Display array
void displayarray(double data[], int size)
{
   for (int i = 0; i < size; i++)
   {
       printf(" %0.1lf", data[i]);
   }
}

fillpointerarray.cpp

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

void fillpointerarray(double data[], double *pdata[], int size)
{
   for(int i = 0; i < size; i++)
   {
   //*pdata[i] = data[i];
   //double *pdata = new double[i];
   pdata[i] = &data[i];
   }
}

showpointers.c

#include <stdio.h>

//show pointers
void showpointer(double *pdata[], int size)
{
   for (int i = 0; i < size; i++)
   {
   printf(" %0.1lf", *pdata[i]);
   }
}

sortbypointers.cpp

#include <iostream>
#include <iomanip>
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; j < 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 << setprecision(1) << fixed << *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;
}

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.

Testing

Test the script (or makefile)

First delete all existing object file and executable files.

Second enter ls -l to show the contents of the directory.

Third run the script fie (or makefile)

Fourth enter ls -l to show the contents of the directory.

Run 1

8.7

0.0

-4.0

Run 2

100.4

0.0

5.9

-60.0

Run 3 //eight adjacent zeros

900000000.94

900000000.39

900000000.73

900000000.25

900000000.13

900000000.27

Run 4 //only one number with 12 adjacent zeros

88.000000000000.8

//Be sure to format your output to show the entire number.

Run 5 //all negative numbers with 10 adjacent zeros

-1.000000000037

-1.000000000022

-1.000000000089

-1.000000000043

-1.000000000002

-1.000000000010

-1.000000000091

-1.000000000013

-1.000000000031

-1.000000000021

-1.000000000099

End of testing

Enter doubles, one per line, and press ctrl+D to terminate 2.22 2.20 3.456 There values were saved in the data array: 2.220000 2.200000 3.456000 The pointer array will be populated next These are the data pointed to by the pointer arrray. 2.2 2.2 3.5 The data have been successfully sorted using the technique of sort by pointer Here are the sorted data:2.0 2.2 3.5

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int main()

{
int i, j, n, tmp, *ptr;
        printf("Enter number of inputs:");
scanf("%d", &n);

        ptr = (int *)malloc(sizeof(int) * n); // dynamic allocation of the memory

        printf("Enter your inputs: ");
for (i = 0; i < n; i++)

       {
scanf("%d", ptr + i);
}

        for (i = 0; i < n - 1; i++)                           // sorting the array

       {
for (j = i + 1; j < n; j++)

                {
if (*(ptr + i) > *(ptr + j))

                        {
tmp = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = tmp;
}
}
}

         printf("Output: ");
for (i = 0; i < n; i++)

        {
printf("%d ", *(ptr + i));                  // Printing the sorted array
        }
printf(" ");

        free(ptr);                     // Releasing the the memory allocated.
        return 0;

}

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