ENGR/CS 101 CS Session Lecture 11 Log into Windows (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f12-courses/engrcs101/cs.html Right-click on Inclass11.zip link. Save link/target to Visual Studio 2010 projects folder (or wherever the others are). Browse to Inclass11.zip, right-click on it and select Extract All... and extract project folder Double-click into the NameSearch folder, then double-click on the Visual Studio Solution file. Lecture 11 ENGR/CS 101 Computer Science Session 1 Outline Arrays Files OpenFileDialog StreamReader Design of linear search Implementing linear search Lecture 11 While-loops ENGR/CS 101 Computer Science Session 2 Storing Collections Last class we discussed searching and sorting algorithms. Today we will discuss what we need to implement these algorithms. First, we need a place to store the collection of data. We want this storage arranged to allow us to access each element easily. Also, we want to be able to identify an element of the collection by using the position (or index) of the element in the collection. Lecture 11 ENGR/CS 101 Computer Science Session 3 Arrays The simplest collection in most programming languages is the array. Formally, an array is an ordered collection of elements of the same type that is accessed by an index. Ordered means that the relative position of each element matters. To access an array element, you give the array name and the index of the element. Lecture 11 ENGR/CS 101 Computer Science Session 4 Arrays Almost all programming languages (including C#) use <array name>[<index>] as the syntax for accessing an array element. Note this is the same syntax we used to access an individual character in a string. You can think of a string as a special kind of array. Lecture 11 ENGR/CS 101 Computer Science Session 5 Arrays The indexes of an array usually start at 0 or 1. For technical reasons, most modern programming languages (including C#) start indexing at 0. Here is a picture of an array of 10 integers: anArray 23 45 76 39 5 87 16 92 54 63 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Lecture 11 ENGR/CS 101 Computer Science Session 6 Interface Mockup NameSearch is a GUI project that we can use to explore arrays and the linear search algorithm. The Load button will load a file into the large TextBox called nameBox. Lecture 11 ENGR/CS 101 Computer Science Session 7 Interface Mockup A string can be entered in the small textbox called target. Clicking on the Search button will search for the target Text in the lines of nameBox. If the target string is found, its position/index will be displayed in the label named resultLabel. If the target string is not found, "-1" will be displayed. Lecture 11 ENGR/CS 101 Computer Science Session 8 Reading from a File The click handler for the Load is already written. The design of the handler is as follows: 1. Create an OpenFileDialog object 2. Make the dialog pop up and wait for the user to respond 3. Check if user canceled. If so, go back to waiting. 4. Attach the chosen file to a StreamReader object 5. Read the contents of the file and store it into the name box. Lecture 11 ENGR/CS 101 Computer Science Session 9 OpenFileDialog An OpenFileDialog is the window that pops up when a user wants to open a file. Lecture 11 ENGR/CS 101 Computer Science Session 10 StreamReader A file stream is a program object that is opened (i.e., attached to a physical file) when it is created. StreamReader objects are used to read data from a file. There are also SaveFileDialog and StreamWriter objects for writing data into a file. Lecture 11 ENGR/CS 101 Computer Science Session 11 Design of Linear Search Recall that the linear search algorithm is to start at one end of a collection and check if each one is the target item, stopping when the target is found. The result of doing a linear search often is the index of the target element in the collection that is then used to access other data or to do other computations. For this project we will just display the index of the target, if found. Lecture 11 ENGR/CS 101 Computer Science Session 12 Design of Linear Search As noted previously, the data collection we will be working with is the lines of text in the nameBox. The Load button click handler treats the Text property as one string. C# also allows this data to be viewed as an array of strings where each element is a line of the text. The name of this property is Lines. So for example, we can access the first line of the nameBox as nameBox.Lines[0]. Lecture 11 ENGR/CS 101 Computer Science Session 13 Design of Linear Search Other data items that we need are Lecture 11 position – an integer that is the index of the target element in the collection found – a boolean flag that is used to stop the search when the target is found ENGR/CS 101 Computer Science Session 14 Design of Linear Search Here is the full algorithm for the Search button click handler: 1. Initialize position to 0 and found to false 2. While position is less than the length of list of names and the target has not been found do 2.1 If nameBox.Lines[position] equals target string then 2.1.1 Set found to true Else 2.1.2 Increment position 3. If target has been found then 3.1 Set the result label to position as a string Else 3.2 Set the result label to "-1" Lecture 11 ENGR/CS 101 Computer Science Session 15 While-Loops For indexing a string in the cipher project, we know how many times we want to repeat the encipher action, so we used a for-loop We don't know when the searching loop will find a match, so we need to use a while-loop to implement this behavior. The syntax for the while loop is: while (<condition>) { // do something } Lecture 11 ENGR/CS 101 Computer Science Session 16 While-Loops The condition of the while-loop is tested and if it is true, the body of the loop is executed. When the condition becomes false, execution continues with the statement after the loop. For our program, we want the loop to stop if it has checked every string in the nameBox.Lines array or if a match is found. We can do this with the following condition: while (position < nameBox.Lines.Length && !found) // both must be true to repeat Lecture 11 ENGR/CS 101 Computer Science Session 17 Implementing Linear Search Double-click on the Search button to get to the click handler code stub for this button. Write the code on the following two slides in the searchBtn_Click handler. It implements the design on Slides 14 and 15. Lecture 11 ENGR/CS 101 Computer Science Session 18 Implementing Linear Search // Declare and initialize variables int position = 0; // index of the target bool found = false; // flag to indicate target found // while we haven't reached the end of the list // and the target is not found while (position < nameBox.Lines.Length && !found) { // check if the target is at this position if (nameBox.Lines[position] == target.Text) found = true; else // it's not so try the next element position++; } Lecture 11 ENGR/CS 101 Computer Science Session 19 Implementing Linear Search // see if the target was found if (found) // display its position as a string resultLabel.Text = position.ToString(); else // otherwise display "-1" resultLabel.Text = "-1"; Lecture 11 ENGR/CS 101 Computer Science Session 20 Exploring Search Application When you have finished implementing the linear search algorithm, run the application with various search targets. E.g., what happens if you search for "Cody" (without the quotes)? For "Mary"? For "kevon"? Why does "kevon" have a result of -1? Lecture 11 ENGR/CS 101 Computer Science Session 21