Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re-write this program so that it searches an array of integers rather than chara

ID: 3548091 • Letter: R

Question

Re-write this program so that it searches an array of integers rather than characters.


// This program performs a linear search on a character array
// Place Your Name Here
#include <iostream>
using namespace std;
int searchList(char[], int, char); // function prototype const int SIZE = 8;
int main()
{
char word[SIZE] = "Harpoon";
int found;
char ch;
cout << "Enter a letter to search for:" << endl;
cin >> ch;
found = searchList(word, SIZE, ch);
if (found == -1)
cout << "The letter " << ch
<< " was not found in the list" << endl;
else
cout << "The letter " << ch <<" 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(char List[], int numElems, char 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 ;

int main()

{

int size,ar[100],i,found,srchElem;

cout<<"Enter size of list :"<<endl;

cin>>size;

cout<<"Enter elements of list :"<<endl;

for(i=0;i<size;i++)

{

cin>>ar[i];

}

cout << "Enter value to search for:" << endl;

cin >> srchElem;

found = searchList(ar, size, srchElem);

if (found == -1)

cout << "The value " << srchElem

<< " was not found in the list" << endl;

else

cout << "The value " << srchElem <<" is in the " << found + 1

<< " position of the list" << endl;

return 0;

}


int searchList(int List[], int numElems, int value)

{

for (int count = 0; count <= numElems; count++)

{

if (List[count] == value)

{

return count;

}

}


return -1; // if the value is not found, -1 is returned

}