Assignment 2

CS 265/571 Advanced Programming Tools and Techniques
Instructors: Jeremy Johnson and Kurt Schmidt
Due date: Friday Feb. 11 at 11:55 pm

Introduction

The purpose of this assignment is for students to become familiar with compiling, running, and testing programs in the Unix environment, using library routines, and the concept of generic programming.  Students will be introduced to the languages of the course, C, C++, Java, and Perl, and will have a chance to review a fundamental algorithm, namely quicksort.  Most of the coding for the assignment will simply involve writing main programs and either using library routines or code from class. See Lecture 4 for relevent material and code.

The assignment involves sorting strings.  A main program will be written that reads strings, one per line, from standard input, and writes the same strings, in sorted order, to standard output.  This interface is the same as the Unix sort filter, except that only one string per line is allowed and the options that sort supports are not included.

Sorting is a very common operation and is widely supported through standard library routines for different languages.  Rather than reinventing the wheel, when sorting is required it is a good idea to use a library routine.  A good library routine must provide a good interface (see chapter 4 of the text) and should support many different situations (i.e. different types of elements that are to be sorted).  Algorithms that support different data types are called generic and programming to support generic algorithms is called generic programming.  Different languages have different types of support for generic programming (e.g. void* and function pointers in C, templates and virtual methods in C++, interfaces and the Object class in Java).  As part of this assignment you will investigate support for generic programming in different languages.  While it is generally not a good idea to implement your own sorting routine, it is good to be aware of the algorithms that are available, be able to read code implementing the algorithm, and to be aware of the behavior and runtime of important algorithms (this will be pursued later in the assignment on performance).

Problems

For each problem, you are to write a separate main program.  All main programs should follow the same interface as indicated in the introduction.

  1. Write and test a main program in C, that reads strings (one per line) from standard input and writes the same strings in sorted order to standard out, one string per line.  Use an array of pointers to character arrays to store the strings. I.e., you have a statically-allocated array of pointers, char *[LINE_CAP] = 0; (you may assume you won't have more than 1,000,000 lines to parse, see below). You will then use malloc or strdup to get memory for each line, as needed. There are examples in your book. Don't forget to give the memory back, using free (not delete).

    You should use the quicksort code from class to sort the strings.  This code was modified to sort an array of character arrays (see quicksort.c).  See the lecture notes on C for how to read/write strings.

  2. Redo question 1 using the C library routine qsort.
  3. Redo question 1 in C++, modifying the provided quicksort code, to use a template.  Use a vector of strings to store the strings.
  4. Redo question 3 using the C++ sort routine, found in the <algorithm> header file.
  5. Write and test a main program in java, that reads strings (one per line) from standard input and writes the same strings in sorted order to standard output.  Use a vector of strings (Vector to store the strings and use the sort library routine (java.util.Collections.sort) to sort the strings.  See the lecture notes on java for how to read/write strings.
  6. Write and test a main program in perl, that reads strings (one per line) from standard input and writes the same strings in sorted order to standard out.  Use a list of strings to store the strings and the sort operator to sort the strings.  See the lecture notes on perl for an example on how to read from standard input in perl.

Description of input

You may expect no more than 1,000,000 lines of input. You may further assume that each line has no more than 80 characters (careful here). Finally, strings (lines) will have no whitespace.


Description of output

You will output the sorted list of strings, one per line, to stdout.


What to hand in

Place your solution in a directory called A2. Your directory should contain a file called README that describes all of the files in A2 and summarizes what you did. Make sure to indicate if any of the problems were not done or are not working.

For each problem, create a subdirectory called probi, where i is the problem number.  In each directory create a file called main.ext, where ext is the appropriate extension for the language being used (c, cpp, java, pl), with your main program.  Put a brief comment in each main program file indicating the author, what the program does, the date and platform info, and how to use the program.  You should also create a makefile that builds the executable program (see here for some help).  The executable program should be called sorti, where i is the problem number. The Java program will be called sort.class after it is compiled. The makefile from problem 6 will simply copy main.pl to sort6.pl, and give it execute permission.

Make sure you test each program.  Include the file containing sample strings that you used to test your program. It will be in the parent directory, and called testInput .

Create a gzipped tar file containing a directory called A2 (use the command tar to do this) with your solutions.   If you created a directory called A2 with all of the files for your assignment 2 solution, you can use the command:

$ tar -zcvf A2.tar.gz A2

to create the desired gzipped tar file (the convention is to use the .tar extension for tar files and the .gz extension for gzipped files).  See man tar of info tar for more information about tar.

All assignments should be submitted using webct. You should submit your gzipped tar file.