Linear Search - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Linear Search

Description:

target : a float number // The function returns a bool value: // true if target is found in floatArray // false otherwise // Parameters: (in, in, in) ... – PowerPoint PPT presentation

Number of Views:311
Avg rating:3.0/5.0
Slides: 14
Provided by: QiY
Category:
Tags: linear | search | target

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 target
  • // Is target in the array?
  • // Linear search!
  • return 0

0 1 2 3 4 5
6 7

49
3
  • // 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 (?, ?, ?)
  • // Parameters (in, in, in)
  • bool SearchArray(const float floatArray, int
    size,
  • float target)
  • for (int i 0 i
  • if (floatArrayi target)
  • return true
  • else
  • // ?
  • return false

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

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
  • if (i target)
  • return true
  • return false

6
  • void ReadArray(float s, int size)
  • bool SearchArray(const float floatArray,
    int size, float target)
  • int main()
  • float MyArray50, targetint count
  • ReadArray(MyArray, count)
  • cin target
  • while (!cin.eof())
  • if (SearchArray(MyArray, count, target))
  • cout
  • else
  • cout
  • cin target
  • return 0

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

8
Linear Search
  • What is the index of the target?

9
Function Prototype
  • // Returns the index of the first array element
  • // that has a value of target.
  • int IndexOfTarget(const float floatArray,
  • int size, float target)
  • // What if target is not in the array?
  • // Return -1

10
  • void ReadArray(float s, int size)
  • int IndexOfTarget(const float floatArray,
    int size, float target)
  • int main()
  • float MyArray50, targetint count
  • ReadArray(MyArray, count)
  • cin target
  • while (!cin.eof())
  • index IndexOfTarget(MyArray, count,
    target)
  • if (index -1)
  • cout in the array.
  • else
  • cout index
  • cin target
  • return 0

11
  • // The function has three parameters
  • // floatArray array of type float
  • // size int, number of elements of
    floatArray
  • // target a float number
  • // The function returns an integer
  • // If target is found in floatArray, the
    index of the first
  • // element with value being target is
    returned
  • // Otherwise, -1 is returned.
  • // Parameters (in, in, in)
  • int IndexOfTarget(const float floatArray, int
    size,
  • float target)
  • for (int i 0 i
  • if (floatArrayi target)
  • return i
  • else
  • // ?

0 1 2 3 4 5
6 7
12
  • // The function has three parameters
  • // floatArray array of type float
  • // size int, number of elements of
    floatArray
  • // target a float number
  • // The function returns an integer
  • // If target is found in floatArray, the
    index of the first
  • // element with value being target is
    returned
  • // Otherwise, -1 is returned.
  • // Parameters (in, in, in)
  • int IndexOfTarget(const float floatArray, int
    size,
  • float target)
  • for (int i 0 i
  • if (floatArrayi target)
  • return i
  • return -1

13
  • Quiz5 Part III
  • Due 930 pm Wednesday
  • Program 3
  • Due 930 pm Tuesday
  • Program 4
Write a Comment
User Comments (0)
About PowerShow.com