Question

You will be reading in 3 files in the program. One will contain a list of...

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 readWords function will read in from file “fileName” into the string array “array”. There will be “size” entries in the input file. If the file exists the function will read in the file and return true. If the file does not exist the function will return false.

The linearSearch function will use the linear search to see if “wordToFind” in in the array “words”. The size of the words array is “size”. As with the example in the text book your function will return -1 if the item is not found and the index in the array if it is found.

The binarySearch function will use a binary search to see If “wordToFind” exists in the array “words”. As with the example in the text book your function will return -1 if the item is not found and the index in the array if it is found.

The program should be named “homework1.cpp”.

A template for the assignment is on eLearning. The file of words to be found is named "findWords.txt", there are 20 words in this file. The file of unsorted words is "unsortedWords.txt" and the file of the sorted words is "sortedWords.txt". There are 1000 words in the "unsortedWords.txt" file and there are 1000 words in the "sortedWords.txt" file.

Note: The code in the text book for calculating the middle for the binary search is: middle = (first + last) / 2;

This will cause an overflow error some very big arrays (but not for the program you are writing).

Instead you should use: middle = first + (last - first) / 2;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

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);

int main()

{

string words[20], sortedWords[1000], unsortedWords[1000];

if(!readWords(words, 20, "findWords.txt")){

cout<<"Unable to open file"<<endl;

}

if(!readWords(sortedWords, 1000, "unsortedWords.txt")){

cout<<"Unable to open unsortedWords.txt file"<<endl;

}

if(!readWords(unsortedWords, 1000, "sortedWords.txt")){

cout<<"Unable to open sortedWords.txt file"<<endl;

}

return 0;

}

bool readWords(string array[], int size, string fileName)

{

ifstream in;

in.open(fileName.c_str());

if (in.fail())

return false;

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

cin >> array[i];

return true;

}

int linearSearch(string wordToFind, const string words[], int size)

{

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

{

if (words[i] == wordToFind)

return i;

}

return -1;

}

int binarySearch(string wordToFind, const string words[], int size)

{

int first = 0, last = size - 1;

while (first <= last)

{

int middle = first + (last - first) / 2;

if (words[middle] == wordToFind)

return middle;

if (words[middle] < wordToFind)

first = middle + 1;

else

last = middle - 1;

}

return -1;

}

Add a comment
Know the answer?
Add Answer to:
You will be reading in 3 files in the program. One will contain a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    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....

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

    DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.    Task 1 – Reverse array (3 pts) Given an integer array like: const...

  • SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names...

    SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names as follows: {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",                         "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",                         "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",                         "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",                   "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"}; Write a program to sort and display the names in alphabet order (use selection sort). The program prompts...

  • Write a main function that declares the names, marks, number of elements as well as the...

    Write a main function that declares the names, marks, number of elements as well as the value to be searched for and the index of the returned function calls. Read and display the contents of names and marks. Ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student. Sort the arrays...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • Please help with my C++ homework! 1. The following function is supposed to perform binary search....

    Please help with my C++ homework! 1. The following function is supposed to perform binary search. It has no errors and will execute correctly. int binarySearch(int array[], int size, int value) {    int first = 0,             // First array element        last = size - 1,       // Last array element        middle,                // Mid point of search        position = -1;         // Position of search value    bool found = false;        // Flag    middle = (first + last)...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT