C++: Open two input files and one output file. Input files unsorted_dictionary.t
ID: 3589306 • Letter: C
Question
C++:
Open two input files and one output file.
Input files
unsorted_dictionary.txt - this file contains 16000 words
keywords.txt - this file contains 84 words
Read in these two files and store their contents in 2 arrays of strings. Do not use a vector.
Sort both arrays using either a selection sort, bubble sort or an insertion sort. Use the same sort function for sorting both arrays. Do not use the STL sort algorithm or quick sort.
Search the dictionary array for each keyword. If the keyword is not present in the dictionary array, write a message to the output file that the keyword is not found in the dictionary (see sample output below). Count the number of keywords not found. You will print this number at the end of the program.
for the fales just put raandom names and random keys and i'll that out
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
#include<string.h>
#include<sstream>
using namespace std;
void swap(string a, string b)
{
string tmp = a;
a = b;
b = tmp;
}
// A function to implement bubble sort
void bubbleSort(string arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
//tokenizer
vector<string> split(const string &s, char delim) {
stringstream ss(s);
string item;
vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
//search in dictionary
bool search(string dictionary[], int n, string keyword)
{
for (int i = 0; i < n; i++)
{
if (dictionary[i] == keyword)
return true;
}
return false;
}
int main()
{
string line;
ifstream myfile("unsorted_dictionary.txt");
ifstream keywords("keywords.txt");
//output file
ofstream out;
out.open("output.txt");
string dictionaryArray[16000];
string keywordArray[84];
int i = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << ' ';
vector<string> tokens = split(line, ' ');
for (auto it = tokens.begin(); it != tokens.end(); it++)
{
dictionaryArray[i++] = *it;
}
}
myfile.close();
}
else
cout << "Unable to open file";
i = 0;
if (keywords.is_open())
{
while (getline(keywords, line))
{
cout << line << ' ';
vector<string> tokens = split(line, ' ');
for (auto it = tokens.begin(); it != tokens.end(); it++)
{
keywordArray[i++] = *it;
}
}
myfile.close();
}
else
cout << "Unable to open file";
//sort both array
bubbleSort(dictionaryArray, 16000);
bubbleSort(keywordArray, 84);
int countOfKeywordNotFound = 0;
for (int j = 0; j < 84; j++)
{
if (!search(dictionaryArray, 16000, keywordArray[j]))
{
out << keywordArray[j] << " the keyword is not found in the dictionary" << endl;
countOfKeywordNotFound++;
}
}
cout << "Number of keyword not found in dictionary: " << countOfKeywordNotFound << endl;
system("pause");
return 0;
}
//unsorted_dictionary.txt
//keywords.txt
//output.txt
Please test this program with input files given to you. Please let me know in comment for any queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.