CSCI 261 - Programming Concepts (C++)Fall 2017 - Assignment 6 - Sam I AmQuick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks |
|||||||||||||||||||
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | |||||||||||||||||||
This assignment is due
by Friday, November 10, 2017 11:59 PM.
In this homework, we will focus on File I/O and SFML.
OverviewHave 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/OYou 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 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 words are "IF " and "THEN " with a count of 1 each. Additionally, there are
51 unique words. (Hint: you'll need to use the minMax
algorithm)The Specifics Part II: SFMLNow 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.
When you run your project, be sure to select "
C++ (Makefile)
" as your run option. Once you have the following two pieces in your program:
RenderWindow window( VideoMode( 640, 480 ), "SFML Window" );
window.display();
run it. You WILL get an error message that says
bash: line 12: 646995 Segmentation fault $file.o $args
THIS IS OK! We can safely ignore that message. Why? Well, Cloud9 is
trying to run your program in the tab you have open right now. But SFML
wants to open a new window to display the graphics. Cloud9 cannot open
a second window on its own. But you can! Part of the magic setup
process that you did at the beginning added another runner to your
Cloud9 workspace. Go to "
Run > Run With > C9vnc
". This will pop open a little window in your workspace with
information that looks like the following:
Your GUI is running at https://class-examples-jpaone.c9users.io/vnc.html.
VNC client running at https://class-examples-jpaone.c9users.io/vnc.html 2017-03-20 02:30:40,315 INFO supervisord started with pid 635120 2017-03-20 02:30:41,318 INFO spawned: 'novnc' with pid 635125 2017-03-20 02:30:41,320 INFO spawned: 'xvfb' with pid 635126 2017-03-20 02:30:41,322 INFO spawned: 'x11vnc' with pid 635127 2017-03-20 02:30:41,325 INFO spawned: 'fluxbox' with pid 635128 2017-03-20 02:30:42,417 INFO success: novnc entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2017-03-20 02:30:42,419 INFO success: xvfb entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2017-03-20 02:30:42,419 INFO success: x11vnc entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2017-03-20 02:30:42,420 INFO success: fluxbox entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
That first line is the most important one. It is telling you that your GUI is running at another website. Open that URL in a new tab. You will see a window like below:
Right click on the background and select "Applications > Shells > Bash".
This will open a terminal window just like in the Cloud9 environment. We will enter all of our commands here to run our program.
Remember all those commands you were
copying in before? Well, they are Linux commands (congrats! you've been
working with Linux this whole time). What you need to do is to change
your directory (folder) to where your Assignment6 is. If you have been
following our naming scheme, then Assginment6 should be located inside of
a folder called A6. You can change to this folder by typing
cd A6/Assignment6
If you type the command "
ls
" you should see a list of all the files. One of them will colored
green with a
*
next to it. This should be
a.out
, the executable for your program! You can run your program by typing the following command:
./a.out
You might see a message that says
"Failed to use XRandR extension". Once again, it's ok. Ignore it. More
importantly, you should now see a new window that has popped up
containing your SFML program! How cool!
We also saw the development of a smiley face in class.
A few key lines of
code covered follow:
// Draw a circle object called star and color it yellow
CircleShape star; star.setPosition( 15, 15 ); star.setRadius( 300 ); star.setFillColor( Color::Yellow ); window.draw( star ); // Draw a rectangle object called rect and color it blue RectangleShape rect; rect.setSize( Vector2f( 45, 150 ) ); rect.setPosition( 200, 150 ); rect.setFillColor( Color(0, 0, 255) ); window.draw( rect ); // Draw a text object called label Font myFont; if( !myFont.loadFromFile( "data/arial.ttf" ) ) return -1; Text label; label.setFont( myFont ); label.setString( "Hello World!" ); label.setPosition( 250, 520 ); label.setColor( Color::Black ); window.draw( label ); 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
Hints
Grading Rubric
Your submission will be graded according to the following rubric.
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 A6 folder in your workspace tree. Select "Download"
from the pop-up menu. This will download a file called A6.zip to your computer. It
contains all the files of your A6 folder (therefore Lab6A,
Lab6B, Assignment 6). Now in Canvas, go to Assignments > A6. Upload your
A6.zip file you just downloaded. And voila! Easy
peasy.
This assignment is due
by Friday, November 10, 2017 11:59 PM.
| |||||||||||||||||||
Last Updated: 11/05/17 19:55
|