Question

In: Computer Science

C++ For this assignment, you will create a search benchmark, that is a comparison between the...

C++

For this assignment, you will create a search benchmark, that is a comparison between the Linear and Binary Search algorithm on the same list of names provided in a text file.

1. Ask the user to enter a filename
2. Ask the user to enter a name; this is the search item.
3. Loop until you reach the end of the file and read all the names and store them in an array. Ensure that the array is large enough, for now a size of 50 is good.

i) Make sure that you do not occur an out of bounds error on the array.

ii) You can convert the case of the names in the file as soon as you read it and store it in an array.
4. Your program should call a function that uses the Linear Search algorithm to locate the search item.

i) Make sure you make your comparisons case insensitive. That is, convert the user entered name to upper case for the assignment (converting to lower case will also result the same but I am testing with upper case values) before any comparison.

ii) Your function should keep a count of the number of comparisons it makes until it finds the value or reaches the end of the array.
5. Your program should then call a function that uses the Binary Search algorithm to locate the same value. It should also keep count of the number of comparisons it makes.
6. Display the number of comparisons that both the algorithms make.

Hints:

  • Even though, for now the 'names.txt' only contains first names, don't forget to use getline() to read the names both from the file and from the standard input device.
  • getline(inFile, name) will read from the file you opened and attached to inFile and store the result in the string variable name. The string variable can be an array element as well.
  • You can use the following code to convert the strings to all uppercase. Write a function convertToUpperCase(string &) that takes a string value by reference and converts it to uppercase using the following code:
//string userInput contains the user's input or a name read from the file
for(int index = 0; index < userInput.length(); index++)
{
if(userInput[index] >= 'a' && userInput[index] <= 'z')
userInput[index] = toupper(userInput[index]);
}
cout << "uppercase is: " << userInput << endl;
  • Other functions that you need to write should have the following prototypes:
    • int getFileInput(string [], int, string): the arguments are the string array, the size of the array and the user input (search item that you need to search for in the array).
    • int linearSearch(const string [], string, int, int &): the arguments are the string array, search item, the size of the array and the number of comparisons passed by reference.
    • int binarySearch(const string [], string, int, int &): the arguments are the string array, search item, the size of the array and the number of comparisons passed by reference.

Example 1: Your program should start by getting the file name from the user, followed by a name as user input.

Enter the file name: names.txt 
Enter name to search:

Let's say the user enters 'Sarah' as input then the output should be the following:

Enter name to search: Sarah

Using Linear Search, SARAH was found in the array in 32 comparisons.
Using Binary Search, SARAH was found in the array in 9 comparisons.

Example 2:

Enter name to search: Amy

Using Linear Search, AMY was found in the array in 2 comparisons.
Using Binary Search, AMY was found in the array in 9 comparisons.

Example 3:

Enter name to search: Virginia

Using Linear Search, VIRGINIA was found in the array in 35 comparisons.
Using Binary Search, VIRGINIA was found in the array in 13 comparisons.

Example 4:
If at any time the user enters a file name that doesn't exist, or your program is unable to open the file, you should display an error message. For example, if input.txt file doesn't exist, the output should be the following.

Enter file name: input.txt
Error!! Could not open file!

Solutions

Expert Solution

Program:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

//Function to convert the strings to all uppercase
void convertToUpperCase(string &str)
{
for(int i=0; i<str.size(); i++)
{
if(islower(str[i]))
str[i] = toupper(str[i]);
}
}

//Function to read the names from the file
void getFileInput(string nameArray[], int &n, string fname)
{
ifstream ifs;
ifs.open(fname.c_str());

if(!ifs){
cout<<"Error! Could not open file!";
exit(1);
}

for(n=0; getline(ifs, nameArray[n]) ; n++)
{
convertToUpperCase(nameArray[n]);
}
}

//Function to search a name using linear searching
void linearSearch(const string nameArray[], string name, int n, int &com)
{
for(com=0; com<n; com++){
if(nameArray[com]==name)
break;
}
com++;
if(com==n+1) com = -1;
}

//Function to search a name using binary searching
void binarySearch(const string nameArray[], string name, int n, int &com)
{
int i=0, j=n-1;
com = 0;

while(i<=j)
{
int mid = (i+j)/2;
com++;
if(nameArray[mid]==name)
return;
if(nameArray[mid]>name)
j=mid-1;
else
i=mid+1;
}
}


//main function
int main()
{
string fname, name, nameArray[50];
int n, m;

cout<<"Enter a file name: ";
cin>>fname;

getFileInput(nameArray, n, fname);

cin.ignore();

cout<<"Enter name to search: ";
getline(cin, name);

convertToUpperCase(name);

linearSearch(nameArray, name, n, m);

if(m==-1)
{
cout<< "Error: Not found!" <<endl;
return 0;
}

cout<<"Using linear search, " << name << " was found in the array in " << m << " comparisons." <<endl;

binarySearch(nameArray, name, n, m);

cout<<"Using binary search, " << name << " was found in the array in " << m << " comparisons." <<endl;

return 0;
}

names.txt

Al Clark
Angela Klein
Ashton Willams
Jerry Smith
Joan Garcia
Joyce Itin
Leslie McHenry
Mark Armstrong
Mary Jones
Melody Jung
Morgan Stock
Nick Lee
Robert Innes
Vahid Hamidi
Vera Moroz

Output:

Run1:

Enter a file name: name.txt
Error! Could not open file!

Run2:

Enter a file name: names.txt
Enter name to search: susan
Error: Not found!

Run3:

Enter a file name: names.txt
Enter name to search: nick lee
Using linear search, NICK LEE was found in the array in 12 comparisons.
Using binary search, NICK LEE was found in the array in 2 comparisons.


Related Solutions

In this assignment you will create a Binary Search Tree to storeand retrieve objects of...
In this assignment you will create a Binary Search Tree to store and retrieve objects of type ItemType. The purpose of this assignment is for you to become familiar with basic tree operations, and understand the efficiency of trees compared to previously studied data structures. Binary Tree nodes have only two children, left and right. Nodes are compared based on their Key instance variable, which in this assignment is of type ItemType. All elements in the left subtree of a...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
ORGANIZATIONAL BEHAVIOR AND MANAGEMENT Benchmark Information This benchmark assignment assesses the following programmatic competencies: BS in...
ORGANIZATIONAL BEHAVIOR AND MANAGEMENT Benchmark Information This benchmark assignment assesses the following programmatic competencies: BS in Accounting; BS in Business Administration; BS in Business Analytics; BS in Business Information Systems; BS in Business Management; BS in Entrepreneurial Studies; BS in Finance; BS in Finance and Economics; BS in Hospitality Management; BS in Marketing and Advertising; BS in Sports Management; BS in Supply Chain and Logistics Management 1.1: Demonstrate skills for effective collaboration, negotiation, and teamwork. BS in Applied Management 1.4:...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
How would I create a program in C++ where you build and maintain two binary search...
How would I create a program in C++ where you build and maintain two binary search trees of information? I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations: L -- for launching a satellite, which will save it's info to the first set or D -- for deorbit the satellite. The other operations I'm looking to implement are: F -- for find, where user...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT