Searching an Array for a Specific Item

Searching a list for a given item is one of the most common operations performed on a list. The search algorithm we describe is called the sequential search or linear search. As the name implies, you search the array sequentially, starting from the first array element.

// This program illustrates how to use a sequential search in a // program. #include <iostream> using namespace std; //create a Global varibale for the Array size const int ARRAY_SIZE = 10; //prototype my function int seqSearch(const int list[], int listLength, int searchItem); int findLargestNumber(int[], int ); //my Main program int main() { int intList[ARRAY_SIZE] = {10, 23, 2, 32, 45, 12, 4, 17, 100, 83}; int number; cout << "The largest value is " << findLargestNumber(intList, ARRAY_SIZE) << endl; cout << "Enter the number to be searched: "; cin >> number; cout << endl; //using out search function, look through the array to see if we can find the number int pos = seqSearch(intList, ARRAY_SIZE, number); //if we have found if (pos!= -1) cout <<"The number: " << number << " is found at index " << pos << endl; else cout << "The number: " << number << " is not in the list." << endl; return 0; } // Parameters are Array, the Array length, and the item we are searching for //This is a sequential search int seqSearch(const int list[], int listLength, int searchItem) { int indexLocation = 0; bool found = false; //loop through the array and see if we can find a match while ( (indexLocation < listLength) && !found) if (list[indexLocation] == searchItem) found = true; else indexLocation++; if (found) return indexLocation; else return -1; } int findLargestNumber(int pArray[], int size) { int maxValue = pArray[0]; // Initialize the maximum value to the first element for (int i = 1; i < size; i++) { // Start the loop from the second element if (pArray[i] > maxValue) { // Update the maximum value if the current element is greater maxValue = pArray[i]; } } return maxValue; // Return the largest number }

This C++ program demonstrates how to use sequential search within an array. It also finds the largest number within the same array. The user provides a number to be searched within the array, and the program reports whether it is found and at what index.

Here's how it works:

  1. #include <iostream>: This includes the standard C++ library for input/output operations.

  2. using namespace std;: This allows the use of standard identifiers without requiring the std:: prefix.

  3. const int ARRAY_SIZE = 10;: This declares a constant integer ARRAY_SIZE and assigns it the value of 10.

  4. Two function prototypes are declared: seqSearch(const int list[], int listLength, int searchItem) and findLargestNumber(int[], int). seqSearch will be defined to perform the sequential search, while findLargestNumber will be defined to find the largest number in the array.

  5. In main(), an array intList is declared and initialized with ten numbers. An integer number is declared for storing the user's input.

  6. The program outputs the largest value in the array using findLargestNumber(intList, ARRAY_SIZE).

  7. The program then prompts the user to enter a number to search for in intList. The user's input is stored in number.

  8. seqSearch(intList, ARRAY_SIZE, number) is called, and the return value (the index of number in intList if it was found, or -1 if it was not found) is stored in pos.

  9. An if-else statement checks whether pos is -1. If it is not, it means the number was found in the array, and its index is output. If pos is -1, the number was not found in the array, and a message indicating this is output.

  10. The function seqSearch is defined to perform a sequential search through an array for a specified number. It iterates through each element in the array until it finds a match (in which case it returns the index of the matching element) or it reaches the end of the array (in which case it returns -1).

  11. The function findLargestNumber is defined to find the largest number in an array. It initializes maxValue to the first element in the array, then iterates through the rest of the array. If it finds an element larger than maxValue, it updates maxValue to this new larger number. Once it has checked every element, it returns maxValue.

  12. Finally, the program ends by returning 0, indicating successful execution.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark