Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.

Similar presentations


Presentation on theme: "1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays."— Presentation transcript:

1 1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays

2 2 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays  We search an array to find a particular element in an array  For example, we might like to search an array of student grades for all students who got higher than 90% (i.e. A’s)  How would we do this?

3 3 11/27/06CS150 Introduction to Computer Science 1 Sequential or Linear Search  Compare each element of the array with the value (or key) that we are searching for  This is called linear or sequential search  Linear Search Algorithm: o For each array element  If the current element contains the target  Return the subscript of the current element o Return -1

4 4 11/27/06CS150 Introduction to Computer Science 1 Write the function findElement void findElement(int [], int, int&); int main() { int grades[10]; int element, index = -1; for(int i = 0; i < 10; i++) cin >> grades[i]; cout << "Which element would you like to find?" << endl; cin >> element; findElement(grades, element, index); if(-1 == index) cout << "Element could not be found!" << endl; else cout << "Element was found at index " << index << endl; }

5 5 11/27/06CS150 Introduction to Computer Science 1 Function to find element void findElement( int ar[], int x, int & index) { for(int i = 0; i < 10; i++) if(ar[i] == x) index = i; }

6 6 11/27/06CS150 Introduction to Computer Science 1 27.1 Problem  Write a function to return the index of the smallest element in a subarray  A subarray is a section of an array. The subarray is determined by its starting and ending indexes  The function will have the following arguments: o The array, o The starting index of the subarray, o The ending index of the subarray, o The index of the smallest element.

7 7 11/27/06CS150 Introduction to Computer Science 1 Function findIndexOfMin void findIndexOfMin(const int x[], int startIndex, int endIndex, int& index) { You fill in the rest }

8 8 11/27/06CS150 Introduction to Computer Science 1 Random Number Generation Revisited  Remember, the library contains a function for generating random numbers  The statement used to produce integers in the range 0 - 5 is o int x = rand() % 6;  To simulate the role of a dice we would use the statement o int x = 1 + rand() % 6

9 9 11/27/06CS150 Introduction to Computer Science 1 27.2 Random Number Generation  Write a program that will simulate the roll of a dice 6000 times and show the frequency in which each side appeared Face Frequency 1 1003 2 1017 3 983 4 994 5 1004 6 999

10 10 11/27/06CS150 Introduction to Computer Science 1 Practice  Read in a list of last names (strings) and exam scores (ints) and allow the user to find the names of people who scored between two numbers. Input: Smith97 Jones88 Franklin93 Query: 90 99 Smith Franklin Declare the data structures Write code to read in the data (assume at most 24 students) Write a function to perform the query


Download ppt "1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays."

Similar presentations


Ads by Google