Lab 10: Letter Frequency Console
Concepts
Now that you have some experience with many fundamental programming tasks, it's time to test your mettle. Use file I/O, functions, arrays, and loops together in one assignment to create a program that displays a summary of data.
Program from what You Know
Did you know that most programs aren't well-designed? They really aren't. Often, they're put together by geographically diverse people with different styles, knowledge, programming ability and design taste. But, the programs work. It's a freakin' miracle, but they do work.
When given a task in your engineering field that could be solved with software, we do not expect you to be expert programmers (unless your field is software engineering). However, we do expect you to be able to assemble the things you know how to code into cohesive, meaningful, useful programs.
You know how to open a file and read data from it. You know how to declare and work with arrays. You know that C++ can be your friend or your enemy. You know how to use a framework to draw some simple graphics. You know how to conduct repetitive tasks with loops. You know about datatypes.
Try to rely on these things that you know, before over-complicating your own solution with things that you don't yet know. This doesn't mean you shouldn't explore and learn more. You should! Always! Until your last dying breath! But, start with what you know first, and build from there.
Once you have reviewed the assignment requirements below, you should begin to think about how the requirements fit into discrete, specific tasks that you know how to accomplish. If you are unsure about how to approach a particular problem or requirement, ask!
Instructions
Write a program that prints the frequencies of letters that occur in the full text of Alice in Wonderland. When your program runs, it should look like this:
b: 1745
c: 3003
d: 5469
e: 15394
f: 2382
g: 2944
h: 7889
i: 8635
j: 236
k: 1290
l: 5212
m: 2466
n: 8053
o: 9479
p: 1968
q: 220
r: 6611
s: 7269
t: 12201
u: 3979
v: 963
w: 2951
x: 176
y: 2585
z: 80
Most frequent letter: e
Your program should:
- Open the file called
alice.txt
(in 22_letterFrequencyConsole folder). - Count the occurances of alpha characters, and store the frequencies in an array. (Treat lower and upper case letters the same. In other words, an occurance of 'a' or 'A' are equivalent.)
- Display a list frequencies, as shown above.
- Display the most frequent letter, as shown above.
Hints
This assigment isn't difficult if you start early, ask questions, and do things one step at a time.
Create a mental plan of what tasks your program needs to accomplish. Tackle the first task (eg, "can i open the file ok?") and conduct a sanity check. Then tackle the next task (eg, "can I read all the letters in the file, and store the frequencies of each letter in an array?") and conduct another sanity check.
Do not just dive into the assignment and try to implement the functions described in testApp.cpp. Create a mental plan of what tasks your program needs to accomplish. Tackle the first task (eg, "can i open the file ok?") and conduct a sanity check. Then tackle the next task (eg, "can I read all the letters in the file, and store the frequencies of each letter in an array?") and conduct another sanity check.
We suggest writing your program (one step at a time!) and getting it to work first, and then consult the rubric requirements. Don't let the rubric requirments lead your solution -- you'll likely get confused. First, get your program working, then use the rubric requirements as hints for improving your program.
There is nothing wrong with doing all of your steps inside main at first and then refactoring your work into functions later, once your program is working.
Use the value "alice.txt" for the filename.
Notice that you should use an array to store letter frequencies, indexed from 0 - 25, with the frequencies of 'a' and 'A' stored in frequencies[0] (and so on). There is an elegant way to increment the occurance of a particular letter in the array. What is 'a' minus 97? What is 'b' minus 97? Why subtract 97? (see the ascii table)
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Aha, another full-fledged program. Can you defeat me? I anxiously *bzzzt* await your solutions.
I will check for a list of frequencies in the form "L: N" where L is a letter and N is a number. (similar to the example console output above)
I will check for the sentence "Most frequent letter: L" where L is the most frequent letter in alice.txt.
Anything else your program "says" is up to you.
This work is worth 150 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 3 | |
Correct submission of src directory as a .zip file. | 2 | |
Demonstrates decent style (whitespace, etc) | 15 | |
Sufficient use of constants | 10 | |
Uses a function to populate frequencies in array | 15 | |
Properly checks input file for error | 10 | |
Properly closes input file | 10 | |
Uses a function to determine the maximum frequency | 15 | |
Correctly determines array index of the maximum frequency | 10 | |
Uses a function to print the letter frequencies | 15 | |
Correctly prints the list of frequencies | 20 | |
Uses a function to print the most frequent letter | 15 | |
Correctly prints the most frequent letter | 10 |
Concepts Exercised: program design, data and I/O, declaring facts, repeated tasks, application, making decisions, problem solving
© 2011 Yong Joseph Bakos.