Download presentation
Presentation is loading. Please wait.
1
CS 1430: Programming in C++
2
Linear Search Is a value in the array?
Compare index against array values Find the last lowest value: 1. for (I = 0; I < size; I ++) if (s[i] <= s[index]) index = I; 2. for (int I = size – 1; I >= 0; I --) if (s[i] < s[index])
3
. . . . 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; } 40 45 51 44 59 50 ?
4
Linear Search Function Find 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);
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 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; 40 45 51 44 59 50 ?
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 (floatArray[i] == target) return true; } return false;
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 (i == target) return true; } return false; // Correct? // if (floatArray[i] == target)
8
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; }
9
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)
10
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
11
Find the Largest Value of All Array Elements
Linear Search Find the Largest Value of All Array Elements
12
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);
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 = 1; i < size; i ++) if (s[i] > max) max = s[i]; return max; }
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 = s[0]; for (int i = 0; i < size; i ++) if (s[i] > max) max = s[i]; return max; } Correct? Good?
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 = 0; for (int i = 0; i < size; i ++) if (s[i] > max) max = s[i]; return max; } Correct? max = s[0];
16
//-----------------------------------------------------------
// 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)
17
Schedule Quiz5-4 Due 10 PM, Wednesday Friday Quiz2 (10 points) Lab6
18
Any Questions about Grading?
Prog3 Any Questions about Grading?
19
Optional Pairs Start Early!
Prog4 Optional Pairs Start Early!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.