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

Write a function named "read_json" that takes a JSON formatted string as a parameter and return the data represented by the input in the appropriate types for this language. For example, if the input string represents a JSON object you should return a key-value store containing the same data?
What is a digital projector? How is it different from computer screen?
Why is OS important in every data processing system? Please Answer Fast! ​
Constructing a concurrent server by spawning a process has some advantages and disadvantages compared to multithreaded servers. Discuss a few.
3. Problem 5. A digital computer has a memory unit with 24 bits per word. The instruction set consists of 150 different operations. All instructions have an operation code part (opcode) and an address part (allowing for only one address). Each instruction is stored in one word of memory. a. How many bits are needed for the opcode? b. How many bits are left for the address part of the instruction? c. What is the maximum allowable size for memory? d. What is the largest unsigned binary number that can be accommodated in one word of memory?

Write a program to read a list of exam scores given as integer percentages in the range 0-100. Display the total number of grades in each letter grade defined as follows:90-100 is an A, 80-89 is a B, 70-79 is a C, 60-69 is a D, 0-59 is an F. Use a negative score as a sentinel to indicate the end of the input. (The negative value is used just to end the loop, do not use it in the calculations). Then output the highest and lowest score, and the average score.For example if the input is: 72 98 87 50 70 86 85 78 73 72 72 66 63 85 -1the output would be:Total number of grades = 14Number of As =1Number of Bs = 4Number of Cs = 6Number of Ds = 2Number of Fs = 1The highest score is 98The lowest score is 50The average is 75.5This is what I have so far and it is not working correctly:public static void main(String[] args) {// scannerScanner scnr =new Scanner (System.in);//ints grades and countint x;int A = 0;int B = 0;int C = 0;int D = 0;int F = 0;int count = 1;//int min max totalint min, max;int total = 0 ;//doubledouble average;//prompt user for inputSystem.out.print("Please enter the exam scores as integer ");System.out.print("percentages in the rage 0-100. ");System.out.println("Please end the list with a negative integer.");//scnrx = scnr.nextInt();min = x;max = x;//while loopwhile (x >= 0){x = scnr.nextInt();if (x >= 0){total = total + x;count++;if (x < min)min = x;if (x > min)max = x; }while (x >= 90 && x <= 100) {x = scnr.nextInt();A++;//Grade Bif (x >= 80 && x <= 89)B++;//Grade Cif (x >= 70 && x <= 79)C++;//Grade Dif (x >= 60 && x <= 69)D++;//Grade Fif (x >= 0 && x <= 59)F++;}}// averageaverage = total/count;//results/outputSystem.out.println("Total number of grades: " + count);System.out.println("Number of A's: " + A);System.out.println("Number of B's: " + B);System.out.println("Number of C's: " + C);System.out.println("Number of D's: " + D);System.out.println("Number of F's: " + F);System.out.println("Highest score: " + max);System.out.println("Lowest score: " + min);System.out.println("Average: " + average);}}

Answers

system out print , 100-5292

A majority of the people whose information was compromised are homeless; therefore there is no easy way to contact them in order to alert them of the security breach. How should Data-Time Inc. manage this breach in security

Answers

Incomplete question. Here's the full question:

Data-Time Inc. is a company that manages databases for a large city in Colorado. Included in these databases is information collected from the city’s homeless shelters and free clinics. Specifically, the databases contain personal information of the users of these services over the past 10 years; this includes people’s Social Security numbers and health records.

This data is highly secure and only accessible to the employees of Data-Time Inc. Employees are given a laptop when they are hired which allows them to access the database remotely. Unfortunately, one of these laptops is stolen and the security of the database is compromised.

A majority of the people whose information was compromised are homeless; therefore there is no easy way to contact them in order to alert them of the security breach. How should Data-Time Inc. manage this breach in security?

Explanation:

Since the emphasis is not on reversing the breach that has occurred, but on managing the level of security damage that could occur, it is important measures are directed towards preventing unauthorized access to sensitive information.

For example, the Media Access Control Address (MAC address) of the laptop if known could be greylisted from accessing the server that contains the sensitive information.

A ________ topology uses more than one type of topology when building a network. crossover multiple-use fusion hybrid

Answers

Answer:

hybrid topology

Explanation:

The type of topology that is being described is known as a hybrid topology. Like mentioned in the question this is an integration of two or more different topologies to form a resultant topology which would share the many advantages and disadvantages of all the underlying basic topologies that it is made up of. This can be seen illustrated by the picture attached below.

Which is an example of adaptive social behavior?

Answers

Birds help take care of each other's young to increase their chances of survival, and the behavior is passed on to offspring. 

Hope this helps!! :)

Answer:

D.

Explanation:

edge 2021

Write a program that asks the user to enter two numbers,obtains the two numbers from the user and prints the sum,product,difference, and quotient of the two numbers

Answers

Answer:

#include<iostream>

using namespace std;

//main function

int main(){

   //initialization

   float a1,a2;

//display the message

cout<<"Enter the first number: ";

   cin>>a1;  //store the value

   cout<<"Enter the second number: ";

   cin>>a2;   //store the value

   //display the calculation result

   cout<<"The sum is: "<<a1+a2<<endl;

   cout<<"The Subtraction is: "<<a1-a2<<endl;

   cout<<"The product is: "<<a1*a2<<endl;

   cout<<"The Quotient is: "<<a1/a2<<endl;

}

Explanation:

Create the main function and declare the two variables of float but we can enter the integer as well.

display the message on the screen and then store the input enter by the user into the variable using the cin instruction.

the same process for second input as well, display the message and store the input.

after that, print the output of the calculation. the operator '+' is used to add the two numbers like a1+a2, it adds the number stored in the variable.

similarly, the subtraction operator is '-', product '*' and quotient operator '/'.

and finally, we get the desired output.

Quicksort reaches the worst-case time complexity when:Partition is not implemented in place
Picking the largest one of input or partitioned data as pivot value
Median of input or partitioned data is expensive to calculate
Data is sorted already and always pick the median as pivot
Choose the incorrect statement:
When the median is always picked as pivot in input/partitioned data, then quicksort achieves the best-case time complexity.
Mergesort has O(N(log(N)) time complexity for its worst case, average case and best case
Insertionsort reaches its best-case time complexity O(N log(N)) when the input data is pre-sorted
Quicksort is practically fast and frequently used sorting algorithm.
Choose the incorrect statement:
In the lower bound analysis by using decision tree, each branch uses one comparison to narrow down possible cases
In the lower bound analysis by using decision tree, he number of required comparisons can be represented by height of decision tree
A decision tree to sort N elements must have N^2 leaves
O(N log(N)) lower bound means that comparison-based algorithm cannot achieve a time complexity better than O(N log(N))
Choose the incorrect statement regarding time complexity of union-find operation:
Inverse Ackermann function does not depend on N and is a constant factor.
When we use arbitrary union and simple find for union-find operation, the worst-case time complexity is O(MN) for a sequence of M operations and N elements
Union-by-size and Union-by-rank both improve the time complexity to O(M log(N)) for a sequence of M operations and N elements
To finish the entire equivalence class computation algorithm, we need to go over each pair of elements, so if we use union-by-rank with path compression for find operation, then the overall time complexity is O(N^2 log*N), where log*N denotes the inverse Ackermann function.
Choose the incorrect statement regarding Dijstraâs algorithm
Dijstraâs algorithm is a greedy algorithm
Dijstraâs algorithm requires to dynamically update distance/costs/weights of paths.
To begin with, Dijstraâs algorithm initializes all distance as INF
Dijstraâs algorithm can be implemented by heaps, leading to O(|E|+|V| log(|V|)) time complexity, where, particularly, log(|V|) is due to "insert" operation in heaps.

Answers

i) Picking the largest one of input or partitioned data as pivot value.

ii) Insertion sort reaches its best-case time complexity O(N log(N)) when the input data is pre-sorted

iii) A decision tree to sort N elements must have N^2 leaves

iv) Inverse Ackermann function does not depend on N and is a constant factor.

v) Dijstraâs algorithm requires to dynamically update distance/costs/weights of paths.

What is quicksort?

  • Quicksort is an efficient sorting algorithm used to sort items in an array. It is an in-place algorithm, meaning it does not require any additional memory for sorting.
  • The algorithm works by choosing a pivot element from the array and partitioning the array based on the pivot.
  • All elements that are less than the pivot are placed before it, and all elements that are greater than the pivot are placed after it. This is repeated until the array is completely sorted.
  • Quicksort is considered one of the fastest sorting algorithms and is used in many applications, such as sorting data in databases.
  • It is also used in many programming languages, including C, Java, and Python.
  • Quicksort is an example of a divide-and-conquer algorithm, as it splits the array into smaller pieces which are easier to sort.

To learn more about quicksort refer to:

brainly.com/question/29981648

#SPJ4

Other Questions