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

Need to create 3 functions + 1 main function Good Bye World Function Write a fun

ID: 3548676 • Letter: N

Question

Need to create 3 functions + 1 main function
  • Good Bye World Function
    • Write a function that prints "Good bye world! " that can be passed to the atexit function
    • The function pointer parameter of atexit has no parameters and returns nothing.
  • Int Compare Functions
    • Write two functions that compares ints and can be passed to the qsort function
    • Function 1
      • This function should return < 0 if the first int is smaller, > 0 if the first int is larger, or 0 if the two ints are the same.
    • Function 2
      • This function should return > 0 if the first int is smaller, < 0 if the first int is larger, or 0 if the two ints are the same.
      • This is the opposite of the first function.
    • The function pointer parameter for qsort takes two void pointers (pointers to the two values to be compared) and returns an int.
  • Driver (main function)
    1. Create a function pointer variable that can point to your Int Compare functions.  Do not initialize it yet.
    2. Create the following int array
      • int myArray[] = {92, -3, 1, 0, 9, -39, 1};
    3. Initialize the created function pointer and make it point to the first Int Compare function.
    4. Call atexit with your Good Bye World function.
    5. Print the elements of myArray.  They should be unsorted.
    6. Call qsort on myArray with the function pointer variable created in step 1.
    7. Print the elements of myArray.  They should be sorted now.
    8. Set the function pointer to point to the second Int Compare function.
    9. Repeat step 6.
    10. Print the elements of myArray.  They should be sorted in a different order now.  W
    11. Return from main.  It should print "Good Bye World! ".
Need to create 3 functions + 1 main function

Explanation / Answer

#include<stdio.h>


void quicksort(int (*abc)(void *,void *),int * x,int l,int h)

{

int q;

if(l>=h)

return;

q=partition(abc,x,l,h);

quicksort(abc,x,l,q-1);

quicksort(abc,x,q+1,h);

}


//abc is a function pointer that points to a function that accepts two void pointers as arguments and returns an integer

int partition(int (*abc)(void *,void *),int * x,int l,int h)

{

int a=x[l],i=l,temp,j;

for(j=l+1;j<=h;j++)

{

if((*abc)(&x[j],&a)==-1 || (*abc)(&x[j],&a)==0)

{

i=i+1;

temp=x[i];

x[i]=x[j];

x[j]=temp;

}

}

temp=x[l];

x[l]=x[i];

x[i]=temp;

return i;

}


//abcd is a function pointer that points to a function that accepts no argument and returns nothing

void at_exit(void (*abcd)())

{

(*abcd)();

}


void Good_Bye_World()

{

printf("Good bye world! ");

}


int compare1(void * ptr1,void * ptr2)

{

int n1 = *(int *)ptr1;

int n2 = *(int *)ptr2;

if(n1<n2)

return -1;

else if(n1>n2)

return 1;

else if(n1==n2)

return 0;

}


int compare2(void * ptr1,void * ptr2)

{

int n1 = *(int *)ptr1;

int n2 = *(int *)ptr2;

if(n1>n2)

return -1;

else if(n1<n2)

return 1;

else if(n1==n2)

return 0;

}




int main()

{

int i,size;

int (*functionPtr)(void *,void *);

int myArray[] = {92, -3, 1, 0, 9, -39, 1};

functionPtr = &compare1;


at_exit(Good_Bye_World);


size = sizeof(myArray)/sizeof(myArray[0]);


printf(" UnSorted Array : ");

for(i=0;i<size;i++)

printf("%d ",myArray[i]);


quicksort(functionPtr,myArray,0,size-1);


printf(" Sorted Array : ");

for(i=0;i<size;i++)

printf("%d ",myArray[i]);


functionPtr = &compare2;


quicksort(functionPtr,myArray,0,size-1);


printf(" Reverse Sorted Array : ");

for(i=0;i<size;i++)

printf("%d ",myArray[i]);


at_exit(Good_Bye_World);


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