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