Question: 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
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.
Next, you need to open the following file using an input file stream: greeneggsandham.txt (shown below). 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)
greeneggsandham.txt:
I AM SAM. I AM SAM. SAM I AM.
THAT SAM I AM! THAT SAM I AM! I DO NOT LIKE THAT SAM I AM!
DO WOULD YOU LIKE GREEN EGGS AND HAM?
I DO NOT LIKE THEM, SAM I AM. I DO NOT LIKE GREEN EGGS AND HAM.
WOULD YOU LIKE THEM HERE OR THERE?
I WOULD NOT LIKE THEM HERE OR THERE. I WOULD NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM.
WOULD YOU LIKE THEM IN A HOUSE? WOULD YOU LIKE THEM WITH A MOUSE?
I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM.
WOULD YOU EAT THEM IN A BOX? WOULD YOU EAT THEM WITH A FOX?
NOT IN A BOX. NOT WITH A FOX. NOT IN A HOUSE. NOT WITH A MOUSE. I WOULD NOT EAT THEM HERE OR THERE. I WOULD NOT EAT THEM ANYWHERE. I WOULD NOT EAT GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM.
WOULD YOU? COULD YOU? IN A CAR? EAT THEM! EAT THEM! HERE THEY ARE.
I WOULD NOT, COULD NOT, IN A CAR.
YOU MAY LIKE THEM. YOU WILL SEE. YOU MAY LIKE THEM IN A TREE!
I WOULD NOT, COULD NOT IN A TREE. NOT IN A CAR! YOU LET ME BE. I DO NOT LIKE THEM IN A BOX. I DO NOT LIKE THEM WITH A FOX. I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM.
A TRAIN! A TRAIN! A TRAIN! A TRAIN! COULD YOU, WOULD YOU ON A TRAIN?
NOT ON TRAIN! NOT IN A TREE! NOT IN A CAR! SAM! LET ME BE! I WOULD NOT, COULD NOT, IN A BOX. I WOULD NOT, COULD NOT, WITH A FOX. I WILL NOT EAT THEM IN A HOUSE. I WILL NOT EAT THEM HERE OR THERE. I WILL NOT EAT THEM ANYWHERE. I DO NOT EAT GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM.
SAY! IN THE DARK? HERE IN THE DARK! WOULD YOU, COULD YOU, IN THE DARK?
I WOULD NOT, COULD NOT, IN THE DARK.
WOULD YOU COULD YOU IN THE RAIN?
I WOULD NOT, COULD NOT IN THE RAIN. NOT IN THE DARK. NOT ON A TRAIN. NOT IN A CAR. NOT IN A TREE. I DO NOT LIKE THEM, SAM, YOU SEE. NOT IN A HOUSE. NOT IN A BOX. NOT WITH A MOUSE. NOT WITH A FOX. I WILL NOT EAT THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE!
YOU DO NOT LIKE GREEN EGGS AND HAM?
I DO NOT LIKE THEM, SAM I AM.
COULD YOU, WOULD YOU, WITH A GOAT?
I WOULD NOT, COULD NOT WITH A GOAT!
WOULD YOU, COULD YOU, ON A BOAT?
I COULD NOT, WOULD NOT, ON A BOAT. I WILL NOT, WILL NOT, WITH A GOAT. I WILL NOT EAT THEM IN THE RAIN. NOT IN THE DARK! NOT IN A TREE! NOT IN A CAR! YOU LET ME BE! I DO NOT LIKE THEM IN A BOX. I DO NOT LIKE THEM WITH A FOX. I WILL NOT EAT THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE! I DO NOT LIKE GREEN EGGS AND HAM! I DO NOT LIKE THEM, SAM I AM.
YOU DO NOT LIKE THEM. SO YOU SAY. TRY THEM! TRY THEM! AND YOU MAY. TRY THEM AND YOU MAY, I SAY.
SAM! IF YOU LET ME BE, I WILL TRY THEM. YOU WILL SEE.
SAY! I LIKE GREEN EGGS AND HAM! I DO! I LIKE THEM, SAM I AM! AND I WOULD EAT THEM IN A BOAT. AND I WOULD EAT THEM WITH A GOAT... AND I WILL EAT THEM, IN THE RAIN. AND IN THE DARK. AND ON A TRAIN. AND IN A CAR. AND IN A TREE. THEY ARE SO GOOD, SO GOOD, YOU SEE! SO I WILL EAT THEM IN A BOX. AND I WILL EAT THEM WITH A FOX. AND I WILL EAT THEM IN A HOUSE. AND I WILL EAT THEM WITH A MOUSE. AND I WILL EAT THEM HERE AND THERE. SAY! I WILL EAT THEM ANYWHERE! I DO SO LIKE GREEN EGGS AND HAM! THANK YOU! THANK YOU, SAM I AM.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
