Practices
  Home arrow Practices arrow Page 2 - Basic Array Searching in C++
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

Basic Array Searching in C++
By: Bryan Roth
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2004-12-27

    Table of Contents:
  • Basic Array Searching in C++
  • Sequential Search
  • Binary Search
  • Coding a Binary Search

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Basic Array Searching in C++ - Sequential Search


    (Page 2 of 4 )

    The sequential search is best used if the array you are searching is unsorted. This method of searching is usually used on small arrays of less than 16 elements. We start the sequential search by first declaring a target to be found. The search initiates at the beginning of the array until it finds the target. 

    In the following example we will find a target value of 23 within a one dimensional array. At index 0, 32 is not equal to 23 so we proceed on to the next element.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    At index 1, 431 is not equal to 23 so we proceed.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    At index 2, -34 is not equal to 23 so we proceed.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    Finally at index 3, 23 is equal to 23 and we have found our target.

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    32

    431

    -34

    23

    12

    Now we will implement this example of a sequential search into C++ code. The program below asks the user for a target to be found, then uses a for loop to analyze each element of the array. If the array element is equal to the target it will display that the target was found. Whenever a target is found the variable “flag” will be incremented by 1. At the end of the program if the variable “flag” is less than one, then the target was obviously not found.

    #include <iostream>
    using namespace std;

    int main()
    {
      const int arraySize = 5;
      double target;
      int array[arraySize] = {32, 431, -34, 23, 12};
      int flag;

      // flag is used to log how many times the target is encountered.

      flag = 0;

      cout << "Enter a target to be found: ";
      cin >> target;

      for(int cntr = 0; cntr < arraySize; cntr++)
      {
        if(array[cntr] == target)
        {
    cout << "Target found in array index " << cntr << "."
         << endl;

          flag += 1;
        }
      }
     
      // Test to see if target was found.

      if(flag < 1)
      {
        cout << "Target not found." << endl;
      }

      return 0;
    }

    The sequential search does have a pitfall. It is very slow and its performance rating is low. If a person had an array of one million elements, that would mean there could be up to one million comparisons, and that takes time! The sequential search method would be advisable to use only if the array you were searching was unsorted and small.

    More Practices Articles
    More By Bryan Roth


       · A good refresher on basic array searching, but there are some flaws. Can you fix...
       · Hey Anonymous Loozah,1. I realized that the table examples where the same over...
       · Thanks. I'm looking forward to your next article.
       · on the one hand this is a useful article, on the other, anyone who doesn't know how...
       · Thanks for the quick editing.
     

       

    PRACTICES ARTICLES

    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas
    - Choosing the Right Team
    - Trees





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway