Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that reads 1 0 , 0 0 0 words into an array of strings; the List of 1 0 , 0 0

Write a program that reads 10,000 words into an array of strings; the "List of 10,000 Random Words" file is on Moodle. The program will then read a second file (the list of "Search Words" is also on Moodle) that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list. Do not use an array for the second file, process the words as they are read. Note that some of these "words" might have spaces; handle your input accordingly.
I keep getting 348 words, but that is not right. What part of my code is incorrect? The Int main() is underlined in green sayingthe bytes of stake exceeds the limit. How do I make it bigger?
Here is my code:
#include
#include
#include
using namespace std;
const int NUM_WORDS =10000; // number of words in the first array
int main(){
// declare and initialize the first array with the 1000 words
string words[NUM_WORDS];
ifstream infile("10000RandomWords.txt");
if (!infile.is_open()){
cout << "Error: could not open 1000RandomWords.txt"<< endl;
return 0;
}
for (int i =0; i < NUM_WORDS; i++){
infile >> words[i];
}
infile.close();
// open the second file and read the words one by one
ifstream searchfile("Search Word.txt");
if (!searchfile.is_open()){
cout << "Error: could not open SearchWords.txt"<< endl;
return 0;
}
string searchword;
int count =0; // counter for the number of words found
while (searchfile >> searchword)
{
// remove leading and trailing white space characters from searchword
searchword.erase(0, searchword.find_first_not_of("\t"));
searchword.erase(searchword.find_last_not_of("\t")+1);
// search the first array for the current search word
for (int i =0; i < NUM_WORDS; i++){
if (searchword == words[i]){
// if the search word is found, increment the counter
count++;
// break; // exit the loop
}
}
}
searchfile.close();
// print the number of words found
cout << "Number of words found: "<< count << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions