1 / 16

Linear Search

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;

Download Presentation

Linear Search

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linear Search Is a value in the array?

  2. 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

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

  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 < size; i++) { if (floatArray[i] == target) return true; else // ? } // When to return false? 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 < size; i++) { if (floatArray[i] == target) return true; } return false; }

  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 < size; i++) { if (i == target) return true; } return false; } // Correct? // if (floatArray[i] == target)

  7. 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; }

  8. 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)

  9. 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

  10. Linear Search Find the Largest Value of All Array Elements

  11. 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);

  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 = s[0]; for (int i = 1; i < size; i ++) if (s[i] > Max) Max = s[i]; return Max; }

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

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

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

  16. Schedule • Quiz 5-3 Due Today • Quiz5-4 Due 5 PM, Wednesday • Friday Quiz4-4 (5 points) • Program 4 • QuizTracing NOW!

More Related