CSCI 261 - Programming Concepts (C++)

Spring 2018 - Assignment 6 - Sam I Am

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

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This assignment is due by Thursday, April 5, 2018 11:59 PM.

In this homework, we will focus on 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 week's 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 graph the frequencies. You'll be amazed at the results!


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;
}


The Specifics Part I: File I/O



You must first create a struct named WordCount to represent a word and the number of occurrences. It must have two members, a count stored as an int and a word stored as a string. Since this struct definition will be used in multiple files, you probably would want to create this in its own header file and then include this header in any other file that requires the use of the WordCount struct. (Hmm, this seems similar to the Blackjack set up).

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 following 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:

# 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 frequency word and its count. To verify your output, the most frequent word is "I" with a count of 83. The least frequent word is "IF" with a count of 1. Additionally, there are 50 unique words. (Hint: you'll need to use the minMax algorithm)


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 word in the book, with its height proportional to the corresponding frequency. The bar that corresponds to the most frequent word should be painted with a different color. The bar that corresponds to the least frequent letters should be painted with yet a third color.


Step I - Drawing the Bars



To draw the bars of the GUI you will use the RectangleShape object called bar. For each word you should draw a bar with a height proportional to the word 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 words. 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 vector. At each iteration, draw a bar for the correspondent owrd. 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 word 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.

When you've drawn all the bars for all your words, 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.

Points Requirement Description
2 All code submitted properly
4 Labs completed
10 Word count is outputted correctly - both content and format
4 SFML window opens and draws properly
2 Bar graph displays correct coloring and relative sizing
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
26 Total Points


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.

From your Cloud9 workspace, right click on the Set6 folder in your workspace tree. Select "Download" from the pop-up menu. This will download a file called Set6.zip to your computer. It contains all the files of your Set6 folder (therefore L6A, L6B, A6).

Now in Canvas, go to Assignments > Set6. Upload your Set6.zip file you just downloaded. And voila! Easy peasy.

In summary, follow these specific steps:
  • right click on the Set6 folder in Cloud9.
  • select "Download" from the pop-up menu.
  • upload the Set6.zip file to Canvas Set6.
  • after you submit, download the file and double check it contains all that you think it contains!


This assignment is due by Thursday, April 5, 2018 11:59 PM.
Last Updated: 04/03/18 12:31


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