CPSC 225: Spring 2003
Assignment 4: Decoding with files and arrays


FOR THIS ASSIGNMENT, you will be decoding a coded message. The program will use two files and one or two arrays. The files that you will need are /home/cs225/message.dat and /home/cs225/code.dat. You should copy these into your own account. The first file, message.dat, contains a message in coded form. The second file, code.dat, contains the code that was used to encode the message. Note that your program must work not just with these particular files, but with any files that meet the description given below.

Your program should use functions to break up the problem into subtasks in a reasonable way. For this assignment, there is an additional requirement that you should not use any global variables. All communication among functions should be done by passing parameters or by using return values.

Turn in a printout of your program in class on Monday, February 17. For this assignment, you can get any kind of help anywhere you like, and you can work together with other people in the class. However, your grade will be based only partly on the program that you turn in. The major part of the grade will be based on a ten-to-fifteen minute individual discussion of the program with me. I will ask everyone to make an appointment for this discussion on Tuesday, February 18. During the discussion, you should be able to explain the design of the program and the algorithms that it uses and to answer detailed questions about how it works. You might be asked to produce similar code segments on the board. The point is to demonstrate that you have a full understanding of the work that you turn in.


In this assignment, each character in an original message has been replaced by a string of characters in the coded version of the message. In the specific data that I have prepared, the code that is used is based on Morse code, using "." and "-" characters instead of the "dits" and "dots" of Morse code. For example, an A is encoded by the string ".-", and a comma is encoded as "--..--" However, your program should be able to work for other, similar codes.

The code is specified in the file named code.dat. Each line of this file contains a character, followed by a blank space, followed by the string that is used as the code for that character. Your program should begin by reading the code from this file and storing it. (Note that one of the characters is itself a space, which makes reading the file several times harder than it would be otherwise.) The code data can be stored in two arrays -- one to hold the characters and one to hold the corresponding code strings. Alternatively, if you prefer, you can store the data in a single array of structs, where each struct holds a character and its code string. Your can assume that there are no more than 100 characters and that the code strings do not contain spaces.

The message is stored in the file named message.dat. It consists of a sequence of code strings, separated by spaces and ends-of-line. You should read the strings from this file, find the decoded character corresponding to each string, and output the decoded message. You can write the message to cout. The words in the decoded message should be arranged in a paragraph. No line should be longer than 70 characters. Any line break should come at the end of a word, not between words. If there is any string in the file that is not a legitimate code, your program should indicate that fact in some way.


David Eck