// FILE: LnSearch.cpp // SEARCHES AN INTEGER ARRAY FOR A GIVEN ELEMENT (THE TARGET) int lin_search (const apvector & items, // IN: the array being searched int target) // IN: the target being sought // Array elements ranging from 0 to length() - 1 are searched for // an element equal to target. // Pre: The target and array are defined. // Post: Returns the subscript of target if found; // otherwise, returns -1. { // Local data ... int next; // index of the current score next = 0; while (next < items.length()) //Invariant: Target was not found in subarray a[0] through a[next-1] // and next is less than or equal to items.length(). if (items[next] == target) return next; else next++; return -1; // Target not found } // end lin_search