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;
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.