Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ and Search.cpp Please. Summary: searching text files for (key, value) pai

ID: 3591391 • Letter: I

Question

In C++ and Search.cpp Please.

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 stringsoften 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 program: words-by-freq.txt headache the meanwhile The first input is the filename, and the remaining inputs are keys to search for. The keys are followed by #, which stops the program. Here's the correct output given this input sequence:

Explanation / Answer

int GetFileStats(string filename,string& firstKey,string& firstValue){

firstKey = "?";

firstValue = "?";

int N = 0;

string kv; // Declaring a string variable to read each line from input file

ifstream input_file (filename.c_str()); // opening input text file. c_str converts string to char* as ifstream expects a const char* as argument

if (input_file.is_open()) // checking if file can be opened

{

while(getline(input_file,kv)) // iterating through each line in the file

{

if(N==0){ // condition to check if it is the first line

firstValue = kv.substr(0,kv.find(" ")); // splitting the line using space as delimiter. find() gives you index of the string given to it.

// i.e. space in our case. substr() returns the string from given start position to end position.

firstKey = kv.substr(kv.find(" ")+1,kv.length()); // similary we get key using find and substr functions.

}

N++; // incrementing N for counting the number of lines.

}

input_file.close();

}

else {

exit (EXIT_FAILURE); // program exits if file cannot be opened

}

return N;

}

bool SearchForKey(string filename, string key,string& value,int& rank){

value = "<<not found>>";

rank = 0;

bool found = false;

string kv; // Declaring a string variable to read each line from input file

int line_num = 1; // declaring an integer to maintain line number for rank

ifstream input_file (filename.c_str()); // opening input text file. c_str converts string to char* as ifstream expects a const char* as argument

if (input_file.is_open()) // checking if file can be opened

{

while(getline(input_file,kv)) // iterating through each line in the file

{

if(kv.substr(kv.find(" ")+1,kv.length())==key){ // getting key value from each line using find and substr and comparing it with given key value to the function.

value = kv.substr(0,kv.find(" ")); // if key is found, then assign its value

rank = line_num; // assign the line number as rank

found = true; // makinf found true

break;

}

line_num++; // incrementing to keep track of line numbers.

}

input_file.close();

}

else {

exit (EXIT_FAILURE);

}

return found;

}