Question

Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

Program is in C++.  

Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded real number); and the average word length (also un-rounded). For example, suppose the file tobe.txt contains the following text:

To be  or not  TO BE,  
 THAT  IS  

really   the  question.

For the purposes of this problem, we will use whitespace to separate words. That means that some words include punctuation, as in "be,". For the input above, the call of wordStatsPlus("tobe.txt"); should produce exactly the following output. The number of "unique letters" is 14 because the file contains 14 distinct letters of the alphabet from A-Z: a, b, e, h, i, l, n, o, q, r, s, t, u, and y.

Total lines = 4
Total words = 11
Total unique letters = 14 (53% of alphabet)
Average words/line = 2.75
Average word length = 3.45455

If the input file does not exist or is not readable, your function should instead print the following output:

Error, bad input file.

Constraints: Your solution should read the file only once, not make multiple passes over the file data.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

/*
  
Write a function named wordStats that accepts as its parameter a string holding
a file name, opens that file and reads its contents as a sequence of words, and
produces a particular group of statistics about the input. You should report:
1. the total number of lines;
2. total number of words;
3. the number of unique letters used from A-Z, case-insensitively, and
4. its percentage of the 26-letter alphabet;
5. the average number of words per line (as an un-rounded real number); and
6. the average word length (also un-rounded).
Author: reaw17 Jan 9th 2018
*/

#include <iostream>
#include <fstream> // std::ifstream
#include <sstream> // std::ifstream
#include <string> // std::getline(string)
#include <cctype>
#include <iomanip>
#include <map>

using namespace std;

void mostCommon( string filename ) {

ifstream input( filename );
string line;

size_t totalNumOfWords = 0;
size_t totalNumOfLines= 0;
double totalLenOfWords = 0;

string token;
char charInWord;
map< char, int > charFreq;
map< char, int >::iterator iter;

cout << endl;
if ( !input.is_open() ) {
cout << "Error, bad input file" << endl;
} else {

while ( true ) {
input >> token;
if( input ) {
totalNumOfWords++;
totalLenOfWords += token.length();

for( size_t i = 0; i < token.length(); i++ ) {
if ( isalpha( token[ i ] ) ) {
charInWord = token[ i ];
charInWord = toupper( charInWord );
charFreq[ charInWord ]++;
} //end if()
} //end for()
} else {
break;
}
} // end while()
input.close();
cout << setw( 14 ) << left << "Total lines" << " = " << totalNumOfWords << endl;
cout << setw( 14 ) << left << "Total words" << " = " << totalNumOfWords << endl;
cout << setw( 13 ) << left << "Total unique letters" << " = " << charFreq.size() << endl;
cout << setw( 10 ) << left << "Average words/line" << " = " << ( totalLenOfWords / totalNumOfLines) << endl;
cout << setw( 10 ) << left << "Average word length" << " = "
<< ( totalLenOfWords / totalNumOfWords ) << endl;

for ( map< char, int >::iterator it = charFreq.begin(); it != charFreq.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
}
}

/* The main function() */
int main() {

string filename = "tobe.txt";

mostCommon( filename );

cout << endl;
cout << "This is my what happened!" << endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

  • Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name...

    Problem 3 Write a function named repeat_words that takes two string parameters: 1. in_file: the name of an input file that exists before repeat_words is called 2. out_file: the name of an output file that repeat_words creates Assume that the input file is in the current working directory and write the output file to that directory. For each line of the input file, the function repeat_words should write to the output file all of the words that appear more than...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

  • Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different...

    Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when uniqueWords is called. uniquewords creates the output file. Input. The function uniquewords takes two parameters: ◆ inFile, a string that is the name of text file that is to be read. The file that inFile refers...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • hey please answer in python 3 This is the test for these questions. it has to...

    hey please answer in python 3 This is the test for these questions. it has to be exactly the same as the Expected output. notice my code is very close to getting it but im always off by 0.01 or 0.02. Thank you for the help! In this exercise, you will build a program to process files containing paragraphs. The aim of the program is to simply count the number of words and compute their length. The file, however, may...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT