Thread: Search
View Single Post
Old 10-06-2004, 03:21 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Revamped quite a bit.

Okidoky.
Implementing so many details in the main() function makes code very hard to read. C++ (just like C) has the disadvantage that it is hard to read (for those who didn't write the code) anyway. So what I did is basically wrote your code from scratch, without looking at your code. The first tangible result is that I need much less variables to start with. But more importantly, the complete application has become much more readable. Readable in the sense that I've split all the details in their own specialized functions. If a function meets the requirements, then that function doesn't need any attention in the future.

What you should be aware of is that my binary and linear search methods are suitable for both multiple searches (in a loop) and for single searches. No need to program almost-identical algorithms this way.
The second major thing that I did is to use std::sort(). Note that factors regarding sorting algorithms was *not* one of the requirements as you posted. So lets use std::sort() since it is there for us already. Obviously I needed to add the <algorithm> header.

Another thing what I did, is using int main() ... return 0; instead of void main(). Lets try to do the very basics right from the beginning. It is only 2 seconds more work :).
However, I've decided to usesystem("PAUSE"); as an optional anti-console-flash method. Change it yourself into a correct method. Notice how I am an anti system( ) person. Lets try to do the very basics right from the start, like I mentioned earlier :)

The code follows below. I think you'll undestand it. But feel free to ask about it any time:
Code:
/*** main.cpp ***/ #include <iostream>
#include <cstdlib> //system("PAUSE") #include <algorithm> //std::sort() using namespace std;

//Globals are dangerous. But for entry level study it will do. void swap2(int&m, int&n);
void displaySearchRequest(unsigned);
int find_first_linear(int*, int, int);
int find_first_binary(int*, int, int);
void displayArray(int*, int);

int linLogicPos[5];
int binLogicPos[5];
int searchValue[5];

//Lets keep the amount of numbers to be found in a constant. Change this value //only if you would like to change the amount of numbers to be found. const unsigned amount = 2;

int main()
{
   int theArray [10]={ 40,60,30,70,90,80,5,25,95,100 };
  int size = sizeof(theArray) / sizeof(*theArray);
  cout<<"Initial array:\t";
  displayArray(theArray, size);
   cout<<endl<<endl;
   
   //Engage menu to ask for number(s) to be found.
   displaySearchRequest(amount);
   //Find by Linear. for(int i = 0; i < amount; ++i)
      linLogicPos[i] = find_first_linear(theArray, searchValue[i], size);

   //Find by Binary, but we need to sort first. //Lets use the STL sort routine since it is not relevant to the analisys. //You could always implement your own sort routine if needed.
  sort(theArray, theArray+10);
  for(int i = 0; i < amount; ++i)
      binLogicPos[i] = find_first_binary(theArray, searchValue[i], size);
   
   //Display the final results.
   cout<<"VALUE\tLINEAR\tBINARY"<<endl;
   for (int i = 0; i < amount; ++i)
      cout<<searchValue[i]<<"\t"<<linLogicPos[i]<<"\t"<<binLogicPos[i]<<endl;

  system("PAUSE");
  return 0;
}

/*=========================================================*/ void displayArray(int* array, int size)
{
   for (int k = 0; k < size; ++k)
      cout<<array[k]<<" ";
}

/*=========================================================*/ // k = amount of numbers to search for. void displaySearchRequest(unsigned k)
{
   for(int i = 0; i < k; ++i)
   {
      cout<<"Please enter an integer: ";
      cin>>searchValue[i];
   }
}

/*=========================================================*/ int find_first_linear(int* theArray, int x, int size)
{
   int pos(-1);
   for ( register unsigned k = 0; k < size; ++k)
      if( x == theArray[k] )
         { pos = k; break; }

   //Return logical position of found value (-1 if not found). return pos;
}

/*=========================================================*/ int find_first_binary(int* a, int x, int size)
{
   int low = 0, high = size - 1;
   while( low <= high )
   {
      int mid = ( low + high ) / 2;
      if( a[mid] < x )
            low = mid + 1;
      else if( a[mid] > x )
            high = mid - 1;
      else
      { return mid; }//Found.
   }
   { return -1;}  //Not found.
}
__________________

Last edited by Valmont; 10-06-2004 at 03:47 PM.
Valmont is offline   Reply With Quote