Question

In: Computer Science

Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...

Programming lang C++

Write a program that reads 10,000 words into an array of strings. The program will then read a second file 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.

Solutions

Expert Solution

//eng.txt -- file that contains 10,000 words here for testing purpose I have taken only 17/18 words

program
will
then
read
a
second
file
that
contains
an
undetermined
number
of
words
and
mister
search
them

------------------------------------

//example.txt ---file containing undetermined no of words

hello
program
and
or
display

--------------------------

//main.cpp

#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;


///function to check if word is present in string array having size 'size'
///return true if found else false
///sequential search needs O(n) time
bool searchWord(string strArray[],int size ,string word)
{

for(int i=0;i<size;i++){

if(word == strArray[i]) ///word found
{
//cout<<"*"<<word<<"*"<<word.size()<<endl;
return true;
}

}
return false;
}


int main()
{

char file1[]="eng.txt"; /// first file name containing 10000 words
char file2[]="example.txt"; /// first file name containing words to be search

string strArray[10000];
string testArray[100]; /// consider MAX 100 words are present
///.. You can change this max value but check your system memory availability
int cnt1=0; /// counter to measure no of words present in first file
int cnt2=0; /// counter to measure no of words present in second file

///reading first file
fstream out;
string line;
out.open(file1,ios::in); ///open in read mode
if(out.is_open()){ ///if file exits
while(!out.eof()){ /// read file line by line
getline(out,line);
if(line.size()>0)
strArray[cnt1++]=line; ///store the line in string array & increment cnt1
}
}
else{
cout<<"\nThe file with name "<<file1<<" not present ";
exit(1);
}
out.close(); ///close file

/*for(int i=0;i<cnt1;i++){
cout<<"\n"<<strArray[i];
}*/

///now read second file

out.open(file2 , ios::in); ///open in read mode

if(out.is_open()){ ///if file exits
while(!out.eof()){ /// read file line by line
getline(out,line);
if(line.size()>0)
testArray[cnt2++]=line; ///store the line in test array & increment cnt1
}
}
else{
cout<<"\nThe file with name "<<file2<<" not present ";
exit(1);
}
out.close(); ///close file

/*
cout<<"\nThe english file contains total "<<cnt1<<" words\n";
cout<<"\nTest array \n\n";

for(int i=0;i<cnt2;i++){
cout<<"\n"<<testArray[i];
}
*/

if(cnt1<=0 || cnt2<=0){
cout<<"\nOne of the given file is empty";
exit(0);
}


cout<<"\nThe test file contains total "<<cnt2<<" words\n";

///Now simply search each word in testArray wrt strArray

int total=0; ///total words that are present in both files

for(int i=0;i<cnt2;i++){
if( searchWord(strArray , cnt1 ,testArray[i]) && testArray[i]!="\n")
total++; ///if word found increment total
}

cout<<"\nThe test file contains "<<total<<" words that are present in both files\n";
if(total<cnt2-1){
cout<<"\nThe test file contains "<<(cnt2-total)<<" words that are not present in first file";
}
return 0;
}

--------------------------------

if same filename is provided then ->

if file2 contains more than file1


Related Solutions

Please in C++ language Write a program that reads 10,000 words into an array of strings....
Please in C++ language Write a program that reads 10,000 words into an array of strings. The program will then read a second file 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.
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those integers into an array and sorts the integers in the array. The program should then prompt the user for an index, and then display the integer at that index. For example, if the array contains ten integers and the contents are 79 4 42 51 12 22 33 17 91 10 after sorting, the integer at index 6 is 42 You are encouraged to...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT