CSC161 2011S Imperative Problem Solving

Assignment 8: A Sorting Utility

Assigned: Friday, 22 April 2011
Due: 11:00 p.m., Wednesday, 27 April 2011

This assignment is also available in PDF.

Summary: In this assignment, you will develop a sorting utility similar to the standard Unix sort utility.

Purposes: To give you more experience with strings, function pointers, memory allocation, and more.

Expected Time: Three to four hours.

Collaboration: I encourage you to work in groups of two or three students. However, you may work on your own or in groups of up to size four. You may discuss the assignment with anyone you wish, provided you clearly document such discussions.

Submitting: Post to Pioneer Web a tarball of your important files (your .c files, your .h files, your Makefile, a typescript of a sample session, a README and anything else you deem appropriate).

Warning: So that this assignment is a learning experience for everyone, I may spend class time publicly critiquing your work.

Sorting

Write a Linux utility, srt, that sorts lines of text.

If srt is called with no command-line arguments, it should read its input from standard input, sort the lines alphabetically, and print the output to standard output.

If srt has -n as a command-line argument, it should treat each line as a number.

If srt has -r as a command-line argument, it should reverse sort, from largest to smallest. (When sorting alphabetically, largest means alphabetically last.)

If srt has -u as a command-line argument, it should unsort the values, putting them in random order.

If srt has -wnum as a command-line argument, it should support lines of up to num characters. If the -w command-line argument is not given, you can use 256. (This command-line argument is intended to make it easier for you to know how much space to allocate for the buffer you use for gets or other string input routine.)

If srt has -lnum as a command-line argument, it should support up to num lines. If the -l command-line arguemnt is not given, you can use 1024. (This command-line argument is intended to make it easier to know how big to make your array of strigs and in other places in which you may need to reserve space for a string.)

If srt is called with -knum as a command-line argument, it should break up the input at the separator character (a space, by default) and sort using the numth column.

If srt is called with -tchar as a command-line argument, it should use char as the separator. (This option is pointless when used without -k.)

Anything else on the command line should be treated as a file name. No more than one file name should be permitted. If no file name appears, you should read from standard input.

Examples

srt
Sort lines read from standard input.
srt students
Alphabetically sort the lines in the file students.
srt students -r
Alphabetically sort the lines in the file students in reverse order.
srt -n grades
Sort the lines of grades numerically.
srt -w511 courses
Sort the lines of courses, allowing up to 511 characters per entry.
srt -t, -k2 gradebook -n -r
Given a grade book of the form last,first,num by grade from largest to smallest.
srt -l10000 -k1 students
Given a list of no more than 10,000 students of the form last first middle, sort alphabetically by first name.
srt -n students grades
An erroneous command because two file names are given.
srt -u students
Sort the lines of students in random order.

 

History

Wednesday, 10 November 2010 [Samuel A. Rebelsky]

  • Designed.

Thursday, 11 November 2010 [Samuel A. Rebelsky]

Friday, 22 April 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Apr 22 08:12:08 2011.
The source to the document was last modified on Fri Apr 22 08:12:06 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Assignments/assignment.08.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu