Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal: Your assignment is to write a C++ program to read in a file of English text, and determine the length distribution of words in

Goal: Your assignment is to write a C++ program to read in a file of English text, and determine the length distribution of words in the text as well as count the occurrences of specific words. This assignment will allow you to practice working with arrays. Program Output: The text for The Message translation of John 1 was copied into a file called john1_niv.txt. Footnote references were removed. My solution to this assignment was run using this file. The following screenshot shows what your program should output when this file is specified. As you can see it has counted the number of words of each length, up to 12 characters long. It also counts the number of words that are over 12 characters long. It displays the corresponding percentage to the right of the actual count. In addition, it has counted the number of times two groups of specific words occur (God words1 and article words2), displaying both the counts and percentages. Implementation using arrays: This lab assignment gives you the opportunity to practice using arrays. As you read through the words in the text, you will use one array (an int array of 12 elements) to count the number of words of each length, and you will use two sets of parallel arrays to count the number of occurrences of specific words. Functions you need to write: 1) A function that takes a string read from the file as a parameter, and returns a new string which contains no characters except letters of the alphabet, and with all letters converted to upper case. NOTE: for this function you will want to use functions defined in . But there is a small wrinkle. The test file was copied from the internet and it contains some 8-bit ASCII characters. If you call functions directly, you will get an assert. You should cast the char value to unsigned char first before passing to the function. Use static_cast. 1 GOD, JESUS, CHRIST, HOLY, SPIRIT 2 A, AN, THE 2) A function that takes a count array and the number of elements in this array as parameters, and sets all array elements to zero. 3) A function that takes a word, a count array, the number of elements in the array, and a reference parameter for the count of words with length longer than the size. This function will increment the appropriate count depending on the length of the word. Note that this function will not be called with a zero-length word, because your main program will ignore these words, but because you are a good programmer you will include an assert statement to make sure it is not called with an empty word. 4) A function that takes a word, an array of special words, a parallel array of counts, and the number of elements in the array as parameters. The function will update the appropriate count if the word matches one of the words in the array of special words. 5) A function that takes a count array, the number elements in the array, the count of longer words and the total number of words in the file. This function writes the results to cout. 6) A function that takes a special word array, the parallel count array, the number elements in the arrays, and the total number of words in the file. This function writes the results to cout. Main Program The main program first prompts the user to enter a file name. You will open that file for input. If the file is not successfully opened, you will print an informative error message and exit the main program. Next you will initialize all count arrays to zero using the function described above. You will then begin reading through the file word by word. You will call your function to normalize the word you read. In some cases, you may find that there is nothing left in the word because it is a number or contains only punctuation. In that case, you will not try to count that word. For words that are 1 character or longer, you should call the first count function to count it by length. Then you will call the second count function twice, one for each group of specific words. After you finish reading through the file, you will of course close the file. Then call your first print routine to print out the distribution of word lengths as shown in the screen shot. Next call the second print routine twice to print out the occurrences of each group of specific words. Of course, you will also have print descriptive headers before you call each of these routines, since the header printing is specific the words you are counting. Error Handling: Other than checking the file was opened successfully, you should not have to include any other error handling. Program Style Please refer to the Lab 1 assignment document for appropriate style. Here are some additional style rule: When you design your functions: Make sure that all object parameters are passed by reference. In other words, if you are passing a string object or a stream object, pass by reference. If dont intend for your function to modify an object or array parameter, place const keyword before the parameter. The compiler will then make sure you do not accidentally modify that parameter. In this lab many, but not all, of the object and array parameters of the functions described above should be declared const. Here is some guidance specific to this lab: 1) Define a const int variable equal to 12, and use that instead of 12 everywhere in the code. Our style guide for const variables is that their name should be descriptive and in all capital letters. 2) For the parallel arrays, you should not specify the size when you define the array of specific words. Let C++ count it for you based on the list of words you initialize it with. Then use sizeof()/sizeof(string) when you specify the size of the count array.

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

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

=+28.13. Show that the normal and Cauchy laws are stable.

Answered: 1 week ago