Linear Search - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Linear Search

Description:

size : int, number of elements of floatArray. target : a float number. In, Out, InOut? bool SearchArray(const float ... size : int, number of elements in s ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 16
Provided by: QiY
Category:
Tags: linear | search | size

less

Transcript and Presenter's Notes

Title: Linear Search


1
Linear Search
  • Is target in the array?

2
  • void ReadArray(float s, int size)
  • int main()
  • int count
  • float MyArray50, target
  • ReadArray(MyArray, count)
  • // Assume count is 8
  • // Is 51 in the array?
  • // Is 60 in the array?
  • cin gtgt target
  • // Is target in the array?
  • // Linear search!
  • return 0

0 1 2 3 4 5
6 7

49
3
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
  • 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)

4
  • //------------------------------------------------
    -------------------
  • // 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 lt size i)
  • if (floatArrayi target)
  • return true
  • else
  • // ?
  • // When to return false?

0 1 2 3 4 5
6 7
5
  • //------------------------------------------------
  • // 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 lt size i)
  • if (floatArrayi target)
  • return true

6
  • //------------------------------------------------
  • // 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 lt size i)
  • if (i target)
  • return true

7
  • void ReadArray(float s, int size)
  • bool SearchArray(const float floatArray, int
    size, float target)
  • int main()
  • float MyArray50, target
  • int count
  • ReadArray(MyArray, count)
  • cin gtgt target
  • while (!cin.eof())
  • if (SearchArray(MyArray, count, target))
  • cout ltlt Value ltlt target ltlt is in
    the array.
  • else
  • cout ltlt Value ltlt target ltlt is NOT
    in the array.
  • cin gtgt target
  • return 0

8
  • void ReadArray(float s, int size)
  • bool SearchArray(const float floatArray, int
    size, float target)
  • int main()
  • float MyArray50, target
  • int count
  • bool found
  • ReadArray(MyArray, count)
  • cin gtgt target
  • while (!cin.eof())
  • found SearchArray(MyArray, count,
    target)
  • if (found)
  • cout ltlt Value ltlt target ltlt is in
    the array.
  • else
  • cout ltlt Value ltlt target ltlt is NOT
    in the array.
  • cin gtgt target
  • return 0

9
Linear Search
  • Is target in the array?
  • Check array elements one at a time.

10
Linear Search
  • Find the Largest Value
  • of All Array Element

11
  • Find the Largest Value of All Array Elements
  • Function Prototype
  • Function Name
  • MaxArrayValue
  • Function Type
  • float (int)
  • 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)

12
  • //------------------------------------------------
    -----------
  • // 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 s0
  • for (int i 1 i lt size i )
  • if (si gt Max)
  • Max si
  • return Max

13
  • Quiz5
  • Part III
  • Due 5 pm Wednesday
  • Part IV
  • Due 5 PM Friday

14
This Week
  • Thursday
  • Lab 7
  • Friday
  • Lab 206 to do Prog3 and Quiz (Lab)

15
Program 3
  • Due Date 930 PM Friday
  • Program 4
  • Paring Sign Sheet
  • Test 2
  • Week 10
Write a Comment
User Comments (0)
About PowerShow.com