Question

C++ Write a program that uses an arrays of at least 20 string . It should...

C++

Write a program that uses an arrays of at least 20 string . It should call a function that uses the linear search algorithm to locate one of the name. Read list of name from an input file call "StudentName.txt"

The function should keep a count of the numbers of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the numbers of comparisons it makes. Display these values to a file call "out.txt"

1. Problem statement added to program

2. comments to program   

3. Working program with proper indentation

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

We have to store the input file in bin folder C:\Program Files (x86)\Dev-Cpp MinGW64\bin As I installed Dev C++ in C Drive

// Studentname.txt (input name)

Williams
James
Thomson
Kenedy
Patinson
Benson
Alister
Pointing
Richard
Mike
Jenning
Rob
Bob
Sachin
Anil
Kamble
Dravid
Gangully

====================================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;


int linearSearch(string names[],string search,int cnt,int &comparisionCnt);
void binarySearchlinearSearch(string names[],string search,int cnt,int &comparisionCnt);
int main() {
   //Declaring variables
   const int SIZE=20;
string names[SIZE];
string search;
int cnt=0,comparisionCnt=0;
ifstream dataIn;
ofstream dataOut;
  
dataIn.open("StudentName.txt");
   //checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
   dataOut.open("out.txt");
//Reading the data from the file
while(getline(dataIn,names[cnt]))
{
   cnt++;
   }
   dataIn.close();
  
   cout<<"Enter Name to search :";
   getline(cin,search);
  
   int index=linearSearch(names,search,cnt,comparisionCnt);
   if(index!=-1)
   {
      
       cout<<"No of comparisions in Linear Search :"<<comparisionCnt<<endl;
       comparisionCnt=0;
   binarySearchlinearSearch(names,search,cnt,comparisionCnt);
           cout<<"No of comparisions in Binary Search :"<<comparisionCnt<<endl;
          
           dataOut<<"No of comparisions in Linear Search :"<<comparisionCnt<<endl;
           dataOut<<"No of comparisions in Binary Search :"<<comparisionCnt<<endl;
           dataOut.close();
   }
   else
   {
      
       cout<<search<<" not found in the array"<<endl;
       dataOut<<search<<" not found in the array"<<endl;
           dataOut.close();
   }

}

  
  
  
  
   return 0;
}
int linearSearch(string names[],string search,int cnt,int &comparisionCnt)
{

   for(int i=0;i<cnt;i++)
   {
      
       if(names[i].compare(search)==0)
       {
          
           return i;
       }
       comparisionCnt++;
   }
   return -1;
}
void binarySearchlinearSearch(string names[],string search,int cnt,int &comparisionCnt)
{
   int low = 0;
int flag=0,pos;
int high = cnt-1;
while (high >= low) {
int middle = (low + high) / 2;
if (names[middle].compare(search)==0) {
flag=1;
pos=middle;
break;
}

if (names[middle].compare(search) < 0) {
low = middle + 1;
comparisionCnt++;
}
if (names[middle].compare(search) > 0) {
high = middle - 1;
comparisionCnt++;
}

}

}

======================================

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\LinearSearchBinarySearch.exe Enter Name to search : Kamble No of comparisions in L

==================================

// out.txt (Output file)

out - Notepad File Edit Format View Help No of comparisions in Linear Search :15 No of comparisions in Binary Search :4


=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++ Write a program that uses an arrays of at least 20 string . It should...
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
  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...

  • Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an...

    Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a...

  • HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least...

    HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the...

    This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the first 10 chapters of "starting out with >>>C++ From Control Structures through Objects 9th Edition". I will leave a thumbs up rate if the code works and done quickly. Thank you. Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order....

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

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