CSCI 261 - Programming Concepts

Fall 2018 - A7 - Sam I Am

Quick Links: Canvas | Mines | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |

This assignment is due by Thursday, November 15, 2018, 11:59 PM.

· Instructions · Rubric · Submission ·


In this homework, we will focus on arrays, vectors, strings, structs, File I/O, and SFML.


Overview



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

For this assignment, you will read in a text file containing the story Green Eggs and Ham. You will then need to count the number of occurrences of each word and display the frequencies. You'll be amazed at the results!


The Specifics Part I: File I/O



We'll want to use the same WordCount struct from the prior assignment.

Next, you need to open the following file using an input file stream: greeneggsandham.txt. Read this file one word at a time. Every time you read a word in, before you do anything you must remove the punctuation characters from the word (if present): . ? ! , ( ) - ; ' " _ :

We'll want to have created a vector of WordCount to store all of our words and their counts. Once you have stripped out each of the above characters, if this is the first time you are seeing a word then you need to insert it into your vector. Otherwise, if you have seen this word before then you will need to increment the count. (Hint: you'll need to use a searching algorithm on your vector)

After you have read all of the words in the file, you will then need to sort the vector alphabetically by words. (Hint: you'll need to use a sorting algorithm on your vector). Print out all the words and their counts using the following format (substituting the actual words and counts):

# 1 AWORD   :  3
# 2 WORDS2  : 14
...
#21 WORDS21 :  1
Most Frequent:  WORDS2  (14)
Least Frequent: WORDS21 ( 1)

Note how the data is aligned and words are alphabetical. Finally, print out the least frequent word with its count along with the most frequenct word and its count. To verify your output, the word "A" appers 56 times. The word "EAT" appears 23 times. Additionally, there are 50 unique words. (Hint: you'll need to use the minMax algorithm)

We used a vector to store the words and in that context it was an appropriate choice. We are next interested in the frequency of all the letters that appear in the book. For this use case, an array is more appropriate (think about why). We want to be sure we are treating uppercase and lowercase letters the same in our counting. Once the counts for each letter have been stored in an array (what is the appropriate type and size of the array?), print out the letter and frequency in the following format (using 3 decimal places). Also, print the most and least frequent letter with their counts. In the event that two letters have the same frequency, report the letter that comes first alphabetically. To verify your program is running properly, E occurs 277 times (11.58%) and J occurs 0 times (0.0%).

A: 30.123%
B:  0.532%
C: 10.001%
...
Z:  5.330%
Most Frequent: Q (1000)
Least Frequent: E (0)

You will want to make your program as general as possible and not having any assumptions about the data hardcoded in. We will run your program against the greeneggsandham.txt input file. We will also run your program against a second secret input file to ensure your program is flexible and will work on any input file.


The Specifics Part II: SFML



Now that we have processed all of the data, it's time to display it visually. 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 in the book, with its height proportional to the corresponding frequency. The bar that corresponds to the most frequent letter should be colored with a different color. The bar that corresponds to the least frequent letters should be colored with yet a third color.


Step I - Drawing the Bars



To draw the bars of the GUI you will use the RectangleShape class and create an 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 what it is?

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 word 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 and least frequent letters. 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);

Use a loop to traverse your frequencies array. At each iteration, draw a bar 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., a 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.

Lastly, we want to display text at the base of each bar that corresponds to the letter for that bar.

When you've drawn all the bars for all your letters, you should have a result similar to below (colors & size may vary, but results should be proportionally the same):


Functional Requirements



  • You may not make use of the standard library functions sort(), find(), any_of() or anything else from #include <algorithm>. You must implement your own sorting and searching functions.
  • 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.


Hints



  • Do not wait until the day before this is due to begin.
  • As discussed above, we encourage you to Always Be Using Functions. 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.
  • Do not just dive into the assignment. Create a mental plan of what tasks your program needs to accomplish. Convert this to pseudocode. 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 words in the file, and store the frequencies of each word?") and conduct another sanity check. We strongly suggest writing your program (one step at a time!)


Grading Rubric


Your submission will be graded according to the following rubric.

PointsRequirement Description
2 All code submitted properly.
6 All labs completed and submitted
4 Output matches for public Green Eggs and Ham test file & private hidden test file.
4 SFML window opens and draws properly.
4 Bar graph displays correct coloring and relative sizing.
3 File I/O structured appropriately.
2 Functional requirements above met.
2 (1) Comments used
(2) Coding style followed
(3) Appropriate variable names, constants, and data types used
(4) Instructions followed
27 Total Points

This assignment is due by Thursday, November 15, 2018, 11:59 PM.


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. The following instructions are copied from How to Submit Homework.


It is critical that you follow these steps when submitting homework.

If you do not follow these instructions, your assignment will receive a major deduction. Why all the fuss? Because we have several hundred of these assignments to grade, and we use computer tools to automate as much of the process as possible. If you deviate from these instructions, our grading tools will not work. And that makes us very unhappy. And when we're unhappy, we give penalties. Thus, make us happy.


Submission Instructions



Here are step-by-step instructions for submitting your homework properly:
  1. File and folder names are extremely important in this process. Please double-check carefully, to ensure things are named correctly.
    1. The top-level folder of your project must be named Set7
    2. Inside Set7, create 4 sub-folders that are required for this Set. The name of each sub-folder is defined in that Set (e.g. L7A, L7B, L7C, and A7).
    3. Copy your main.cpp , additional header & source files, plus the CMakeLists.txt file into the subdirectories of Set7 (steps 1-2), zip this Set7 folder (steps 3-4), and then submit the zipped file (steps 5-11) to Canvas.
    4. For example, when you zip/submit Set7, there will be 4 sub-folders called L7A, L7B, L7C, and A7 inside the Set7 folder, and each of these sub-folders will have a file called main.cpp, additional header & source files, plus the CMakeLists.txt file .

  2. Using Windows Explorer (not to be confused with Internet Explorer), find the file named "main.cpp" located inside the folder for the particular lab or homework assignment you will submit.

    STOP: Are you really sure you are viewing the correct assignment's folder?

  3. Now, for A7, right click on the main.cpp to copy the file. Then, return to the Set7/A7 folder and right click to paste the file. In other words, put a copy of your homework's main.cpp source code into the Set7/A7 folder. Repeat this for each additional header & source file you have with this assignment, plus CMakeLists.txt.

    Follow the same steps for L7A, to put a copy of your lab's main.cpp into the Set7/L7A folder. Repeat this process for Set7/L7B, Set7/L7C.

    STOP: Are you sure your Set7 folder now has all your code to submit?

  4. Now, right-click on the "Set7" folder.
    1. In the pop-up menu that opens, move the mouse "Send to..." and expand the sub-menu.
    2. In the sub-menu that opens, select "Compressed (zipped) folder".

    STOP: Are you really sure you are zipping a Set7 folder with sub-folders that each contain a main.cpp file in it?

  5. After the previous step, you should now see a "Set7.zip" file.

  6. Now visit the Canvas page for this course and click the "Assignments" button in the sidebar.

  7. Find Set7, click on it, find the "Submist Assignment" area, and then click the "Choose File" button.

  8. Find the "Set7.zip" file created earlier and click the "Open" button.

    STOP: Are you really sure you are selecting the right homework assignment? Are you double-sure?

  9. WAIT! There's one more super-important step. Click on the blue "Submit Assignment" button to submit your homework.

  10. No, really, make sure you click the "Submit Assignment" button to actually submit your homework. Clicking the "Choose File" button in the previous step kind of makes it feel like you're done, but you must click the Submit button as well! And you must allow the file time to upload before you turn off your computer!

  11. Canvas should say "Submitted!". Click "Submission Details" and you can download the zip file you just submitted. In other words, verify you submitted what you think you submitted!
In summary, you must zip the "Set7" folder and only the "Set7" folder, this zip folder must have several sub-folders, you must name all these folders correctly, you must submit the correct zip file for this homework, and you must click the "Submit Assignment" button. Not doing these steps is like bringing your homework to class but forgetting to hand it in. No concessions will be made for incorrectly submitted work. If you incorrectly submit your homework, we will not be able to give you full credit. And that makes us unhappy.


This assignment is due by Thursday, November 15, 2018, 11:59 PM.

Last Updated: 10/26/18 17:09


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