This lab will combine reading data from a file and searching the array to find a specific value.
Assignment
Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered.
After looping in main() for the input, write a search function, with the following prototype:
int findWord(string [], int, string);
with arguments as follows:
1) The first argument, a string[] (array), is list of words read from the file.
2) The second argument, an 'int', is the maximum size of the file (define a constant such as MAX_SIZE in main(), not as a global variable, and pass it into this function here).
3) The third argument, a 'string', is requested word to be found.
The return 'int' should be the position in the array where the word appears (starting with 0 for the first word) or the value -1 if the word is not present in the array.
The input file is a list of dictionary words --
dictionary.txt
-- which you
can find in this week's module.
How to Search
In your findWord function, use the linear search algorithm, which starts at the beginning of the array and then steps through the elements sequentially until either the desired value is found or the end of the array is reached.
For example, if the array is:
{ "hello", "goodbye", "sometimes", "never" }
and we want to find the word "sometimes", the algorithm would check each element, starting with "hello", to see if it is equal to the search key. When the search reaches either the found word or the end of the array, the algorithm stops.
Example Output
Enter the word to find (-1 to exit): abacus The word "abacus" is in the file at position 8884. Enter the word to find (-1 to exit): life The word "life" is in the file at position 1. Enter the word to find (-1 to exit): -1
What to Submit
Upload your main.cpp file (source code for your solution) and a screenshot of a single run with at least two lookups and an exit (i.e. entering '-1) to stop the program.
PreviousNext
#include<iostream>
#include<fstream>
#include <bits/stdc++.h>
using namespace std;
int findWord(string arr[], int MAX_SIZE, string word)
{
// Iterate over array
// if current word is equal to target
// return index
for(int i =0; i<MAX_SIZE; i++)
{
if(arr[i]==word)
{
return i;
}
}
// return -1 means word was not found
return -1;
}
int main()
{
// I have defined this 2, you can change it later
int const MAX_SIZE = 20;
fstream fileIn;
string filename = "dictionary.txt";
// Open file to read
fileIn.open(filename.c_str());
// Check if file exists
if(!fileIn)
{
cout<<"File cannot be opened!"<<endl;
return 0;
}
// Create an array of size MAX_SIZE
string arr[MAX_SIZE];
// index variable for array indexing
int index = 0;
string word = "";
// Iterate loop till the last word
while(fileIn>>word)
{
// add word to array
// increment index by 1
arr[index] = word;
index++;
}
string w;
// Ask user to enter a word to search
cout<<"Enter the word to find (-1 to exit): ";
cin>>w;
// Iterate a loop till user doesn't enter -1
while(w!="-1")
{
// call function to find word
// if position is -1, means word does not exists
// else print the location of word
int position = findWord(arr,MAX_SIZE,w);
if(position==-1)
{
cout<<"The word "<<w<<" doesn't exist in
file"<<endl;
}
else
{
cout<<"The word "<<w<<" is in the file at
position "<<(position+1)<<endl;
}
cout<<"Enter the word to find (-1 to exit): ";
cin>>w;
}
return 0;
}
INPUT FILE I USED
amateur
life
soul
sole
math
science
therapy
abacus
mine
toll
love
hate
abuse
marry
lenses
pair
fair
fare
mature
mint
OUTPUT-

This lab will combine reading data from a file and searching the array to find a...
Read a "dictionary" file and store the listed words in an array (not an arraylist) Analyze a given text file "file.txt" and search for every word in "file.txt" in the "dictionary.txt" file using a binary search algorithm. (You may not use the binarySearch method in arrays class) Print words not found in the dictionary as potentially incorrect; count the number of incorrectly spelled/not located words, count the number of correctly spelled words, and count the total number of words. Output...
// This program performs a linear search on a character array // Place Your Name Here #include <iostream> using namespace std; int searchList(char[], int, char); // function prototype const int SIZE = 8; int main() { char word[SIZE] = "Harpoon"; int found; char ch; cout << "Enter a letter to search for:" << endl; cin >> ch; found = searchList(word, SIZE, ch); if (found == -1) cout << "The letter " << ch << " was not found in the...
This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....
You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...
write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...
[C++]
Outline:
The movie data is read from a file into an array of structs and
is sorted using the Selection Sort method and the search is done
using the Binary Search algorithm with the year of release as key.
This assignment upgrades to an array of pointers to the database
elements and converts the Selection Sort and Binary search
procedure on the database to selection sort and binary search on
the array of pointers to the database.
Requirements:
Your...
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...
C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...
Stuck on this computer science assignment
Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o.
Provided files:
Assignment.cpp - starter assignment with function prototypes
companies.txt - file for program to read.
earnings.txt - file for program to read.
Input1.txt
Input2.txt - Test these inputs out manually, or use stream redirection
Input3.txt - These inputs are based...
The function takes three parameters: string array, int arraySize, and string word The function will loop through the string array. If word is found in the array, return the matched element’s position (index) in the array. Otherwise, return -1. When a match is found in the array, the loop should be terminated at the time --- no need to continue the loop. If no match is found, the function should display all elements in the array, as well as the...