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

Credit:
25 points.

Reading

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

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. (5 points) In CS1 you probably learned about sorting algorithms and implemented one or more of them.1A simple way to test such an algorithm is to generate a sequence of “random” numbers, sort them, and check that the result is in ascending order. Sample program sorter-start.c shows how this might be done in C, all but the actual sorting. For this problem your mission is just to fill in code for the sort function so that it actually sorts. It's completely up to you which sorting algorithm to implement, though I'm inclined to recommend that you just do one of the simple-but-slow ones (e.g., bubble sort or selection sort). If you feel ambitious, you could try quicksort or mergesort, though mergesort is apt to be more trouble since it requires a work array. (If you're tempted to look up and use a library function: Don't; that misses the point of this assignment, and you won't get full credit.) Note that when you get your sort working you should edit the comments at the start of the program appropriately!

  2. (10 points) For this problem your mission is to further revise the sort program from the previous problem so that rather than generating random data it reads the values to sort from a file and writes the sorted values to another file. The completed program should take two command-line arguments giving the names of the input and output files. (It should not prompt the user for anything.) The program should print appropriate error messages if not enough arguments are supplied, if it cannot open the input and output files, or if the input file contains anything but a sequence of integers. Since we have not yet talked about how to make arrays larger at runtime, just write the program with a fixed-size array for holding input, and have the program print an error message if the number of input values exceeds the size of the array. It's up to you whether you keep the part of the existing program that checks whether the sort succeeds (I say “might as well”); if you do, just have it print to standard output as before.

    Hints:

  3. (10 points) A very simple way to encrypt text is to rotate each alphabetic character N positions. For example, if N is 1, “abc XYZ 1234” becomes “bcd YZA 1234”. (This is obviously not industrial-strength encryption but is good enough to somewhat obscure the plaintext.) Write a C program that implements this scheme. The program should take three command-line arguments: the number of positions to rotate (which for simplicity should be a positive integer), the name of the input file, and the name of the output file. It should print error messages as appropriate (not enough command-line arguments, non-numeric N, input or output file cannot be opened). For valid arguments, it should encrypt the input file and write the result to the output file.

    There are probably several ways you could approach encoding each character. To get full credit, your program must encrypt using the following approach, rather than other ways you may have seen for doing this sort of thing:

    First look up the character in a string representing the alphabet, then convert it also using the alphabet string. Starter code for such a scheme, to encode int variable inchar, is available here.

    (I ask you to do this partly for more practice working with strings and pointers, but also because it doesn't rely on characters being encoded in ASCII (which on most systems these days they are, but C doesn't require it).)

    Hints:

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

... them.1
If you didn't take CS1 and don't know about sorting, ask me about supplemental reading.



2021-09-29