HOMEWORK 10 - LETTER FREQUENCY

Concepts

Have you ever finished a book and wondered, "Geez, I wonder how many times the letter Q occurs in this text?" No? This week's homework illustrates a fundamental use of the array: storing related values in a single data structure, and then using that data structure to reveal interesting facts about the data.

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 freaky 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 data types.

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.

ABCUF

Always Be Using Functions. Let that be a rule of thumb for the remainder of the semester. When working on your programs, try to reach an end result where main doesn't do much "low level stuff" but rather leverages functions in order to do what your program needs to do.

For example, if you were to write a pizza-making program, don't write one super-long, hard-to-read, scare-your-date-away implementation of main. Use functions:

int main() {
    CreateShoppingList();
    BuyIngredients();
    GatherIngredientsInKitchen();
    PourYourselfGlassOfFineChianti();
    MakeDough();
    MakaDaSauca();
    //...
    CookPizza();
    return 0;
}

Get Started

For this specific homework you should download our SFML project template. Start the extraction process now, and then read the rest of these instructions (as the extraction process might take some time). See Lab08B for details on extracting the contents from the provided zip file (specifically in the "Before You Begin" and "Open and Run the Project" sections).

Once you have the project loaded into Visual Studios, please notice where your code for Part I and Part II should reside in main. Of course, if you use functions in your solution (which we suggest - see Part I Hints), then the function prototypes and function definitions should go above and below main respectively.

Homework Part I - Console

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:

a: 9805
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:

Part I Hints

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. 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 strongly suggest writing your program (one step at a time!)

As discussed above, we encourage you to Always Be Using Functions (ABCUF). However, 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.

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 online ASCII Table.)

You could solve the problem of which array index to increment with one VERY long switch statement. We do not recommend this; there are far more elegant (and easier to code/debug) solutions!

We strongly suggest you get the console version of this program working, and then focus on the GUI (part II). In other words, most points on this project will be checking your solution to Part I.

Homework Part II - Graphical User Interface

Ready for the GUI part of your homework? Fasten your seat belt and enjoy the ride! In this part you will draw a window that shows a bar for each letter of the alphabet, with its height proportional to the corresponding frequency (computed in Part I). The bar that corresponds to the most frequent letter should be painted with a different color. Each bar is identified by its correspondent letter with a label at the bottom.

a) Drawing the Bars

To draw the bars of the GUI you will use the RectangleShape object called bar. For each letter you should draw a bar with a height proportional to the letter frequency. All bars should have the same width, can you guess which?

To set the bar's size you should use the object's setSize function. In a similar way, to set the bar's position you should use the object's setPosition function. The following example shows how to change the size of the bar object using the values width=20 and height=100 AND the position of the bar object using the values x=80 and y=250. Note that because you will be drawing a bar for each letter using a loop, you should use variables like width, height, x, and y to set the bar's properties accordingly.

bar.setSize(Vector2f((float)20, (float)100));
bar.setPosition(Vector2f((float)80, (float)250));

To set the bar's color you should use the setFillColor function. Remember to set a different color to the bar that corresponds to the most frequent letter. After setting the bar's size, position, and color, you should draw the object on your window using the window's draw function, illustrated in the code that follows.

bar.setFillColor(Color::White);
window.draw(bar);

b) Drawing the Text Labels

To draw the text labels of the bars, you first need to load the particular font you will use (we include arial.ttf in the data directory of the SFML template) and then use the Text object (perhaps called text). To set the text's position you should use the object's setPosition function in a similar way as you did for the RectangleShape object. To set the text's label you should use the object's setString function. This function expects a string and what you have for each letter is a char. The following snippet of code illustrates how to set the position of the text object using the values x=80 and y=250 with a text label based on character 'a' (assuming you have a charToString function available). Finally, to actually draw the text object on your window you should use the draw function.

text.setPosition(Vector2f((float)80, (float)250));
text.setString(charToString('a'));
text.setColor(Color::Blue);
window.draw(text);

c) Putting it All Together (hints, hints, hints...)

Use a loop to traverse your frequencies array (computed in Part I). At each iteration, draw a bar and a text label for the correspondent letter. Let's say that you are using a variable named height to compute the height of the 'to be drawn' bar. You can then use the height of your window (i.e., the HEIGHT constant) divided by the frequency of the most frequent letter as a scaling factor to compute the height of each bar. After the bar's height is computed, determining its y coordinate is a piece of cake: just subtract the bar's height from the window HEIGHT.

Submission

You need to submit your solution of this homework (HW10) with two lab assignments: Lab10A and Lab10B. Detailed generic instructions for submitting homework assignments are available. For homework due next Wednesday (Apr 6th), follow these specific steps: