linear search
Download
Skip this Video
Download Presentation
Linear Search

Loading in 2 Seconds...

play fullscreen
1 / 16

Linear Search - PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on

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;

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

PowerPoint Slideshow about ' Linear Search' - debra-mcintosh


An Image/Link below is provided (as is) to download presentation

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

Linear Search

Is a value in the array?

slide2
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 function find target in an array
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);

slide4
//-------------------------------------------------------------------//-------------------------------------------------------------------

// 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

slide5
//------------------------------------------------

// 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;

}

slide6
//------------------------------------------------

// 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)

slide7
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;

}

slide8
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 search1

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 search2

Linear Search

Find the Largest Value

of All Array Elements

slide11
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);

slide12
//-----------------------------------------------------------//-----------------------------------------------------------

// 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;

}

slide13
//-----------------------------------------------------------//-----------------------------------------------------------

// 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?

slide14
//-----------------------------------------------------------//-----------------------------------------------------------

// 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];

slide15
//-----------------------------------------------------------//-----------------------------------------------------------

// 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
Schedule
  • Quiz 5-3

Due Today

  • Quiz5-4

Due 5 PM, Wednesday

  • Friday

Quiz4-4 (5 points)

  • Program 4
  • QuizTracing

NOW!

ad