Arrays Visual Basic 2010 How to Program 1992

Arrays Visual Basic 2010 How to Program © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 2 Arrays An array is a group of variables (called elements) containing values that all have the same type. To refer to a particular element in an array, we specify the name of the array and the position number of the element to which we refer. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 2 Arrays Accessing Array Elements ◦ The position number in parentheses more formally is called an index or “subscript. ” An index must be a nonnegative integer or integer expression. ◦ If a program uses an expression as an index, the expression is evaluated first to determine the index. ◦ For example, if variable value 1 is equal to 5, and variable value 2 is equal to 6, then the statement c(value 1 + value 2) += 2 adds 2 to array element c(11). © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 2 Arrays Array Length ◦ Every array “knows” its own length (that is, number of elements), which is determined by the array’s Length property, as in: c. Length ◦ All arrays have the methods and properties of class System. Array. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 3 Declaring and Allocating Arrays The following statement can be used to declare the array : Dim c(11) As Integer The parentheses that follow the variable name indicate that c is an array. The number in parentheses in the array declaration helps the compiler allocate memory for the array c. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 3 Declaring and Allocating Arrays We also can explicitly specify the array bounds, as in Dim c(0 To 11) As Integer The explicit array bounds specified in the preceding statement indicate that the lower bound of the array is 0 and the upper bound is 11. The size of the array is still 12. Because the lower bound must always be 0, we do not include “ 0 To” when declaring an array’s bounds in this book. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 3 Declaring and Allocating Arrays Initializer Lists ◦ You can follow an array declaration with an equal sign and an initializer list in braces ({ and }) to specify the initial values of the array’s elements. ◦ For instance, Dim numbers() As Integer = {1, 2, 3, 6} declares and allocates an array containing four Integer values. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 3 Declaring and Allocating Arrays The compiler determines the array bounds from the number of elements in the initializer list—you cannot specify the upper bound of the array when an initializer list is present. Visual Basic can also use local type inference to determine the type of an array with an initializer list. So, the preceding declaration can be written as: Dim numbers = {1, 2, 3, 6} © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 3 Declaring and Allocating Arrays Default Initialization ◦ When you do not provide an initializer list, the elements in the array are initialized to the default value for the array’s type— 0 for numeric primitive data-type variables, False for Boolean variables and Nothing for String and other class types. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 4 Declaring and Allocating Arrays Array method Get. Upper. Bound returns the index of the last element in the array. The value returned by method Get. Upper. Bound is one less than the value of the array’s Length property. For arrays that represent lists of values, the argument passed to Get. Upper. Bound is 0. In this example, array 1. Get. Upper. Bound(0) returns 4, which is then used to specify the upper bound of array 2, so array 1 and array 2 have the same upper bound (4) and the same length (5). This makes it easy to display the arrays’ contents side-byside. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 6 Assignment - Using Arrays to Analyze Survey Results Consider the following problem statement: ◦ Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the student cafeteria, with 1 being “awful” and 5 being “excellent. ” Place the 20 responses in an integer array and determine the frequency of each rating. ◦ Summarize the number of responses of each type (that is, 1– 5). © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz Consider the following problem statement: ◦ A geography instructor would like to quiz students on their knowledge of the flags of various countries. The instructor has asked you to write an application that displays a flag and allows the student to select the corresponding country from a list. The application should inform the user of whether the answer is correct and display the next flag. The application should display a flag randomly chosen from those of Australia, Brazil, China, Italy, Russia, South Africa, Spain and the United States. When the application executes, a given flag should be displayed only once. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz The application uses arrays to store information. One array stores the country names. Another stores Boolean values that help us determine whether a particular country’s flag has already been displayed, so that no flag is displayed more than once during a quiz. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz GUI ◦ Figure 7. 6 shows the application’s GUI. ◦ The flag images should be added to the project as image resources—you learned how to do this. ◦ The images are located in the Images folder with this chapter’s examples. ◦ This application introduces the Combo. Box control, which presents options in a drop-down list that opens when you click the down arrow at the right side of the control. ◦ A Combo. Box combines features of a Text. Box and a List. Box. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz You can click the down arrow to display a list of predefined items. If you choose an item from this list, that item is displayed in the Combo. Box. If the list contains more items than the drop-down list can display at one time, a vertical scrollbar appears. You may also type into the Combo. Box control to locate an item. In this application, the user selects an answer from the Combo. Box, then clicks the Submit Button to check if the selected country name is correct for the currently displayed flag. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz Combo. Box property Drop. Down. Style determines the Combo. Box’s appearance. Value Drop. Down. List specifies that the Combo. Box is not editable (you cannot type text in its Text. Box). In this Combo. Box style, if you press the key that corresponds to the first letter of an item in the Combo. Box, that item is selected and displayed in the Combo. Box. If multiple items start with the same letter, pressing the key repeatedly cycles through the corresponding items. Set the Combo. Box’s Drop. Down. Style property to Drop. Down. List. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

7. 8 Case Study: Flag Quiz Then, set the Combo. Box’s Max. Drop. Down. Items property to 4, so that the drop-down list displays a maximum of four items at one time. We do this to demonstrate that a vertical scrollbar is added to the drop-down list to allow users to scroll through the remaining items. We show to programmatically add items to the Combo. Box shortly. Figure 7. 7 shows the application’s code. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

To pass an array argument to a method, specify the name of the array without using parentheses. For example, if array hourly. Temperatures has been declared as Dim hourly. Temperatures(24) As Integer the method call Display. Data(hourly. Temperatures) passes array hourly. Temperatures to method Display. Data. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

The For Each…Next repetition statement iterates through the values in a data structure, such as an array, without using a loop counter. For Each…Next behaves like a For…Next statement that iterates through the range of indices from 0 to the value returned by Get. Upper. Bound(0). Instead of a counter, For Each…Next uses a variable to represent the value of each element. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

Sorting an Array © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

Sorting in Descending Order ◦ To sort an array in descending order, first call method Sort to sort the array, then call Array. Reverse with the array as an argument to reverse the order of the elements in the array. ◦ Exercise : asks you to modify this program to display an array’s values in both ascending and descending order. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

Declaring and Initializing Rectangular Arrays ◦ A two-dimensional rectangular array numbers with two rows and two columns can be declared and initialized with ' numbers in a 2 by 2 array Dim numbers(1, 1) As Integer numbers(0, 0) = 1 ' leftmost element in row 0 numbers(0, 1) = 2 ' rightmost element in row 0 numbers(1, 0) = 3 ' leftmost element in row 1 numbers(1, 1) = 4 ' rightmost element in row 1 ◦ Alternatively, the initialization can be written on one line, as shown with and without local type inference below: Dim numbers = {{1, 2}, {3, 4}} Dim numbers(, ) As Integer = {{1, 2}, {3, 4}} © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

Consider the following problem statement: ◦ An instructor gives three tests to a class of 10 students. The grades on these tests are integers in the range from 0 to 100. The instructor has asked you to develop an application to keep track of each student’s average and the class average. The instructor has also asked that there be a choice to view the grades as either numbers or letters. Letter grades should be calculated according to the following grading system: 90– 100 A 80– 89 B 70– 79 C 60– 69 D Below 60 F The application should allow a user to input each student’s three test grades, then compute each student’s average and the class average for all grades entered so far. The application should display numeric grades by default. It should also display a grade distribution chart showing how many of the numeric grades fall into the ranges 0– 9, 10– 19, … 90– 99 and 100. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

GUI for the Grade Report Application ◦ In this application, we introduce Radio. Button controls to allow the user to select between displaying grades in numeric or letter format. ◦ A Radio. Button is a small white circle that either is blank or contains a small dot. ◦ When a Radio. Button is selected, a dot appears in the circle. ◦ A Radio. Button is known as a state button because it can be in only the “on” (True) state or the “off” (False) state. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

You’ve previously used the Check. Box state buttons (introduced in Chapter 5). Radio. Buttons are similar to Check. Boxes, but Radio. Buttons normally appear as a group—only one Radio. Button in the group can be selected at a time. By default, all Radio. Buttons become part of the same group. To separate them into several groups, each group must be in a different container (typically a Group. Box). © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

If the Radio. Button is checked, the Checked property returns True; otherwise, it returns False. A Radio. Button also generates an event when its checked state changes. Event Checked. Changed occurs when a Radio. Button is either selected or deselected. We use this event to switch between the numeric grade and letter grade views in the grades. List. Box. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

The rectangular array grades (line 4) has 10 rows and 3 columns to store the grades for 10 students and three exams per student. Variable student. Count (line 5) keeps track of the number of students processed so far. After 10 students are processed, the program disables the input Text. Boxes and Submit Button. The Grade. Report_Load event handler (lines 8– 13) displays the column heads in the grades. List. Box. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

Method Display. Bar. Chart ◦ Many programs present data to users in a visual or graphical format. ◦ For example, numeric values are often displayed as bars in a bar chart. ◦ In such a chart, longer bars represent proportionally larger numeric values (see Fig. 7. 18). ◦ Method Display. Bar. Chart (Fig. 7. 27) displays a graphical summary of the grade distribution by creating a bar chart that shows how many numeric grades fall into each of the ranges 0– 9, 10– 19, … 90– 99 and 100. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

An array’s size cannot be changed, so a new array must be created if you need to change the size of an existing array. The Re. Dim statement “resizes” an array at execution time by creating a new array and assigning it to the specified array variable. Figure 7. 28 demonstrates the Re. Dim statement. © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

© 1992 -2011 by Pearson Education, Inc. All Rights Reserved.

The output of Fig. 7. 28 shows that after the Re. Dim statement executes, the size of values 1 is changed to 7 and the value of each element is reinitialized to the default value of the type of the array element (that is, 0 for Integers). To save the original data stored in an array, follow the Re. Dim keyword with the optional Preserve © 1992 -2011 by Pearson Education, Inc. All Rights Reserved.
- Slides: 72