Searching Through an Unsorted List of Integers

list - the list to be searched
item - the item we're looking for
length - the number of elements in the list
index - the location of the item (if found)
found - flag indicating if item was found
void Search(const int list[],
            int item,
            int length,
            int& index,
            Boolean& found)
{
  index = 0;
  while (index < length && item != list[index])
    index++;

  found = (index < length);

}
You would use the function something like this:
int index;
Boolean found;

Search(list, 14, 15, index, found);

if (found)
  cout << "14 was found at index: " << index;
else
  cout << "14 was not found";

A picture of an unsorted list is here.

Previous page
Next page

Back to Lesson 2 Index
Back to Outline