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:
#include <iostream>
: This includes the standard C++ library for input/output operations.using namespace std;
: This allows the use of standard identifiers without requiring thestd::
prefix.const int ARRAY_SIZE = 10;
: This declares a constant integerARRAY_SIZE
and assigns it the value of10
.Two function prototypes are declared:
seqSearch(const int list[], int listLength, int searchItem)
andfindLargestNumber(int[], int)
.seqSearch
will be defined to perform the sequential search, whilefindLargestNumber
will be defined to find the largest number in the array.In
main()
, an arrayintList
is declared and initialized with ten numbers. An integernumber
is declared for storing the user's input.The program outputs the largest value in the array using
findLargestNumber(intList, ARRAY_SIZE)
.The program then prompts the user to enter a number to search for in
intList
. The user's input is stored innumber
.seqSearch(intList, ARRAY_SIZE, number)
is called, and the return value (the index ofnumber
inintList
if it was found, or -1 if it was not found) is stored inpos
.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. Ifpos
is -1, the number was not found in the array, and a message indicating this is output.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).The function
findLargestNumber
is defined to find the largest number in an array. It initializesmaxValue
to the first element in the array, then iterates through the rest of the array. If it finds an element larger thanmaxValue
, it updatesmaxValue
to this new larger number. Once it has checked every element, it returnsmaxValue
.Finally, the program ends by returning 0, indicating successful execution.
COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark