Question: In C program. 1. Using the code below, implement the descending function: /* bubble sort takes in a compare function pointer to call the correct

In C program. 1. Using the code below, implement the descending function:

/* bubble sort takes in a compare function pointer to call the correct function depending on sort type */

void bubbleSort( int n[], int size, int (*compare) (int a, int b))

{ int pass, i, temp; for( pass = 1; pass < size; pass++ ) { 

for( i = 0 ; i < size - 1 ; i++ ) { if ((*compare)( n[i] , n[i+1]) ) { //If not in order, swap

 temp = n[i]; n[i] = n[i+1]; n[i+1] = temp; 

} }

} }

/* returns 1 if not in ascending order */ int ascending (int a, int b){ } return (a > b) ? 1 : 0; 

/* returns 1 if not in descending order */ int descending (int a, int b){ //TODO: add implementation here!

}

2. You are writing a basic program to work with survey data gathered from some field work.

Some of the data lists have 10 values others have 1,000,000 values. Write a COMPLETE PROGRAM that does the following:

  1. Ask the user for the number of data points to work with (you can assume all data points are of type double). Saves that number in an integer variable iSize.

  2. Use malloc() to dynamically allocates a block of memory (an array) large enough to fit the exact number of data points. Make sure the allocation was successful by checking malloc()s return pointer for NULL.

  3. Use a for loop to fill the array by asking the user to enter the values, 1 at a time (1 per line).

  4. Ask the user if they want to sort in ascending or descending order.

    (Note: User can enter 1 for ascending. 2 for descending)

  5. Use the BubbleSort() function implemented above to sort accordingly.

  6. Display the sorted list.

NOTE: Make sure to unallocated the memory (- return it-), using free()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!