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

C++: Open two input files and one output file. Input files: unsorted_dictionary.

ID: 3590318 • 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.

Sample output:

keyword not found: alignas
keyword not found: alignof
keyword not found: and_eq
keyword not found: asm
keyword not found: bitand
keyword not found: bitor
keyword not found: char16_t
keyword not found: char32_t
keyword not found: compl
keyword not found: const_cast

...
words not found: 23

okay so for the text files since they are huge just write to text file like put anything in them and i'll try them with the regular file here.

Explanation / Answer

#define FILE_DICTIONARY "unsorted_dictionary.txt"

#define FILE_KEYWORDS "keywords.txt"

#define DICTONARY_SIZE 16000

#define KEYWORD_SIZE 8

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

bool Search(string arrDictionary[DICTONARY_SIZE], int iNoOfWords, string strKeyword)

{

for (int i = 0; i < iNoOfWords; i++)

{

if (strKeyword == arrDictionary[i])

{

return true;

}

}

return false;

}

int main()

{

int iNoOfKeywords = 0;

int iNoOfWords = 0;

string arrDictionary[DICTONARY_SIZE];

string arrKeywords[KEYWORD_SIZE];

ifstream fileDictionary(FILE_DICTIONARY);

if (!fileDictionary)

{

cout << "Error opening unsorted_dictionary.txt" << endl;

system("pause");

return -1;

}

ifstream fileKeywords(FILE_KEYWORDS);

if (!fileKeywords)

{

cout << "Error opening keywords.txt" << endl;

system("pause");

return -1;

}

// read dictonary.

int iIndex = 0;

while (!fileDictionary.eof() && iIndex < DICTONARY_SIZE)

{

getline(fileDictionary, arrDictionary[iIndex], ' ');

//cout << arrDictionary[iIndex] << " ";

iIndex++;

}

iNoOfWords = iIndex;

// sort dictonary.

for (int i = 0; i < iNoOfWords - 1; ++i)

{

for (int j = 0; j < iNoOfWords - i - 1; j++)

{

if (arrDictionary[j] > arrDictionary[j + 1])

{

std::swap(arrDictionary[j + 1], arrDictionary[j]);

}

}

}

// read keywords.

iIndex = 0;

while (!fileKeywords.eof() && iIndex < KEYWORD_SIZE)

{

getline(fileKeywords, arrKeywords[iIndex], ' ');

//cout << arrDictionary[iIndex] << " ";

iIndex++;

}

iNoOfKeywords = iIndex;

// sort keywords.

for (int i = 0; i < iNoOfKeywords - 1; ++i)

{

for (int j = 0; j < iNoOfKeywords - i - 1; j++)

{

if (arrKeywords[j] > arrKeywords[j + 1])

{

std::swap(arrKeywords[j + 1], arrKeywords[j]);

}

}

}

iIndex = 0;

while (iIndex < iNoOfKeywords)

{

bool boRet = Search(arrDictionary, iNoOfWords, arrKeywords[iIndex]);

if (true == boRet)

{

cout << "keyword found : " << arrKeywords[iIndex] << " ";

}

else

{

cout << "keyword not found : " << arrKeywords[iIndex] << " ";

}

iIndex++;

}

}

Above code tested on following files:

"unsorted_dictionary.txt"

I

love

my

Country

India

is

Special.

1

2

3

6

9

"keywords.txt"

I

love

my

Country

India

Is

Special.

Test1

Test2

3

Output:-

keyword found : Country

keyword found : I

keyword found : India

keyword not found : Is

keyword found : Special.

keyword not found : Test1

keyword found : love

keyword found : my

Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote