Homework 22: Letter Frequency
This assignment has an extended due date (Apr 4). Start early, so you can ask questions sooner rather than later -- exploring and asking questions is part of this assignment.
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, loops, and graphics together in one assignment to create a program that displays a graphical statistical 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 displays a bar graph representing the frequencies of letters that occur in the full text of Alice in Wonderland. When your program runs, it should look like this:
Your program should:
- Open the file called
alice.txt
(in 22_letterFrequency/bin/data 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 bar graph of frequencies, as shown above.
Hints
Note: There is a typo in testApp.cpp. The suggested function determineMax is misspelled.
Start early. This assigment isn't difficult if you start early, ask questions, and do things one step at a time.
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.
Start early. It's so important, we're mentioning it again.
The comments and function stubs in testApp.cpp
are only a suggestion, not a strict guide.
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.
Use the value "data\\alice.txt" for the filename.
Start early. Can you accomplish one step today?
Notice that the frequencies array stores values indexed from 0 - 25, with the frequencies of 'a' and 'A' stored in frequencies[0]. 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 are we subtracting 97? (see the ascii table)
Don't worry about all the scaling drama until you can draw a crude, ugly bar graph.
Notice how ofRect needs the x/y coordinate of the upper left corner of a box, and then the height and width. For this assignment, you will likely need to use a negative height.
What does the ofDrawBitmapString do?
ofDrawBitmapString
expects a string. To convert a char
into a string
do something like this: string(1, someChar)
. This tells the machine, "Give me a string that is one letter long and contains someChar
."
Start early. Ask questions early.
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Aha, your first full-fledged program. Can you defeat me? I anxiously *bzzzt* await your solutions.
This work is worth 200 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 determine bar graph "scale" | 10 | |
setup loads frequencies | 20 | |
setup determines max frequency index | 10 | |
setup determines scale | 10 | |
setup sets background color | 20 | |
draw correctly draws a bar graph of frequencies | 20 | |
draw colors bar of highest frequency | 10 | |
draw correctly draws letters a - z | 10 | ofBitmapString |
Concepts Exercised: program design, data and I/O, declaring facts, repeated tasks, graphics, application, making decisions, problem solving, fun
© 2011 Yong Joseph Bakos.