ENGR/CS 101 CS Session Lecture 16 Log into Windows (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f13-courses/cs101.html Right-click on Inclass16.zip link. Save link/target to Visual Studio 2012 Projects folder (or wherever the others are). Browse to Inclass16.zip, right-click on it and select Extract All... and extract project folder Browse to the extracted folder, double-click into the Statistics folder to the solution file. Lecture 16 ENGR/CS 101 Computer Science Session 1 Outline Arrays Finding minimum and maximum values Lecture 16 ENGR/CS 101 Computer Science Session 2 Storing Collections Last class we discussed searching and sorting algorithms. Today we will discuss how the data storage might be arranged. First, we want the data to be stored in a collection that is arranged to allow us easy access to each element. 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 16 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. Lecture 16 ENGR/CS 101 Computer Science Session 4 Arrays In C#, array variables are declared using the following syntax, where <type> is the type of the elements: <type> [] <name>; As with other object types, arrays must be created. The syntax for this is as follows, where <size> is the number of elements: <name> = new <type> [<size>]; Lecture 16 ENGR/CS 101 Computer Science Session 5 Arrays To access an array element, you give the array name and the index of the element. Almost all programming languages (including C#) use: <array name>[<index>] 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 16 ENGR/CS 101 Computer Science Session 6 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 16 ENGR/CS 101 Computer Science Session 7 Statistics Project If you have not already done so, browse to the Statistics project folder and double-click on the solution file. Statistics is an application that we can use to explore arrays and algorithms that use them. Lecture 16 ENGR/CS 101 Computer Science Session 8 Interface Mockup The Load button will load a file into the left TextBox called displayBox. It has a scrollbar to allow any number of items. The project has a file random_numbers.txt containing these numbers. Lecture 16 ENGR/CS 101 Computer Science Session 9 Interface Mockup Each of the other buttons will be used to compute a statistic of the numbers loaded from the file. The result is then displayed in the textbox next to the button. They are named minimumBox, maximumBox, averageBox, and stddevBox, respectively. Lecture 16 ENGR/CS 101 Computer Science Session 10 Textbox as an Array Double-click on the Load button. This should take you to its handler, which is partially written. The display textbox is loaded as a single string was done in the previous project. 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 display as displayBox.Lines[0]. Lecture 16 ENGR/CS 101 Computer Science Session 11 Creating an Integer Array In order to compute statistics on the data, we must convert the strings in the display into integers. First we need to declare an integer array. int [] items; Then we need to create the array after we know how many items there will be in the Load button handler. items = new int [num_items]; Lecture 16 ENGR/CS 101 Computer Science Session 12 Filling the Array To fill the array, we need to access the display lines one at a time using a for-loop. For each line, we compute the equivalent integer and store it in the items array. for (int i = 0; i < num_items; i++) { items[i] = Int32.Parse(displayBox.Lines[i]); } Lecture 16 ENGR/CS 101 Computer Science Session 13 Finding the Minimum Value In the Minimum button handler, we write the code for finding the minimum value in the items array. The basic idea is to go through the array keeping track of the current minimum value. Start by assuming the first element is the minimum value. Use a for-loop and an if-statement to check the rest of the elements. Lecture 16 ENGR/CS 101 Computer Science Session 14 minimumBtn_Click code // assume first item is min int min = items[0]; // look at the rest of the items for (int i = 1; i < num_items; i++) { if (items[i] < min) // a new min min = items[i]; } // display the result minimumBox.Text = min.ToString(); Lecture 16 ENGR/CS 101 Computer Science Session 15 In-class Exercise Run the program and make sure that it reports the minimum value. Double-click on the Maximum button. Write the handler code to determine the maximum value in the items array and display it in the maximum textbox. Hint: how does this differ from finding the minimum value? Lecture 16 ENGR/CS 101 Computer Science Session 16