HOMEWORK 09 - READING AND WRITING WITH DATA FILES
Concepts
In this homework, we will continue to practice file I/O, data manipulation, and while loops.
Overview
For this assignment, your job is to find the secret message hidden within a large file of words. To do this, we need to find the words that correspond to specific values. That is, you will read a value, search for a word in your input file that has the same value, and then print that word to an output file. You will then read the next value, search for the next word in your input file that has that value, and then print that word to an output file.
The Specifics
Points
In order to calculate how much a given word is worth, you need to know how many points each character in the word is worth. Here's the breakdown.
- Anything that is not a letter = 0 points
- e, t, a, o, n = 10 points
- r, i, s, h, d = 20 points
- l, f, c, m, u = 30 points
- g, y, p, w, b = 40 points
- v, k, j, x, q = 50 points
- z = 60 points
File Format
Your input files are formatted with one value/word per line. Your list of words, found in words.txt
has one
string
,
all lower-case, per line. Your list of word values that you are searching for
in the encoded message, found in
points.txt
has one int
per line. You should place these input files at the project level, which should be the same
directory as your main.cpp
file. You should see both your input files AND
a .vcxproj file in the same directory.
Your output file, message.txt
should have spaces separating each word (no newlines!).
Example
Here's an example
Wordsin
you
are
tin
grade
awesome!
programming
is
pure
Points
80
40
130
Right off the bat you (as a person) know that there are 3 words in this secret message, because you are only given 3 point values. (Of course, the computer won't know there are 3 words until your program finishes executing.) One important thing to note is that you should not look at the top of your word list for each point value; in order words, each search will start where the previous search left off.
Let's run through this example:
- Retrieve the first point value (80)
- Start iterating through your list of words, from the top of your file, and calculate the points until you find one that is 80 points
- in = 'i' + 'n' = 20 + 10 = 30
- you = 'y' + 'o' + 'u' = 40 + 10 + 30 = 80
- Print "you " to your output file (i.e., "you" and a space " ")
- Retrieve the next point value (40)
- Continue iterating through your list of words, starting at "are", and calculate the points until you find one that is 40 points
- are = 'a' + 'r' + 'e' = 10 + 20 + 10 = 40
- Print "are " to your output file (i.e., "are" and a space " ")
- Retrieve the next point value (130)
- Continue iterating through your list of words, starting at "tin", and calculate the points until you find one that is 130 points
- tin = ... = 40
- grade = ... = 100
- awesome! = ... = 130
- Print "awesome! " to your output file (i.e., "awesome!" and a space " ")
Since there are no more point values to retrieve, your program is done (even though more words in your file exist). Your output file should read:
you are awesome!
Hints
- You will need nested loops to complete this assignment. Think carefully about which loop should be on the outside!
Submission
You need to submit your solution of this homework (HW09) with two lab assignments: Lab09A and Lab09B. Detailed generic instructions for submitting homework assignments are available. For homework due this Wednesday, follow these specific steps:
- create a directory called week09.
- within week09, create three subdirectories: Lab09A, Lab09B, and HW09.
- within your new week09/Lab09A directory, copy in your main.cpp file from your Lab09A solution.
- within your new week09/Lab09B directory, copy in your main.cpp file from your Lab09B solution.
- within your new week09/HW09 directory, copy in your main.cpp file from your HW09 solution.
- compress the week09 directory (see Step 3 here for details).
- submit the week09.zip file to Blackboard (see Steps 5-10 here for details).
- after you submit, download the file and double check it contains all that you think it contains!