Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++.

Similar presentations


Presentation on theme: "CS 1430: Programming in C++."— Presentation transcript:

1 CS 1430: Programming in C++

2 Linear Search Is target in the array?
What’s the largest value in an array? Check array elements one at a time.

3 What is the index of the target?
Linear Search What is the index of the target?

4 . . . . Target: 51 Index: 2 Target: 40
Index of the first element being the target Index: 3 Index of the last element being the target Index: 7 Target: 60 Not in the array Return -1 (invalid index) 45 51 40 50 44 59 ?

5 Function Prototype The function returns the index of the first array element that has a value equal to target. The function returns -1 if the target is not found in the array. Function Name IndexOfTarget Function Type int Function Parameters floatArray[]: array of type float size : int, number of elements of floatArray target : a float number In, Out, InOut? // Parameters: (In, In, In) int IndexOfTarget(const float floatArray[], int size, float target);

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 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 < size; i++) if (floatArray[i] == target) return i; else // ? } // When to return -1? return -1; 40 45 51 44 59 ?

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 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 < size; i++) if (floatArray[i] == target) return i; } return -1;

8 //---------------------------------------------------------------
// 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 < size; i++) if (i == target) // Correct? return i; } return -1; NO! if (floatArray[i] == target)

9 void InputArray(float s[], int& size)
int IndexOfTarget(const float floatArray[], int size, float target); int main() { float myArray[50], target; int count, index; InputArray(myArray, count); cin >> target; while (!cin.eof()) index = IndexOfTarget(myArray, count, target); if (index == -1) cout << “Target ” << target << “ is not in the array.”; else cout << “Target ” << target << “ is at index ” << index; cin >> target; } return 0; }

10 Correct? Good? Yes. No! void InputArray(float s[], int& size)
int IndexOfTarget(const float floatArray[], int size, float target); int main() { float myArray[50], target; int count; InputArray(myArray, count); cin >> target; while (!cin.eof()) if (IndexOfTarget(myArray, count, target) == -1) cout << “Target ” << target << “ is not in the array.”; else cout << “Target ” << target << “ is at index ” << IndexOfTarget(myArray, count, target); cin >> target; } return 0; } Correct? Yes. Good? No!

11 Correct and Good! void InputArray(float s[], int& size)
int IndexOfTarget(const float floatArray[], int size, float target); int main() { float myArray[50], target; int count, index; InputArray(myArray, count); cin >> target; while (!cin.eof()) index = IndexOfTarget(myArray, count, target); if (index == -1) cout << “Target ” << target << “ is not in the array.”; else cout << “Target ” << target << “ is at index ” << index; cin >> target; } return 0; } Correct and Good!

12 What’s the Largest Value in an Array?
Linear Search What’s the Largest Value in an Array?

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]; //max = 0; Not correct! for (int i = 1; i < size; i ++) if (s[i] > max) max = s[i]; return max; } Not correct: if (i > max)

14 Find the Index of the Largest Value of All Array Elements
Linear Search Find the Index of the Largest Value of All Array Elements

15 Function Prototype The function returns the index of the first largest array element. Function Name IndexOfMaxArrayValue Function Type int Function Parameters s[] : array of type float (int) size: int, number of elements of s[] In, Out, InOut? // Parameters: (In, In) int IndexOfMaxArrayValue(const float s[], int size);

16 //-----------------------------------------------------------
// The function has two parameters: // s[] : array of int // size : int, number of elements in s[] // The function finds and returns the index of the first // largest array element of s[], assuming size is // positive and in the range. // Parameters: (in, in) int IndexOfMaxArrayValue(const float s[], int size) { int index = 0; float max = s[0]; for (int i = 1; i < size; i ++) if (s[i] > max) max = s[i]; index = i; } return index;

17 //-----------------------------------------------------------
// The function has two parameters: // s[] : array of int // size : int, number of elements in s[] // The function finds and returns the index of the first // largest array element of s[], assuming size is // positive and in the range. // Parameters: (in, in) int IndexOfMaxArrayValue(const float s[], int size) { int index = 0; float max = s[0]; for (int i = 1; i < size; i ++) if (s[i] > max) //max = s[i]; // Do we need this statement? index = i; } return index; // YES!

18 //-----------------------------------------------------------
// The function has two parameters: // s[] : array of int // size : int, number of elements in s[] // The function finds and returns the index of the first // largest array element of s[], assuming size is // positive and in the range. // Parameters: (in, in) int IndexOfMaxArrayValue(const float s[], int size) { int index = 0; //float max = s[0]; for (int i = 1; i < size; i ++) if (s[i] > s[index]) // if (s[i] > max) //max = s[i]; // Not needed any more index = i; } return index; Very good!

19 //---------------------------------------------------------
// The function has two parameters: // s[] : array of int // size : int, number of elements in s[] // The function finds and returns the index of the first // largest array element of s[], assuming size is // positive and in the range. // Parameters: (in, in) int IndexOfMaxArrayValue(const float s[], int size) { int index = 0; for (int i = 1; i < size; i ++) if (s[i] > s[index]) index = i; return index; }

20 Parallel Arrays int numStudents; string names[30]; float scores[30];
char grades[30]; // names[0] : name of the 1st student // scores[0]: score of the 1st student // grades[0]: grade of the 1st student // names[9] : name of the 10th student // scores[9]: score of the 10th student // grades[9]: grade of the 10th student // names[i] : the name of the (i + 1)th student // scores[i]: the score of the (i + 1)th student // grades[i]: grade of the (i + 1)th student Short note Add nested for loops here

21 Linear Search int IndexOfName(const string s[], int size, string target) { for (int i = 0; i < size; i++) if (s[i] == target) return i; } return -1; int IndexOfGrade(const char chArray[], int size, char target) if (chArray[i] == target)

22 Parallel Arrays int IndexOfName(const string s[], int size, string name); int IndexOfMaxValue(const float s[], int size); int main() { int numStudents, index; string names[30], name; float scores[30]; char grades[30]; // Call function to input data cin >> name; index = IndexOfName(names, numStudents, name); if (index == -1) cout << “The name is not in the array.”; else cout << “The score for “ << name << “ is “ << scores[index]; cout << “The grade for “ << name << “ is “ << grades[index]; } index = IndexOfMaxValue(scores, numStudents); cout << names[index] << “ has the highest score of ” << scores[index]; return 0; Short note Add nested for loops here

23 Parallel Arrays int IndexOfGrade(const char s[], int size, char grade); int CountOfGrade(const char s[], int size, char grade); int main() { int numStudents, index, count; string names[30], name; float scores[30]; char grades[30], grade; // Input data cin >> grade; // A, B, C, D or F index = IndexOfGrade(grades, numStudents, grade); if (index == -1) cout << “No one has grade ” << grade << “.”; else cout << “Student ” << names[index] << “ has grade ” << grade; count = CountOfGrade(grades, numStudents, grade); cout << count << “ students have grade ” << grade << ‘.’; return 0; } Short note Add nested for loops here

24 Schedule Quiz5-4: Due 10 PM Wednesday Quiz5-5: Due 10 PM Friday
Quiz2 (10 points): Friday in class Lab7: Thursday Program 4 Test 2 Friday, Novemeber 6


Download ppt "CS 1430: Programming in C++."

Similar presentations


Ads by Google