Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ For this assignment, download the A6 code pack. This zip file contains several files: main.cpp - the predetermined main.cpp. This file shows the

In C++ For this assignment, download the A6 code pack. This zip file contains several files:

main.cpp - the predetermined main.cpp. This file shows the usage and functionality that is expected of your program. You are not allowed to edit this file. You will not be submitting this file with your assignment.

CMakeLists.txt - the preset CMake file to build with your functions files.

input/greeneggsandham.txt - the contents of Green Eggs and Ham in text format.

input/aliceChapter1.txt - the first chapter of Alice in Wonderland in text format.

output/greeneggsandham.out - the expected output when running your program against the greeneggsandham.txt file

output/aliceChapter1.out - the expected output when running your program against the aliceChapter1.txt file

The contents of main.cpp are shown below:

#include

#include

#include

#include

using namespace std;

#include "functions.h"

int main() {

// open file for parsing

string filename = promptUserForFilename();

ifstream fileIn;

if( !openFile(fileIn, filename) ) {

cerr

cerr

return -1;

}

// read all the words in the file

vector allWords = readWordsFromFile( fileIn );

fileIn.close();

cout

// clean the words to remove punctuation and convert to uppercase

removePunctuation(allWords, "?!.,;:\"()_");

capitalizeWords(allWords);

// find only the unique words in the file

vector uniqueWords = filterUniqueWords(allWords);

cout

// put the words in to alphabetical order

alphabetizeWords( uniqueWords );

// count the number of occurrences of each word

vector uniqueWordCounts = countUniqueWords(allWords, uniqueWords);

// pretty print the unique words and their corresponding counts

printWordsAndCounts(uniqueWords, uniqueWordCounts);

// count the occurrences of every letter in the entire text

unsigned int letters[26] = {0};

countLetters(letters, allWords);

printLetterCounts(letters);

// print statistics on word and letter frequencies

printMaxMinWord(uniqueWords, uniqueWordCounts);

printMaxMinLetter(letters);

return 0;

}

Take note how the program now reads as a series of subtasks and the provided comments are redundant. The code is "self documenting" with the function names providing the steps that are occurring. Your task is to provide the implementations for all of the referenced functions. You will need to create two files: functions.h and functions.cpp to make the program work as intended.

You will want to make your program as general as possible by not having any assumptions about the data hardcoded in. Two public input files have been supplied with the starter pack. We will run your program against a third private input file.

Function Requirements

The requirements of each function are given below. The input, output, and task of each function is described. The functions are:

  1. promptUserForFilename()
  2. openFile()
  3. readWordsFromFile()
  4. removePunctuation()
  5. capitalizeWords()
  6. filterUniqueWords()
  7. alphabetizeWords()
  8. countUniqueWords()
  9. printWordsAndCounts()
  10. countLetters()
  11. printLetterCounts()
  12. printMaxMinWord()
  13. printMaxMinLetter()

promptUserForFilename()

Input: None Output: A string Task: Prompt the user to enter a filename.

openFile()

Input: (1) The input file stream (2) The string filename to open Output: True if the file successfully opened, False if the file could not be opened Task: Open the input file stream for the corresponding filename. Check that the file opened correctly. The string filename will remain unchanged.

readWordsFromFile()

Input: The input file stream Output: A vector of strings Task: Read all of the words that are in the filestream and return a list of all the words in the order present in the file.

removePunctuation()

Input: (1) A vector of strings (2) A string of all the punctuation characters to remove Output: None Task: For each word in the vector, remove all occurrences of all the punctuation characters denoted by the punctuation string. When complete, the input vector will now hold all the words with punctuation removed. The punctuation string will remain unchanged.

capitalizeWords()

Input: A vector of strings Output: None Task: For each word in the vector, convert each character to its upper case equivalent. When complete, the input vector will now hold all the words capitalized.

filterUniqueWords()

Input: A vector of strings Output: A vector of strings Task: The function will return only the unique words present in the input vector. The output vector will not contain any duplicate words.

alphabetizeWords()

Input: A vector of strings Output: None Task: Sort the strings in the input vector alphabetically. When complete, the input vector have the same length and contents but reordered so that the contents are in alphabetical order.

countUniqueWords()

Input: (1) A vector of strings representing all of the words in the file (2) A vector of strings representing only the unique words in the file Output: A vector of unsigned integers Task: For every unique word in the list, count the number of occurrences the unique word is present in the full text. Return a vector of all the counts. Each position in the vector of counts corresponds to the same position in the unique word list. The vector of counts will have the same size as the vector of unique words. Upon completion, neither input vector will be modified.

printWordsAndCounts()

Input: (1) A vector of strings (2) A vector of unsigned integers Output: None Task: For each word and count in the vectors, print out the word and its corresponding count. Upon completion, the two vectors will remain unchanged. Format the output as follows:

#P : ABCDEF : #C

Notice how there are three columns separated by :. We want the : aligned in every row and the values aligned in each column. The columns correspond to the following values:

#P - The position the word in the list. Begin at 1. Right align all values. Allocate enough space for the length of the last position. (If there are less than 10 numbers, then we need only 1 space. If there are less than 100 numbers, then we need only 2 spaces. And so on. Assume there will be at most 109unique words.)

ABCEDF - The unique word. Left align all values. Allocate enough space for the longest word present in the list.#C - The corresponding count of the unique word. Right align all values. Allocate enough space for the length of the largest number. (Assume there will be at most 109unique words.)

An example with actual values is shown below:

1 : BIRTHDAY : 4

2 : BJORNE : 1

3 : HAPPY : 4

4 : TO : 4

5 : YOU : 3

Refer to the expected output files for longer examples on the expected formatting.

countLetters()

Input: (1) An array of 26 unsigned integers (2) A vector of strings Output: None Task: Count the number of occurrences of each letter present in all words. Each position of the array corresponds to each letter as ordered by the English alphabet. Upon completion, the array will hold the counts of each letter and the vector of strings will remain unchanged.

printLetterCounts()

Input: An array of 26 unsigned integers Output: None Task: For each letter, print out the letter and its corresponding count. Format the output as follows:

A : #C

B : #C

...

Y : #C

Z : #C

Notice how there are two columns separated by :. We want the : aligned in every row and the values aligned in each column. The columns correspond to the following values:

A - The letter

#C - The corresponding count of the letter. Right align all values. Allocate enough space for the length of the largest number. (Assume there will be at most 109unique words.)

An example with actual values is shown below:

A : 8

B : 5

C : 0

D : 4

E : 1

F : 0

G : 0

H : 8

I : 4

J : 1

K : 0

L : 0

M : 0

N : 1

O : 8

P : 8

Q : 0

R : 5

S : 0

T : 8

U : 3

V : 0

W : 0

X : 0

Y : 11

Z : 0

Refer to the expected output files for longer examples on the expected formatting.

printMaxMinWord()

Input: (1) A vector of strings (2) A vector of unsigned integers Output: None Task: Print out the two words that occur least often and most often. If there is more than one word that occurs the same number of times, print the one that comes first alphabetically. Upon completion, both input vectors will remain unchanged. Print out the following pieces of information:

The word

The number of occurrences

The frequency of appearance as a percentage to 3 decimal places

Format the output as follows:

Least Frequent Word: ABCDEF #C (#P%)

Most Frequent Word: ABCDEF #C (#P%)

Notice how there are three columns of values. The columns correspond to the following values:

ABCEDF - The word. Left align all values. Allocate enough space for the longer of the two words.

#C - The corresponding count of the word. Right align all values. Allocate enough space for the length of the larger number. (Assume there will be at most 109unique words.)

#P - The frequency of the word. Right align all values. Print to three decimal places.

An example with actual values is shown below:

Least Frequent Word: BJORNE 1 ( 6.250%)

Most Frequent Word: BIRTHDAY 4 ( 25.000%)

Refer to the expected output files for longer examples on the expected formatting.

printMaxMinLetter()

Input: An array of 26 unsigned integers Output: None Task: Print out the two letters that occur least often and most often. If there is more than one letter that occurs the same number of times, print the one that comes first alphabetically. Upon completion, the input array will remain unchanged. Print out the following pieces of information:

  1. The letter
  2. The number of occurrences
  3. The frequency of appearance as a percentage to 3 decimal places

Format the output as follows:

Least Frequent Letter: A #C (#P%)

Most Frequent Letter: Z #C (#P%)

Notice how there are three columns of values. The columns correspond to the following values:

A - The letter.

#C - The corresponding count of the letter. Right align all values. Allocate enough space for the length of the larger number. (Assume there will be at most 109occurrences.)

#P - The frequency of the letter. Right align all values. Print to three decimal places.

An example with actual values is shown below:

Least Frequent Letter: C 0 ( 0.000%)

Most Frequent Letter: Y 11 ( 14.667%)

Refer to the expected output files for longer examples on the expected formatting.

Functional Requirements

You may not make use of the standard library functions sort(), find(), any_of() or anything else from #include . You must implement your own sorting and searching functions.

DO NOT use global variables.

You must use parameters properly, either pass-by-value or pass-by-reference.

Do not use any global variables. You must use parameters properly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions

Question

How should items such as profit and asset values be measured?

Answered: 1 week ago

Question

Who are the users of financial accounting information?

Answered: 1 week ago