| INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS |
SEARCHING: TO LOOK AND TO FIND
The computer was invented to perform complex mathematical computations, however this trend has changed and the computer has become more of a searching machine (engine). Non-stop, this machine looks to find proper matches, travel destinations, inventories, personal files, or even previous computations. In fact, searching is the center of most computer applications and increasingly dominates all aspects of our home, school, and office applications. Many algorithms have been developed to perform these searches, however, some searching algorithms are easy, some are difficult, and some are faster than others. Among the searching algorithms we can name sequential (linear) search, binary search and hashing. Each of these searches has their own advantages and disadvantages. One can argue that, with the high speed and memory size of todays computer, linear search would be sufficient for relatively small data sets. But, as the amount of data overwhelmingly increases, there is as need to explore alternative algorithms for searching. Hashing performs best in certain situations in comparison to other searching algorithms. Another efficient searching algorithm is known as search tree built on a data structure called tree. The data on a tree is stored in a fashion representing the branches of a tree and a root, interestingly it should be viewed upside down. The tree-searching algorithm can be difficult to understand and program. Understanding and analyzing various searching techniques using criteria such as consumption of computer time and/or space enables one to choose and decide on a proper search technique.
Every search program consists of the following three major steps.
1. Interaction
2. Look at existing information
3. Responding back
In the interaction step, the program asks the user to enter a proper request; and user responds by inputting the data, usually through the keyboard. In the second step, the program will take the requested information (search key) and search through existing information that is stored in a file, array, memory location, or any other data structures. The program then responds back to say whether or not the information was found.
![]()

![]()
SEARCH TO GUESS YOUR MIND
Think of a number and interact with the following program, in a few attempts the program will guess your number. For example, if the guessed number is within range of 1 to 1024, the number will be found it with less than 20 guesses. Similarly for a million numbers it take less than forty guesses. It is possible to optimize the Guess Program by asking questions such as "IS IT LESS OR MORE? L/M ". In addition, the below guess program can be modified to include other subject matters such as guessing an animal, having an intelligent conversation or becoming a diagnostic system detecting an illness.
1. #include <iostream>
2. using namespace std;
3. main(){
4. int low=1, high=1024,mid,guess;
5. char reply;
6. cout<<"\tTHINK OF A NUMBER BETWEEN 1 AND 1024"<<endl;
7. cout<<"\t I WILL FIND IT IN LESS THAN 20 GUESSES"<<endl;
8. for (guess=19;guess>=0;guess--){
9. mid=(low+high)/2;
10. cout<<"IS IT: "<<mid<<" y/n?";
11. cin>>reply;
12. if (reply=='y'){
13. cout<<" I WON"<<endl;
14. return 1; }//IF
15. else {
16. cout<<guess<<" GUESSES LEFT: "<<endl;
17. guess--;
18. cout<<"IS IT LESS:y/n?";
19. cin>>reply;
20. cout<<guess<<" GUESSES LEFT: "<<endl;
21. if (reply=='y')high=mid+1;
22. else low=mid-1;}//ELSE
23. }//FOR
24. cout <<"\t IT IS HARD TO READ YOUR MIND "<<endl;
25. }//MAIN

Figure 12.2b Output of Guess Program
If we divide the range of numbers each time into halves, therefore the order of magnitude would be log2N. Therefore, for the range of 1024 =210, there would be a maximum of 10 guesses and for up to 1,047,576=220, only a maximum of 20 guesses is needed. However, for the program you can have the maximum integer of the system by using
const INT_MAX in the header file #include <limit.h>.
Having a series of data, how do you look for (search) an item? You go through the list of items one by one until you find the item (search succeeded) or until the list is exhausted (search failed). This searching behavior, which sequentially visits each item, is known as sequential or linear search.
LINEAR SEARCH THROUGH A FILE
1. #include<iostream>
2. #include<fstream>
3. using namespace std;
4. main( ){
5. int searchid, empid;
6. cout<<"ENTER THE SEARCH ID: ";
7. cin>>searchid;
8. ifstream fin("employeeid.in");
9. while(fin>>empid){
10. if(empid==searchid){
11. cout<<"EMPLYEE FOUND "<<endl;
12. return 1; }//IF
13. }//WHILE
14. cout<<"NOT FOUND:"<<endl;
15. fin.close();
16. return 0;
17. }//MAIN
![]()


1. #include<iostream>
2. using namespace std;
3. linearsearch(int slot[ ],int searchkey,int n){
4. for(int i=0; i<n;i++) if (searchkey==slot[i]) return i;
5. return -1; }//LINEARSEARCH
6. main(){
7. int slot[]={8,3,1,7,9};
8. const int N=5;
9. int searchkey,index;
10. cout<<"ENTER THE SEARCH KEY:";
11. cin>>searchkey;
12. index=linearsearch(slot,searchkey,N);
13. if (index==-1) cout <<"NOT FOUND"<<endl;
14. else cout<<" FOUND IN LOCATION: "<<index<<endl;
15. return 0; }// MAIN
![]()

Figure 12.4b Output of linear search program
A linear search must search through the entire unsorted data to determine if a match exists. However, if the data are sorted, a linear search will only carry out the searching process as long as the search key is greater than the data.
How does the linear search find the search key 5 from the following sorted data 3, 6, 8, 10, 12?
The search will begin by determining if the search key 5 is greater than the first data 3. If so, the process will continue with the next element of the data until the condition becomes false. There are two possibilities for the condition to become false: one is when the last element of the data is examined and the second is when the search key is equal to the data.
In the following program, the sorted data is stored in an array called slot. To search for an item, the searchkey is compared with each element of the array and the index is incremented as long as searchkey is greater than the current element of the array. The condition of the loop will turn to false when searchkey is no longer greater than slot[i]. The search is successful when the searchkey is equal to slot[i], otherwise the search fails.
1. #include<iostream>
2. using namespace std;
3. main(){
4. int slot[]={1,3,7,8,9};
5. const N=5;
6. int searchkey,i=0;
7. cout<<"ENTER THE SEARCH KEY:";
8. cin>>searchkey;
9. while(searchkey>=slot[i]) {
10. if (searchkey==slot[i]){cout<<" FOUND "<<slot[i]<<endl; return 1;}
11. i++;
12. }//WHILE
13. cout<<" NOT FOUND "<<endl;
14. return 0; }//MAIN
![]()