CSCI 1120 (Low-Level Computing), Fall 2021:
Homework 7

Credit:
20 points.

Reading

Be sure you have read, or at least skimmed, the assigned readings for classes through 10/13.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) one of two ways:

You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (20 points) For this assignment your mission is to write a C program that sorts the lines in a text file (considering each line as a string) using the library function qsort. So for example if the file contains lines "hello world", "abcdef", "the end", and "aaaa", the program should print
    aaaa
    abcdef
    hello world
    the end

    The program should take the name of the file to sort as a command-line argument (and print appropriate error messages if none is given or the one given cannot be opened) and write the result of the sort to standard output.

    To do this, I think you will need to read the whole file into memory. There are various ways to do this and perform the sort; the one I want you to use is somewhat involved but intended to give you more practice working with pointers. To get full credit you must use the approach described here.

    You can check your program's output by using the sort command to sort the input file and comparing its result (captured with I/O redirection!) with your result (also captured with I/O redirection). Traditionally sort just sorted based on what strcmp() returns for the lines in the file, but depending on configuration it may instead do a comparison that is case-insensitive and ignores leading whitespace. For this problem I just want you to do the simple comparison. You can get the sort command to do that by overriding the default configuration, thus:

    LC_COLLATE=C sort filetosort

    (Remember that one way to view differences between text files -- including of course program source -- is with vimdiff. I like to use vimdiff -o.)

Essay and pledge

Include with your assignment the following information.

For programming assignments, please put it a separate file. (I strongly prefer plain text, but if you insist you can put it in a PDF -- just no word-processor documents or Google Drive links please.) For written assignments, please put it in your main document.

Pledge

This should include the Honor Code pledge, or just the word “pledged”, plus at least one of the following about collaboration and help (as many as apply). Text in italics is explanatory or something for you to fill in; you don't need to repeat it!

Essay

This should be a brief essay (a sentence or two is fine, though you can write as much as you like) telling me what if anything you think you learned from the assignment, and what if anything you found interesting, difficult, or otherwise noteworthy.



Footnotes

... footnote1
printf with a %s conversion specification prints a string, which is assumed to end with the null character ('\0'). It's happy to print strings containing any number of newlines.



2021-10-13