Linear Search

1 / 16

# Linear Search - PowerPoint PPT Presentation

Linear Search. Is a value in the array?. void InputArray(float s[], int&amp; size); int main() { int count; float MyArray[50], target; InputArray(MyArray, count); // Assume count is 8 // Is 51 in the array? // Is 60 in the array? cin &gt;&gt; target;

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Linear Search' - debra-mcintosh

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Linear Search

Is a value in the array?

void InputArray(float s[], int& size);

int main()

{

int count;

float MyArray[50], target;

InputArray(MyArray, count);

// Assume count is 8

// Is 51 in the array?

// Is 60 in the array?

cin >> target;

// Is target in the array?

// Linear search!

return 0;

}

0 1 2 3 4 5 6 7 49

Linear Search FunctionFind Target in an Array

The function returns true if target is found in the array,

false otherwise.

Function Prototype

Function Name

SearchArray

FindTarget or Find

Function Type

bool (true/false)

Function parameters

floatArray[]: array of type float

size : int, number of elements of floatArray

target : a float number

In, Out, InOut?

bool SearchArray(const float floatArray[], int size, float target);

//-------------------------------------------------------------------//-------------------------------------------------------------------

// The function has three parameters:

// floatArray[]: array of type float

// size : int, number of elements of floatArray

// target : a float number

// The function returns true if target is found in floatArray,

// false otherwise.

// Parameters: (in, in, in)

//-------------------------------------------------------------------

bool SearchArray(const float floatArray[], int size, float target)

{

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

{

if (floatArray[i] == target)

return true;

else

// ?

}

// When to return false?

return false;

}

0 1 2 3 4 5 6 7

//------------------------------------------------

// The function has three parameters:

// floatArray[]: array of type float

// size : int, number of elements of

// floatArray

// target : a float number

// The function returns a bool value:

// true if target is found in floatArray,

// false otherwise.

// Parameters: (in, in, in)

//------------------------------------------------

bool SearchArray(const float floatArray[],

int size, float target)

{

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

{

if (floatArray[i] == target)

return true;

}

return false;

}

//------------------------------------------------

// The function has three parameters:

// floatArray[]: array of type float

// size : int, number of elements of

// floatArray

// target : a float number

// The function returns a bool value:

// true if target is found in floatArray,

// false otherwise.

// Parameters: (in, in, in)

//------------------------------------------------

bool SearchArray(const float floatArray[],

int size, float target)

{

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

{

if (i == target)

return true;

}

return false;

}

// Correct?

// if (floatArray[i] == target)

void InputArray(float s[], int& size)

bool SearchArray(const float floatArray[], int size, float target);

int main()

{

float MyArray[50], target;

int count;

InputArray(MyArray, count);

cin >> target;

while (!cin.eof())

{

if (SearchArray(MyArray, count, target))

cout << “Value “ << target << “ is in the array.”;

else

cout << “Value “ << target << “ is NOT in the array.”;

cin >> target;}

return 0;

}

void InputArray(float s[], int& size);

bool SearchArray(const float floatArray[], int size, float target);

int main()

{

float MyArray[50], target;

int count;

bool found;

InputArray(MyArray, count);

cin >> target;

while (!cin.eof())

{

found = SearchArray(MyArray, count, target);

if (found)

cout << “Value ” << target << “ is in the array.”;

else

cout << “Value ” << target << “ is NOT in the array.”;

cin >> target;}

return 0;

}

// if (found == true)

### Linear Search

Is target in the array?

Check array elements one at a time

Until target is found or

All array elements have been checked

### Linear Search

Find the Largest Value

of All Array Elements

Find the Largest Value of All Array Elements

Function Prototype

Function Name

MaxArrayValue

Function Type

int (float)

Same as the array type

Function parameters

s[] : array of int

size : int, number of elements in s[]

In, Out, InOut?

// Parameters: (in, in)

int MaxArrayValue(const int s[], int size);

//-----------------------------------------------------------//-----------------------------------------------------------

// The function has two parameters:

// s[]: array of int

// size : int, number of elements in s[]

// The function finds and returns the largest array element

// of s[], assuming size is positive and in the range.

// Parameters: (in, in)

//-----------------------------------------------------------

int MaxArrayValue(const int s[], int size)

{

int Max;

Max = s[0];

for (int i = 1; i < size; i ++)

if (s[i] > Max)

Max = s[i];

return Max;

}

//-----------------------------------------------------------//-----------------------------------------------------------

// The function has two parameters:

// s[]: array of int

// size : int, number of elements in s[]

// The function finds and returns the largest array element

// of s[], assuming size is positive and in the range.

// Parameters: (in, in)

//-----------------------------------------------------------

int MaxArrayValue(const int s[], int size)

{

int Max;

Max = s[0];

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

if (s[i] > Max)

Max = s[i];

return Max;

}

Correct?

Good?

//-----------------------------------------------------------//-----------------------------------------------------------

// The function has two parameters:

// s[]: array of int

// size : int, number of elements in s[]

// The function finds and returns the largest array element

// of s[], assuming size is positive and in the range.

// Parameters: (in, in)

//-----------------------------------------------------------

int MaxArrayValue(const int s[], int size)

{

int Max;

Max = 0;

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

if (s[i] > Max)

Max = s[i];

return Max;

}

Correct?

Max = s[0];

//-----------------------------------------------------------//-----------------------------------------------------------

// The function has two parameters:

// s[]: array of int

// size : int, number of elements in s[]

// The function finds and returns the largest array element

// of s[], assuming size is positive and in the range.

// Parameters: (in, in)

//-----------------------------------------------------------

int MaxArrayValue(const int s[], int size)

{

int Max;

Max = s[0];

for (int i = 1; i < size; i ++)

if (i > Max)

Max = s[i];

return Max;

}

Correct?

if (s[i] > Max)

Schedule
• Quiz 5-3

Due Today

• Quiz5-4

Due 5 PM, Wednesday

• Friday

Quiz4-4 (5 points)

• Program 4
• QuizTracing

NOW!