Question
Here's the link for the .txt files if needed: https://www.dropbox.com/sh/h2y27kbuytaz5da/AAAnwCbmvLNnzAMFen4EpKfKa?dl=0 Here's the main.cpp that can't be changed: /*main.cpp*/ #include #include #include #include using namespace std;
Here's the link for the .txt files if needed: https://www.dropbox.com/sh/h2y27kbuytaz5da/AAAnwCbmvLNnzAMFen4EpKfKa?dl=0
Here's the main.cpp that can't be changed:
/*main.cpp*/
#include
using namespace std;
// // function declarations: // int GetFileStats(string filename, string& firstKey, string& firstValue); bool SearchForKey(string filename, string key, string& value, int& rank);
// // main: // int main() { string filename; cin >> filename; cout > key; while (key != "#") { found = SearchForKey(filename, key, value, rank); if (found == true) { cout >Found key: " >Not found: " > key; } cout
Here's the search.cpp that needs to be changed for the assignment:
/*search.cpp*/
#include
using namespace std;
// // GetFileStats // // Returns the # of (key, value) pairs in the given file. Also returns the first (key, value) // pair via the parameters firstKey and firstValue. // // NOTE: it is assumed the file contains at least one (key, value) pair, in this format: // // value key // value key // value key // # # // // where value and key are one word strings (or numbers that will be input as strings). // If the file cannot be opened, the program will be exited. // int GetFileStats(string filename, string& firstKey, string& firstValue) { // // Initialize return value / parameters: // firstKey = "?"; firstValue = "?"; int N = 0; // // TODO: open and read the (key, value) pairs from the file. The goal is // to simply count how many pairs there are and return this value N. Also, // we need to return the first (key, value) pair via the firstKey and // firstValue parameters; just assign to the parameters to "return" the // values. Don't forget to close the file when you're done. // return N; }
// // SearchForKey // // Opens the given file, searches for the given key, and if found, returns true; if the // key is not found then false is returned. // // If the key is found, the value will be returned via the "value" parameter; likewise // the rank will be returned via the "rank" parameter. The rank is simply the line # // where the (key, value) pair was found: 1 for first line, 2 for second line, etc. // If the key is not found, value and rank should be ignored. // // NOTE: it is assumed the file contains at least one (key, value) pair, in this format: // // value key // value key // value key // # # // // where value and key are one word strings (or numbers that will be input as strings). // If the file cannot be opened, the program will be exited. // bool SearchForKey(string filename, string key, string& value, int& rank) { // // initialize return value / parameters: // value = ">"; rank = 0; bool found = false; // not found: // // TODO: search for the key, and if found, we want to return the associated value. // But we also need to return the "rank" via the rank parameter; the rank is the // line number where the (key, value) appears: 1 for first line, 2 for second // line, and so on. Finally, return true if key was found, false if not. // // Don't forget to close the file when you're done. // return found; }
Summary: searching text files for (key, value) pairs, e.g. movies and their total revenue. Google collects all sorts of interesting data: what words appear most frequently in searches? What web sites are most commonly searched using Alexa? What are the most popular baby names? For example, the data file 'words-by-freq.txt ranks the English words used most commonly in searches. Here's the start of the file: 5627187200 the 3395006400 of 2994418400 and 2595609600 to The words are listed by rank, so the is the most frequently searched word with a frequency of "5627187200". The 2nd most frequently searched word is "of, with a frequency of "3395006400", and so on. The file ends with # and #. In general, we call this type of file a "(key, value)" file, since the data comes in pairs, and the "key" uniquely identifies the 'value. Note that the value is on the left, and the key is on the right. This implies you should view these types of files like this value key value key value key Note that you should view the values and keys as strings-often the values are integers and the keys are strings, but it's dangerous to assume this is always true. Instead, treat all input data as strings, and use string variables. In this exercise, you're going to write a complete C++ program that searches data from a (key, value) input file, outputting some statistics, values, and ranks. The main0 program is written for you (and cannot be modified); your job is to implement the 2 functions in "search.cpp" that do the work of opening, inputting, and searching the input file. First, here's an example input sequence for the programStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started