//#include \"stdafx.h\" #include <iostream> #include <iomanip> #include<cstdio>
ID: 3556128 • Letter: #
Question
//#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;
}
How do I create void function from this? Above is my work and I need to continute.
Create a void test function that is to be called from your main function. In this void test function:
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;
}
void test()
{
//Define a local array of 10 integers with the last integer being a -1 to mark the end of the array
//(this is the array with a sentinel marker at the end). Use a variety of values from 1 to 20 in the first 9 positions.
int local_array1[10] = {1,4,5,8,12,15,16,19,17,-1};
//Include a call that verifies that you can find the first entry in the array./
bool is_found = SearchTillSentinel(1,local_array1);
cout << endl;
if(!is_found)
cout << "1 not found in array " << endl;
//Include a call that verifies that you can find the last entry in the array.
is_found = SearchTillSentinel(17,local_array1);
cout << endl;
if(!is_found)
cout << "17 not found in array " << endl;
//Define a second local array of 10 integers using a different variety of values from 1 to 20.
int local_array2[10] = {11,4,5,8,12,15,16,19,17,1};
//Include a call that verifies that you can find the first entry in the array.
is_found = LinearSearch(local_array2, 10, 11);
cout << endl;
if(!is_found)
cout << "11 not found in array " << endl;
//Include a call that verifies that you can find the last entry in the array.
is_found = LinearSearch(local_array2, 10, 1);
cout << endl;
if(!is_found)
cout << "1 not found in array " << endl;
}
//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;
*/
test();
//system ( "Pause" ) ;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.