For this assignment you must must turn in individual work. It is okay to work with another student to plan your solution, but your source code must be entirely your own creation. |
-bash-4.2$ ./words enter letters: atebp atebp: ape apt enter letters: moo moo: enter letters: appble appble: able ape apple enter letters: exit -bash-4.2$
words.c
and we suggest you create a directory named ~/cs16/p3
to store it in.
Type your name and the date in a comment at the start of the file.~kyledewey/cs16/p3/words
exit
.
words
above, “atebp” and “moo” are groups of letters.)
~kyledewey/cs16/p3/words_skeleton.c
as a base to develop your program.
Note that the dictionary specified in this file must be identical to the dictionary turned in.
You may add or remove words from this dictionary as you see fit (adjusting the DICT_SIZE
constant as necessary) while testing, but the code turned it must have this exact same dictionary.array[ character - ‘a’ ]
goto
.
goto
can lead to ugly code fast, it is very rarely used in practice, and it generally not well-received by other programmers.
You may be interested in reading one of the original writings advocating against goto
, written a whopping 44 years ago.cd
to the
same directory as your source code file, then type the following:turnin p3@cs16 words.c
A series of bonuses are available with this project. In total, your final grade in the whole class can be boosted by as much as 6%. However, you may find these bonuses to be quite challenging. They are comprehensive of the entire course. Note that if you do choose to persue the bonuses, be advised that you must do them in sequence. That is, you cannot get credit for a later bonus without doing all the earlier ones. To get credit for bonus #2, you must have completed bonus #1, to get credit for bonus #3 you must have completed bonuses #1 and #2, and so on. A description of the bonuses follows.
~kyledewey/cs16/p3/dictionary.txt
.
You may make the following assumptions about the dictionary file:
Failed to open dictionary file "dictionary.txt"
”, followed by a newline, and then exit the program.
If you aim for this bonus (and nothing beyond it), you should save your code as words1.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words1
turnin p3@cs16 words1.c
words2.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words2
turnin p3@cs16 words2.c
words3.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words3
turnin p3@cs16 words3.c
~kyledewey/cs16/p3/input.txt
.
You may continue to assume that each group of letters will not exceed 99 letters.
If you cannot open the input file, you should print “Failed to open input file "input.txt"
”, followed by a newline, and then exit the program.
If you aim for this bonus (and nothing beyond it), you should save your code as words4.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words4
turnin p3@cs16 words4.c
~kyledewey/cs16/p3/output.txt
.
If you cannot open the output file, you should print “Failed to open output file "output.txt"
”, followed by a newline, and then exit the program.
If you aim for this bonus (and nothing beyond it), you should save your code as words5.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words5
turnin p3@cs16 words5.c
./words6 -d dictionary_file.txt -i input_file.txt -o output_file.txt
, where
-d
means that the next argument should refer to a dictionary file name,
-i
means that the next argument should refer to an input file name, and
-o
means that the next argument should refer to an output file name.
-
) does not matter.
In other words, both
./words6 -o output_file.txt -d dictionary_file.txt -i input_file.txt
and
./words6 -i input_file.txt -o output_file.txt -d dictionary_file.txt
Failed to open dictionary file "badDictionary.txt"
”.)
exit( int )
fuction in stdlib.h
; see this link if you are interested.)
The conditions under which an error occurs along with what should be printed for each error are below:
./words6 -i input_file.txt -o output_file.txt
, then the default is to try to use “dictionary.txt” as a dictionary file name.
./words6 -d dictionary.txt -o output_file.txt
, then the default is to ask for input from the user interactively (as with the original words
program).
./words6 -d dictionary.txt -i input_file.txt
, then the default is to print out the matching words to the terminal (as with the original words
program).
./words6 -d -i input_file.txt
, then it should print “Expected a dictionary, but received "‹‹whatever switch was received here››"
”. For this particular command line, the appropriate thing to print would be “Expected a dictionary, but received "-i"
”.
./words6 -i -d dictionary_file.txt
, then it should print “Expected an input file, but received "‹‹whatever switch was received here››"
”. For this particular command line, the appropriate thing to print would be “Expected an input file, but received "-d"
”.
./words6 -o -d dictionary_file.txt
, then it should print “Expected an output file, but received "‹‹whatever switch was received here››"
”. For this particular command line, the appropriate thing to print would be “Expected an output file, but received "-d"
”.
./words6 -i -o -d
, then only the first skip should be printed. For example, with this particular command line, the appropriate thing to print would be “Expected an input file, but received "-o"
”.
./words6 -i input_file.txt -d
, then it should print “Expected a dictionary, but it is missing
”.
./words6 -d dictionary_file.txt -i
, then it should print “Expected an input file, but it is missing
”.
./words6 -i input_file.txt -o
, then it should print “Expected an output file, but it is missing
”.
./words6 -d dictionary_file1.txt -d dictionary_file2.txt
, then it should print “Attempted to pass multiple dictionary files
”.
./words6 -i input_file1.txt -i input_file2.txt
, then it should print “Attempted to pass multiple input files
”.
./words6 -o output_file1.txt -o output_file2.txt
, then it should print “Attempted to pass multiple output files
”.
./words6 -i input_file foobar
, then it should print “Unrecognized argument: "‹‹whatever argument was not recognized››"
”. For this particular command line, the appropriate thing to print would be “Unrecognized argument: "foobar"
”.
./words6 -i input_file1.txt -d -i input_file2.txt -o
, the error reported should be “Expected a dictionary, but received "-i"
”, since that is the first error encountered going left to right across the different arguments.
words6.c
.
You can run this particular solution from your CSIL account as follows:
~kyledewey/cs16/p3/words6
turnin p3@cs16 words6.c
To get a better idea of how challenging each bonus may be (and whether or not a persuing a particular bonus would be worth the effort), you may be interested in some summary statistics of my own code as more bonuses were added, along with my impression of the difficulty / hints:
Version | Number of lines (including whitespace) | Number of functions | Dificulty / hints (Purely subjective) |
---|---|---|---|
Original (no bonuses) | 70 | 5 | Less tricky than Project #2 |
With bonus #1 | 96 | 7 | A bit annoying, especially with reading in dictionary words. Somewhat surprisingly, malloc |
With bonuses #1 - #2 | 109 | 7 | No, now reading in dictionary words is annoying. realloc was needed. |
With bonuses #1 - #3 | 110 | 7 | Same process as in bonus #2, so not much tricker than that. |
With bonuses #1 - #4 | 135 | 10 | A bit of extra work to open the file, but existing code was able to be adapated without much change. |
With bonuses #1 - #5 | 149 | 12 | Very similar to bonus #4. The adaption was almost identical. |
With bonuses #1 - #6 | 276 | 19 | Ouch. Once you see a way to handle the ordering portion it becomes straightforward, but lengthy. |