Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The output should look like this exactly Here is the Liear Search function: int

ID: 3852962 • Letter: T

Question

The output should look like this exactly

Here is the Liear Search function:

int LinearSearch (const string a[], int aSize, string toFind)
{
// Look through all items, starting at the front.
for (int i = 0; i < aSize; i++) {
if (a[i] == toFind)
return i;
}

// You've gone through the whole list without success.
return -1;
}

In this assignment, we will practice using Linear Search for a problem Steps for programming . Create a directory OLA9" under the "OLA" directory, then change directory(cd) into the "OLA9" directory Use scite to write a new program named books.cc; Copy the data file booktitles.dat into your project directory with the command: cp SPUB/booktitles.dat Write a C+ program, named "books.cc": 1·The program reads the titles of a collection of books from a data file and stores them in an array. 2. Then, the program prints out the list of book titles, one title per line, 3. Afterwards, the program goes into a query mode prompting the user to query whether a book is in the collection. If the book is in the collection, it display its location in the collection. If the book is not in the collection, inform the user about that. The query mode ends when the usr enters an empty string, i.c., simply press the "ENTER" key when prompted for book title. Notice: The index of the first element in the array is O. Yet, for the output, the value corresponding to the first book should be 1. Same goes for the location values returned in the query mode

Explanation / Answer

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

int Linear_search(char**,int,char*);

void main(){
   char *books[50],*title;
   int i=0,pos;
   ifstream fin("books.txt");
   while(fin>>title){
       books[i++]=title;
       cout<<i<<" : "<<books[i-1];
   }
   fin.close();
   while(1){
       cout<<" Enter the book title: ";
       cin>>title;
       pos=Linear_search(books,i,title);
       if(pos!=-1)
           cout<<" The book is at location "<<pos;
       else
           cout<<" The book is not in the collection";
   }
}
int Linear_search(char** books,int size,char *key){
   for(int i=0;i<size-1;i++){
       if(books[i] == key)
           return i;
       else
           return -1;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote