Question
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
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started