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

Write a C program with the following functions: sort: a void function with 2 arg

ID: 3845084 • Letter: W

Question

Write a C program with the following functions:
sort: a void function with 2 args:
an array of doubles, which I'll call a. an unsigned that is the number of elements in the array.sort may assume without checking that a has at least one element, and that none of the values inthe array is 0 (each value is either positive or negative).sort's job is to sort the array a so that All the positive numbers come before all the negative numbers All the positive numbers are in increasing order All the negative numbers are in (algebraically) increasing orderFor example, if the array held {1, -2, 3, -4, 5, -6} on entry, then it would hold {1, 3, 5, -6, -4, -2} onexit.sort does no I/O.
show: a void function with 2 args: an array of doubles, which show doesn't change, and the
number of elements in the array. show's job is to output the array values, separated by commas,without a comma before the first element or after the last element. e.g. 3, 5, -17, 0, 8show may assume without checking that the array has at least one element.


main: create an array of doubles, and somehow put values into it.
call show to display the array before sortingcall sort to sort the arraycall show to display the array after sortingFor this program, it's fine if you decide to use the qsort function, and fine if you decide not to.

Explanation / Answer

#include <stdio.h>
int noOfNegativeElements=0, i;

int compareFunction (const void * x, const void * y)
{
return ( *(double*)x - *(double*)y );
}

void sort(double *arr, int noOfElements)
{
qsort(arr, noOfElements, sizeof(double), compareFunction);
}

void show(double *arr, int noOfElements)
{
for(i=noOfNegativeElements;i<noOfElements;i++)
       printf("%g ", arr[i]);
   for(i=0; i<noOfNegativeElements; i++)
       printf("%g ", arr[i]);
}

int main(void) {
int noOfElements;
double arr[10003];
   scanf("%d", &noOfElements);
   for(i=0;i<noOfElements;i++)
       scanf("%lf", &arr[i]);
  
   noOfNegativeElements=noOfElements;
  
   show(arr, noOfElements);
printf(" ");
noOfNegativeElements=0;
   for(i=0;i<noOfElements;i++)
   {
       if(arr[i]<0)
       noOfNegativeElements++;
   }
   if(noOfNegativeElements==0)
   noOfNegativeElements=noOfElements;
   sort(arr, noOfElements);
   show(arr, noOfElements);

   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