In this assignment, you'll get some basic experience with pointers. Don't worry! I realize that many people found assignment 2 more difficult than the first assignment, so this one is short. (My solution is only 22 non-blank lines.)
You will write a program to read in an arbitrary number of integers, output them in sorted order, and then print the median value. Here's a sample execution:
How many values will you enter? 5 Enter value 1: 83 Enter value 2: 23 Enter value 3: 567 Enter value 4: 176 Enter value 5: 36 Sorted numbers: 23 36 83 176 567 Median is 83
This program will require the use of scanf() to read the numbers. After the user has given the number of values (5 in the above case), you should dynamically allocate an array exactly large enough to hold that many integers, and then read them in one-by-one.
Then you'll need to sort the integers. It's not hard! C has a function qsort() in <stdlib.h> that can sort arrays of arbitrary type. First you'll need to write a function that compares values in the arrays:
int compare(const int *a, const int *b);
Remember, the const type qualifiers mean that the function doesn't modify the values pointed to by a and b. The function compare() should compare the integers pointed to by a and b, and return -1, 0, or 1 depending on whether the first integer is less than, equal to, or greater than the second, respectively. Then you just call qsort() from main(); see K&R p. 253 for a description of the parameters. The fourth parameter should be written as
(int (*)(const void *, const void *)) compare
which casts compare to be a pointer to a function taking two pointers to const void and returning an int. You don't have to write any complicated type casts yourself in this program, but do try to puzzle this one out.
You should then have no trouble printing out the sorted numbers or the median value. Remember, the median value is the one in the middle of the sorted array, or the average of the two middle ones if the array has even length.
This assignment is due in printed form with sample output on Friday, February 12. Start early, and come to class or office hours with questions.