Sequential Search


Program Listing

SequentialSearch.cpp

/************************************************************************
*                                                                       *
*   Program:    SequentialSearch.cpp                                    *
*                                                                       *
*   Abstract:   This program will perform a sequential search of a      *
*               list for a specified value                              *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

//  Function prototypes

int SequentialSearch(int array[], int size, int value);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To perform a sequential search of a list for a          *
*               specified value                                         *
*                                                                       *
************************************************************************/

int main() {

    const int sentinel = -1;
    const int maxSize = 100;
    
    int array[maxSize];
    
    int size = maxSize;

//  Create an array of random integers

    for (int i = 0; i < size; i++)
        array[i] = rand() % 1000;

//  Print the array

    for (int i = 0; i < size; i++)
        cout << array[i] << ' ';
    cout << endl << endl;

//  Search the array for values given by the user

    cout << "Enter a number to be search for (enter " << sentinel
         << " to quit):  " << flush;
    int value;
    cin >> value;
    while (value != sentinel) {
    
    //  Search the array
    
        int pos = SequentialSearch(array, size, value);
    
    //  Report the results
    
        if (pos >= 0)
            cout << value << " was found in position " << pos << endl;
        else
            cout << value << " was not found" << endl;

        cout << "Enter a number to be search for (enter " << sentinel
             << " to quit):  " << flush;
        cin >> value;
    }
    
    cout << endl << "Good-bye" << endl;
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   SequentialSearch                                        *
*                                                                       *
*   Purpose:    To perform a sequential search of a list for a          *
*               specified value                                         *
*                                                                       *
************************************************************************/

int SequentialSearch(int array[], int size, int value) {
    int i = 0;

//  Search until end of list or value is found

    while (i < size && value != array[i])
        i++;

//  Determine why loop terminated and return appropriate value

    if (i < size)
        return i;
    else
        return -1;
}

Sample Run

838 758 113 515 51 627 10 419 212 86 749 767 84 60 225 543 89 183 137 566 966 978 495 311 367 54 31 145 882 736 524 505 394 102 851 67 754 653 561 96 628 188 85 143 967 406 165 403 562 834 353 920 444 803 962 318 422 327 457 945 479 983 751 894 670 259 248 757 629 306 606 990 738 516 414 262 116 825 181 134 343 22 233 536 760 979 71 201 336 61 160 5 729 644 475 693 514 139 88 521 

Enter a number to be search for (enter -1 to quit):  113
113 was found in position 2
Enter a number to be search for (enter -1 to quit):  88
88 was found in position 98
Enter a number to be search for (enter -1 to quit):  882
882 was found in position 28
Enter a number to be search for (enter -1 to quit):  500
500 was not found
Enter a number to be search for (enter -1 to quit):  -1

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:05:01 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/SequentialSearch.html

Return to Coms 261 home page