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:
-
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.
-
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.
-
Use a for loop to fill the array by asking the user to enter the values, 1 at a time (1 per line).
-
Ask the user if they want to sort in ascending or descending order.
(Note: User can enter 1 for ascending. 2 for descending)
-
Use the BubbleSort() function implemented above to sort accordingly.
-
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started