CSPP 50101 - Lab 9

Lab 9 is due: Saturday, Aug 27 at 11:59pm

In today's lab:

Here are some resources on the C Standard Library that you can use with today's lab: Files for Lab 9 can be found at: /stage/classes/archive/2011/summer/50101-1/lab/lab9/src

Sorting with stdlib.h: qsort()

The C Standard Library includes the function qsort(), which sorts objects of some arbitrary type using a comparison function provided by the programmer. So you don't need to implement a sorting algorithm to use qsort(); you only need to provide a function that determines if one item should precede or follow another in a sorted list.

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;
}

Exercise 1

You can use qsort_demo.c as a staring point for this exercise. Submit your work in a new file, ex1.c.

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
1868
should be sorted as follows:
290
5242
3434
8955
736
1868
5448
3239
To 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.

Time and Dates: time.h

The C Standard Library includes datatypes and functions for the processing of date and time values. To complete Exercises 2 and 3, refer to the references provided at the start of this lab for functions and datatypes in the time.h header file.

Exercise 2

Submit your work for this exercise in file ex2.c.

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 
234
Your 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

Exercise 3

In a new program, ex3.c, make the following modifications to your solution for Exercise 2: Program output should look something like this:
$ ./ex3 2011 8 25 18 15 0
Input: Thu Aug 25 18:15:00 2011
236

Submitting your assignment

Lab 9 is due: Saturday, Aug 27 at 11:59pm

Submit these files for Lab 9: ex1.c, ex2.c, and ex3.c

Also include a simple README file in your lab9 directory with your name, username, and any other comments or questions for the Lab TA.

You will submit this lab with the hwsubmit program.

 
$ hwsubmit cspp50101lab <path to your lab9 directory>

Submit to cspp50101lab, NOT cspp50101.


prepared by Sonjia Waxmonsky