SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Streams And File Io
Exercise:
Programming Projects
Question:10 | ISBN:9780132846813 | Edition: 5

Question

Write a program that will compute the average word length (average number of characters per word) for a file that contains some text. A word is defined to be any string of symbols that is preceded and followed by one of the following at each end: a blank, a comma, a period, the beginning of a line, or the end of a line. Your program should define a function that is called with the input-file stream as an argument. This function should also work with the stream cin as the input stream, although the function will not be called with cin as an argument in this program. If this is being done as a class assignment, obtain the file names from your instructor.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
// Function to compute the average word length
double computeAverageWordLength(istream& input_stream) 
{
    // Variables to keep track of the total word length and word count
    int total_length = 0;
    int word_count = 0;

    // Read each line from the input stream
    string line;
    while (getline(input_stream, line)) {
        istringstream iss(line);

        // Process each word in the line
        string word;
        while (iss >> word) {
            // Count the characters in the word
            total_length += word.length();
            word_count++;
        }
    }

    // Calculate the average word length
    double average_length = static_cast<double>(total_length) / word_count;
    return average_length;
}

int main() {
    // Get the filename from the user
    string filename;
    cout << "Enter the filename: ";
    cin >> filename;

    // Open the input file stream
    ifstream input_file(filename);

    // Check if the file was opened successfully
    if (!input_file) {
        cerr << "Error: Unable to open the file." << endl;
        return 1;
    }

    // Compute the average word length using the function
    double average_word_length = computeAverageWordLength(input_file);

    // Close the input file
    input_file.close();

    // Output the average word length with two decimal places
    cout << "Average Word Length: " << fixed << setprecision(2) << average_word_length << endl;

    return 0;
}

Data in input file(inFile.txt):

Twinkle, twinkle, little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle, twinkle, little star
How I wonder what you are
When the blazing sun is gone
When he nothing shines upon
Then you show your little light
Twinkle, twinkle, all the night
Twinkle, twinkle, little star
How I wonder what you are

 

OUTPUT:

Enter the filename: inputFile.txt
Average Word Length: 4.34

 

0 0

Discussions

Post the discussion to improve the above solution.