In today's lab:
Here are some resources on the C Standard Library that you can use with today's lab:qsort() is useful because you may want to vary the way that you sort a particular datatype depending on the needs of your program. For example, you may want to sort numeric data in ascending or descending order, or to sort strings in a case-sensitive or case-insensitive manner. With qsort(), you can make this change simply by updating the comparison function used to order data in the input array.
You can read more about qsort() in Chapter 16 of Prata.
Program qsort_demo.c in the Lab 9 directory is a simple program that sorts a list of random numbers by calling qsort() from main():
qsort(rand_vals, LEN, sizeof(int), intcmp);This call to qsort() sorts rand_vals, an array of LEN elements, each of size "sizeof(int)". The last argument to qsort() is a pointer to the comparison function intcmp() that returns -1, 0, or 1 to indicate that the value at the first argument is less than, equal to, or greater than the value at the second argument.
int intcmp(const void * px, const void * py) { int x_val = *(int*)px; int y_val = *(int*)py; if (x_val < y_val) return -1; else if (x_val==y_val) return 0; else return 1; }
Modify the the program qsort_demo.c so that integers are sorted by their rightmost "ones" digit. For example "251" would precede "42", since the rightmost digit "1" is smaller than "2". If two integers have the same ones digit then the smaller one precedes the larger one in the sorted output.
As an example, this list of numbers:
736 3239 8955 3434 290 5448 5242 1868should be sorted as follows:
290 5242 3434 8955 736 1868 5448 3239To solve this problem, implement a new function called intcmp_ones() that returns -1,0, or 1 using the ordering described above. (Hint: Use the '%' operator.) Then, update the call to qsort() in main() so that intcmp_ones() is used instead of intcmp() to compare integers.
Write a simple program that allows the user to enter a date at the command line and then computes the number of days since January 1 of the year in which that date occurs. So January 5, 2011 would show a result of 4, since this date is 4 days after the first of the year.
Dates are entered in Year Month Day format, where months are entered as integers 1(January) through 12(December).
Program output should look something like this:
$ ./ex2 2011 1 1 0 $ ./ex2 2011 1 5 4 $ ./ex2 2011 8 23 234Your program should properly handle leap years, i.e. March 1, 2011 is 59 days after January 1, but March 1, 2012 is 60 days after January 1 since 2012 is a leap year:
$ ./ex2 2011 3 1 59 $ ./ex2 2012 3 1 60
You should not need to do any complicated arithmetic to solve this problem, and in fact you can find the solution rather quickly by reading the latest draft of the C99 Standard. In particular you will want to review these sections:
Section 7.23.2.3 of this document includes an example of a simple program that calculates the day of the week of July 4th for a given year. The Lab 9 directory includes an implementation of this program, july4.c, that you can use as a starting point for this exercise. This program takes a year as a command line argument:
$ ./july4 2011 Monday $ ./july4 2012 Wednesday
$ ./ex3 2011 8 25 18 15 0 Input: Thu Aug 25 18:15:00 2011 236