Lab 6: Insertion Sort Revisited

Due: Wednesday, Oct. 8, 2014 @ 11:59 p.m.

Description:

In this lab you will modify your InsertionSort code from Lab 3 to accept an unknown number of integers from standard input (stdin) and sort them. Although you will not know the number of integers for each run, you can expect the count to be a value between 10 and 200 (inclusive). Thus, you will need to reserve space in your array for 200 integers. You will keep reading data from stdin until EOF is encountered, or 200 integers have been read. Once the integers have been read, you will sort the list using the algorithm you wrote for Lab 3.

Background Preparation:

  1. Review the example code given on Blackboard for command line parameters  (Course Content > Prof. Otten's Course Folder > Sample Code > Command Line Args > Command_Args.c)
  2. Read some web pages that discuss Unix Pipes and Command Line Redirection:
    1. http://www.westwind.com/reference/os-x/commandline/pipes.html or
    2. http://www.tutorialspoint.com/unix/unix-io-redirections.htm

Specifications:
You will create two programs for this lab. The first, InsertionSort, is your program from Lab 3 that you will modify to read an unknown number of integers until EOF is read. It will then sort the list of input numbers. To test your program you will create a second program, GenNums, that will generate a random number n between 10 and 200 (inclusive). It will then generate n random integers in the range 0..1000 (inclusive) and output them to standard output (stdout). To guarantee that your code has a different seed every time, GenNums will take an integer argument (representing the seed) on the command line: 

bash$ ./GenNums 54321

To do this, you must have parameters in your main() function to handle the command line values:

int main(int argc, char **argv)

The argc parameter contains the number of command line arguments, including the name of the executable. So for the command shown above, argc will contain the value 2. The argv parameter is an array of cstrings, one for each argument. So for the above example, argv[0] will be "./GenNums" and argv[1] will be "54321"

Inside the program you will use sscanf to read the argument into a seed variable:

sscanf(argv[1], ”%ld”, &seed);

Your modified InsertionSort program will read the output of your GenNums program. Note that this can be done in two ways. The first method uses the Unix redirection operators (> and <) to direct the output of GenNums:

bash$ ./GenNums 54321 > myout
bash$ ./InsertionSort < myout

In the example above, the first command puts the output of GenNums into a file named myout. The second command takes the data in the file myout and uses it as data to be input into InsertionSort.

The second method uses the pipe operator ( | ):

bash$ ./GenNums 54321 | ./InsertionSort

In this example, the output of GenNums is used directly as input to (it is "piped" into) InsertionSort.

You will create a Makefile that will compile and link both programs. 

Submission:

Log in to mason or zeus and copy your lab's files into a directory named Lab6. Start a typescript session in the directory above Lab6. Type "uname -a" to show that you are on mason or zeus. Change to the Lab6 directory and use the ls and cat commands to list all your source files and Makefile. Compile your programs using the make command. Run your programs to show that they execute correctly. 

To get full credit for this lab you must generate five different sets of random numbers using your GenNums program and sort them with InsertionSort. You should also run GetNums independently of the sorting runs. You must also show that your program works with both of the methods (redirection and pipes) given above.

Change back to the directory immediately above Lab6, and use the tar command to create an archive of all the files in the Lab6 directory. Name this archive Lab6.tar. Re-read the man page if necessary to discover the proper options to use.

Finally, exit the typescript session by typing crtl-d.

Verify that your script file was created correctly by using the more (or less) command (your choice) to see the contents of the file. As always, information on these commands can be obtained by using the manual pages ("man more" or "man less"). Once you are sure the file is correct, copy the script file and Lab6.tar back to your local computer and submit both of them to Blackboard as Lab 6.