Exercise 2: Re-write the program so that the user can continue to input values t
ID: 648551 • Letter: E
Question
Exercise 2: Re-write the program so that the user can continue to input values
that will be searched for, until a sentinel value is entered to end the program. Should a pre or post test loop be used?
// This program performs a linear search on a integer array
// Place Your Name Here
#include<iostream>
using namespace std;
int searchList(int [], int, int); // function prototype
const int SIZE = 8;
int main()
{
int nums[8] = {3, 6, -19, 5, 5, 0, -2, 99};
int found;
int target;
cout << "Enter a integer to search for:" << endl;
cin >> target;
found = searchList(nums, SIZE, target);
if (found == -1)
cout << "The integer " << target << " was not found in the list" << endl;
else
cout << "The integer " << target <<" is in the " << found + 1 << " position of the list" << endl;
return 0;
}
//*******************************************************************
// searchList
//
// task: This searches an array for a particular value
// data in: List of values in an array, the number of
// elements in the array, and the value searched for
// in the array
// data returned: Position in the array of the value or -1 if value
// not found
//
//*******************************************************************
int searchList(int List[], int numElems, int value)
{
for (int count = 0;count <= numElems; count++)
{
if (List[count] == value)
// each array entry is checked to see if it contains
// the desired value.
return count;
// if the desired value is found, the array subscript
// count is returned to indicate the location in the array
}
return -1; // if the value is not found, -1 is returned
}
Explanation / Answer
#include<iostream>
using namespace std;
int searchList(int[], int, int); // function prototype
const int SIZE = 8;
int main()
{
int nums[8] = { 3, 6, -19, 5, 5, 0, -2, 99 };
int found;
int target;
int sentinal;
cout << "Please choose a sentinal value: ";
cin >> sentinal;
while (true){
cout << "Enter a integer to search for(enter sentinal value to exit):" << endl;
cin >> target;
if (target == sentinal){ // if user enters a sentinal value break out of the loop
break;
}
found = searchList(nums, SIZE, target);
if (found == -1)
cout << "The integer " << target << " was not found in the list" << endl;
else
cout << "The integer " << target << " is in the " << found + 1 << " position of the list" << endl;
}
return 0;
}
//*******************************************************************
// searchList
//
// task: This searches an array for a particular value
// data in: List of values in an array, the number of
// elements in the array, and the value searched for
// in the array
// data returned: Position in the array of the value or -1 if value
// not found
//
//*******************************************************************
int searchList(int List[], int numElems, int value)
{
for (int count = 0; count < numElems; count++) // must be count < numElements(array index numElems is invalid)
{
if (List[count] == value)
// each array entry is checked to see if it contains
// the desired value.
return count;
// if the desired value is found, the array subscript
// count is returned to indicate the location in the array
}
return -1; // if the value is not found, -1 is returned
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.