CSCI 261 - Programming Concepts (C++)

Spring 2017 - Assignment 08 - Letter Frequency

Quick Links: Blackboard | Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This assignment is due by Tuesday, March 21, 2017 11:59pm.


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();
    MakaSauce();
    //...
    CookPizza();
    return 0;
}


Instructions



Write a program that prints the frequencies of letters that occur in the full text of Alice in Wonderland.

Your program MUST:
  • Open the file called alice.txt. Include this file with your submission.
  • 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.) Ignore all punctuation (* : . , ; [ ] # ' " etc.). We want to count only letters.
  • Use functions. The function prototypes must be written in a separate header file. The function definitions must be defined in a separate implementation file. DO NOT use global variables. You must use parameters properly, either pass-by-value or pass-by-reference.
  • Write to an output file a list of frequencies, as shown below.
  • Display the most frequent letter, as shown below.
When your program runs, it should create a file that looks like this (but with the correct values):

a 2
b 3
c 1
d 3
e 6
f 77
g 2
h 23
i 34
j 4
k 24
l 15
m 235
n 45
o 23
p 23
q 24
r 24
s 24
t 24
u 24
v 24
w 24
x 55
y 45245
z 4

And when your program runs, it should display the following the user in the terminal (again with the correct values):

Most frequent letter: y
Least frequent letter: c


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!


Grading Rubric


Your submission will be graded according to the following rubric.

Points Requirement Description
4 Labs completed
13 Assignment meets functional requirements outlined above
4 (1) Comments used (2) Coding style followed (3) Appropriate variable names, constants, and data types used (4) Instructions followed (5) Assignment compiles


Submission


Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.

In summary, for homework due on Tuesday, March 21 follow these specific steps:
  • create a directory called week08.
  • within week08, create three subdirectories: Lab08A, Lab08B, and A08.
  • within your new week08/Lab08A directory, copy in your main.cpp file from your Lab08A solution.
  • within your new week08/Lab08B directory, copy in your main.cpp file from your Lab08B solution.
  • within your new week08/A08 directory, copy in your main.cpp file AND function files from your A08 solution.
  • compress the week08 directory (see Step 3 here for details).
  • submit the week08.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!

This assignment is due by Tuesday, March 21, 2017 11:59pm.
Last Updated: 01/01/70 00:00


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0