ENGR/CS 101 CS Session Lecture 10

advertisement

ENGR/CS 101 CS Session

Lecture 10

Log into Windows (reboot if in Linux)

Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f11-courses/engrcs101/cs.html

Right-click on Inclass10.zip link. Save link/target to Visual Studio 2010 projects folder.

Browse to Inclass10.zip, right-click on it and select Extract All... and extract project folder

Double-click into the Search folder, then double-click on the Visual Studio Solution file.

Lecture 10 ENGR/CS 101 Computer Science Session 1

Outline

Arrays

Design of linear search

Implementing linear search

C# language

 boolean type, while-loops

Reminder: Project 1 must be completed and submitted as part of the grade for this session.

Lecture 10 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 to this storage 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 10 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 10 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 10 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 10 ENGR/CS 101 Computer Science Session 6

Interface Mockup

Search 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 nameListBox .

Lecture 10 ENGR/CS 101 Computer Science Session 7

Interface Mockup

A string can be entered in the small textbox called targetBox . Clicking on the Search button will search for the target box Text in the lines of nameListBox.

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 10 ENGR/CS 101 Computer Science Session 8

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 10 ENGR/CS 101 Computer Science Session 9

Design of Linear Search

As noted previously, the data collection we will be working with is the lines of text in the nameListBox. We have seen before that the text can be treated as one string using the Text property. (E.g., the Load button handler.)

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 nameListBox as nameListBox.Lines[0].

Lecture 10 ENGR/CS 101 Computer Science Session 10

Design of Linear Search

Other data items that we need are

 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

A boolean variable is one that may have values of true or false (these are literal keywords, not strings). In C#, the type of this kind of variable is bool .

Lecture 10 ENGR/CS 101 Computer Science Session 11

Design of Linear Search

Here is the full algorithm for the Search button handler:

1. Initialize position to 0 and found to false

2. While position is less than the length of the target string and

the target has not been found do

2.1 If nameListBox.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 10 ENGR/CS 101 Computer Science Session 12

While-Loops

For indexing a string in Project 1, 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 4 ENGR/CS 101 Computer Science Session 13

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 nameListBox.Lines array or if a match is found.

We can do this with the following condition: while (position < nameListBox.Lines.Length 

       && !found)  // both must be true to repeat

Lecture 4 ENGR/CS 101 Computer Science Session 14

Implementing Linear Search

Double-click on the Search button to get to the handler code skeleton for this button.

Write the following code in the search_Click handler. It implements the design on Slides 11 and 12.

   // Declare and initialize variables

   int position = 0;     // index of the target

      bool found = false;   // flag to indicate target

                            // has been found

      

      // rest of handler code on next slide

Lecture 10 ENGR/CS 101 Computer Science Session 15

Implementing Linear Search

       // while we haven't reached the end of the list

      // and the target is not found

      while (position < nameListBox.Lines.Length 

             && !found)

      {

         // check if the target is at this position

         if (nameListBox.Lines[position] == targetBox.Text)

            found = true;

         else  // it's not so try the next element

            position++;

      }

      // 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 10 ENGR/CS 101 Computer Science Session 16

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 "Andy"

(without the quotes)? For "Mary"? For "alex"?

Why does "alex" have a result of -1?

Lecture 10 ENGR/CS 101 Computer Science Session 17

Download