Write a program that reads the contents of a text file. The program should then create a structure that has 3 fields: a word, the frequency of the word, the list of line numbers where the word appears. Then the program will open a text file and read the content of the file. It will create another text file as output file. The output file should contain an alphabetical listing of the words, their frequency, and the list of line numbers where the word appears. -g

Answers

Answer 1
Answer:

Answer:

See explaination

Explanation:

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <map>

#include <sstream>

using namespace std;

// structure to hold the word, frequency and list of lines in which word appears

struct wordFrequency

{

string word;

int frequency;

vector<int> lineNumber;

};

// main method

int main()

{

// input file name. You can change the input file name if your file name is different

string inputFileName = "data.txt";

// creating a map class object to hold words uniquely

map<string, wordFrequency> map_word;

// ifstream class object to open and read from file

ifstream fin(inputFileName);

// validating if file is opened or not

if (!fin.is_open())

{

cout << "Error in opening file!";

exit(1);

}

// string to hold line from file

string line;

// int variable to keep track of line number

int lineNumber = 0;

// fetching lines from file

while (getline(fin, line))

{

// increasing the lineNumber count because we fetch another line

++lineNumber;

// breaking a line into words using stringstream class object

string word;

stringstream iss(line);

// iterating over all the words in a line

while (iss >> word)

{

// if the word is not in the map then we create and add a new pair

auto it = map_word.find(word);

if (it == map_word.end())

{

// creating a new struct object to store new word

wordFrequency w;

w.word = word;

w.frequency = 1;

w.lineNumber.push_back(lineNumber);

map_word.insert({word, w});

}

else

{

// if the word is already there then incresing frequency and push line number into list

it->second.frequency += 1;

it->second.lineNumber.push_back(lineNumber);

}

}

}

// closing the input file

fin.close();

// creating a new output file

ofstream fout("WordFrequency.txt");

if (fout.is_open())

{

// iterating over a map

for (auto word : map_word)

{

// writing data to a output file

fout << "Word is : " << word.second.word << endl;

fout << "Frequency is : " << word.second.frequency << endl;

fout << "Appears in line : ";

for (auto i : word.second.lineNumber)

{

fout << i << " ";

}

fout << endl

<< endl;

}

// closing output file

fout.close();

}

else

{

cout << "Error! Not able to create output file!";

}

}


Related Questions

Convert the binary number into a hexadecimal number.
Write a python program to calculate and print the electric bill for Ethiopian Electricity Corporation. (consumer name meter number(mno),last month reading(Imr)and current month reading(cmr) of 50 customers and calculate the net bill amounts as follows: Number of unit(Nou)=cmr-lmr If Nou200 then bill =Nou*2 tax=bill*0.15 netbill=bill+tax Print all the details in the bill for all customers​
Which of the following people was a member of FFA?O Kenny ChesneyO Tim McGrawO Alan JacksonO Billy Currington
Numbers are calculated in Excel using formulas and ______.
Write the prototype for a function named showSeatingChart that will accept the following two-dimensional array as an argument. const int ROWS = 20; const int COLS = 40; string seatingChart[ROWS][COLS]; Note: The two-dimensional array argument must be a const string array. You must include a second integer argument (scalar, not an array).

Select the correct answer.At which stage of art criticism do you sum up your evaluations?

A description

B. judgment

C. interpretation

D. analysis

Reset

Next

Answers

Answer: B. judgment

Explanation: To critique a work of art, there are a 4 steps method:

Description: describe everything you see in the work: name of the artist, the title, what you observe, what media he/she used, the year it was created;

Analysis: describe how the artist uses the elements of art (line, shape, color, texture, space) and the principlesofdesign (balance, movement, pattern, emphasis, contrast);

Interpretation: try to explain what is the meaning of the art, why the artist created it, what was he/she's idea for it, based on the 2 previous steps;

JudgmentorEvaluate: make an opinion of why the art is worth it based on the other steps and give reasons for liking or disliking, its importance or the way it make you feel;

After going through each step, your evaluation or opinion will be expressed in the 4th step: Judgment.

Which of the following protocols is used to unsure secure transmissions on port 443?A. HTTPSB. TelnetC. SFTPD. SHTTP

Answers

Answer:

A. HTTPS

Explanation:

Default HTTP port is 80. This means that the server request is received on port 80. But default HTTP request is susceptible to interception. In order to prevent it, HTTP communication is secured using SSL encryption (HTTPS protocol). In this case the Url string begins with 'https://' which differentiates it from normal web request. The default port used for HTTPS service is 443 instead of 80.

How can you represent a graphic element in a wireframe?You can specify a graphic element by using an)

Answers

Answer:

The answer is "in the form of design, color and graphic".

Explanation:

The wireframe is also recognized as 'skeleton', it is a static, low-fidelity description of various layouts, which provides shapes to the component. It is indeed a visualization of even an interface using only basic shapes.

It is a 2D illustration of a site user interface, which primarily focuses on capacity planning and priority of information, features available, and role expectations like, storyboards which do usually not include design, color, or graphics.

Answer:

box

Explanation:

i just took the quiz and that's the correct answer they're looking for :)

g Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display -"ytivarg". Demonstrate the method in a program that asks the user to input a string and then passes it to the method.

Answers

Answer:

The program written in C++ is as follows; (See Explanation Section for explanation)

#include <iostream>

using namespace std;

void revstring(string word)

{

   string  stringreverse = "";

   for(int i = word.length() - 1; i>=0; i--)

 {

     stringreverse+=word[i];

 }

 cout<<stringreverse;

}

int main()

{

 string user_input;

 cout << "Enter a string:  ";

 getline (std::cin, user_input);

 getstring(user_input);

 return 0;

}

Explanation:

The method starts here

void getstring(string word)

{

This line initializes a string variable to an empty string

   string  stringreverse = "";

This iteration iterates through the character of the user input from the last to the first

   for(int i = word.length() - 1; i>=0; i--)   {

     stringreverse+=word[i];

 }

This line prints the reversed string

 cout<<stringreverse;

}

The main method starts here

int main()

{

This line declares a string variable for user input

 string user_input;

This line prompts the user for input

 cout << "Enter a string:  ";

This line gets user input

 getline (std::cin, user_input);

This line passes the input string to the method

 revstring(user_input);

 return 0;

}

When C++ is working with an operator, it strives to convert operands to the same type. This is known as A) type correction
B) type conversion
C) demotion
D) promotion
E) None of these

Answers

Answer:

The answer is "Option B"

Explanation:

Conversion of type is a way of changing an entity from one data type to another. It is used to make the variable process properly by a function in computer coding.  

In the C++ language, the result of the multitype value added or any other option is the result between those variables as a large type at that time, that's why choice b is correct.

DSL is the abbreviation for?data service line

digital subscriber line

data subscriber line

digital service line

none of the above

Answers

Answer:

digital subscriber line