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

You will create two Boolean array search functions. These functions will do a li

ID: 3555921 • Letter: Y

Question

You will create two Boolean array search functions. These functions will do a linear search of the array to see if the passed in integer value is present anywhere in an array of integers. This array is also passed to these search functions. A linear search means that your loop starts with the first entry in the array (at index position 0) and continues to the next array position until it reaches the end of the array or until a match is found.

One search function will use a sentinel to mark the end of the array. The other search function will use an array length (maximum legal index into the array) to determine when to exit the search loop. Both functions must return true when the search is successful and false when the value was not found.

The search-until-sentinel function searches the array for the value or until it sees the sentinel marker. This function has one int parameter that contains the value to be searched for (ValueToFind) and one int array containing the array of integers to be searched (DataArray). This DataArray is an array of integers with the sentinel at the end. The sentinel value is a -1 in the last position of the integer array.

The other search-until-last-index function searches the array for a specific number of positions. It has an int parameter (ValueToFind), another int parameter (LastValidIndex), and the array of integers (DataArray). This array of integers does not have a sentinel at the end. Instead, the loop searches until either ValueToFind is found or until we have searched the last position in the array as defined by LastValidIndex. LastValidIndex is the index in the array of the last entry in the array.

My work so far is:

#include "stdafx.h"

#include <iostream>

#include <iomanip>

using namespace std;

bool LinearSearch (int myarray[], int size, int lookfor ) // # looking for, size of the array, array looking in

{

int index;

for (index=0, index < size; index++;)

{

if (myarray[index] = lookfor)

{

printf("%d Number is present ");

return true;

}

}

return false;

}

bool SearchTillSentinel (int ValueToFind, int DataArrayWithSentinel[])

{

int index;

while (DataArrayWithSentinel[index] != -1)

{

if (DataArrayWithSentinel[index] = ValueToFind)

{

printf("%d Number is present ");

return true;

}

index++;

}

}

int _tmain(int argc, _TCHAR* argv[])

{

int SearchTillSentinel[] = {1,2,3,4,5,6,7,8,9,-1};

int LinearSearch[] = {11,12,13,14,15,16,17,18,19,20};

system ( "Pause" ) ;

return 0;

}

Explanation / Answer

//#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include<cstdio>
using namespace std;
bool LinearSearch (int myarray[], int size, int lookfor ) // # looking for, size of the array, array looking in
{
int index;
for (index=0; index < size; index++)
{
if (myarray[index] == lookfor)
{
printf("%d Number is present ",lookfor);
return true;
}
}
return false;
}
bool SearchTillSentinel (int ValueToFind, int DataArrayWithSentinel[])
{
int index =0;
while (DataArrayWithSentinel[index] != -1)
{
if (DataArrayWithSentinel[index] == ValueToFind)
{
printf("%d Number is present ",ValueToFind);
return true;
}
index++;
}
return false;
}
//int main(int argc, _TCHAR* argv[])
int main()
{
int array1[] = {1,2,3,4,5,6,7,8,9,-1};
if(!SearchTillSentinel(7,array1))
cout << "7 not found in array " << endl;
if(!SearchTillSentinel(77,array1))
cout << "77 not found in array " << endl;
int array2[] = {11,12,13,14,15,16,17,18,19,20};
if(!LinearSearch(array2, 10,15))
cout << "15 not found in array " << endl;
if(!LinearSearch(array2, 10,155))
cout << "155 not found in array " << endl;

//system ( "Pause" ) ;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote