1 / 53

Objectives

1. Objectives. Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array Code a loop using the For Each…Next statement Access an element in a one-dimensional array. Objectives ( continued ).

diza
Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1

  2. Objectives • Declare and initialize a one-dimensional array • Store data in a one-dimensional array • Display the contents of a one-dimensional array • Code a loop using the For Each…Next statement • Access an element in a one-dimensional array Microsoft Visual Basic .NET: Reloaded

  3. Objectives (continued) • Search a one-dimensional array • Compute the average of a one-dimensional array’s contents • Find the highest entry in a one-dimensional array • Update the contents of a one-dimensional array • Sort a one-dimensional array Microsoft Visual Basic .NET: Reloaded

  4. Objectives (continued) • Enter code in the form’s Load event procedure • Create and manipulate parallel one-dimensional arrays • Create and initialize a two-dimensional array • Store data in a two-dimensional array • Search a two-dimensional array Microsoft Visual Basic .NET: Reloaded

  5. Using Arrays • Simple variable • Also called scalar variable • Is unrelated to any other variable in memory • Array • A group of variables that have the same name and data type and are related in some way • You can picture an array as a group of small adjacent boxes inside the computer’s memory Microsoft Visual Basic .NET: Reloaded

  6. One-Dimensional Arrays • One-dimensional array • Visualize as a column of variables • Each variable is identified by a unique number • Unique number is called “subscript” • Subscript is placed inside parentheses immediately following array name • Example strStates(1) – read “strStates sub one” • Individual variables referred to as “elements” Microsoft Visual Basic .NET: Reloaded

  7. One-Dimensional Arrays (continued) • Declare using keywords Dim or Private • highestSubscript argument in declaration used to specify the number of elements in the array • number of array elements is 1 more than the value in highestSubscript argument Microsoft Visual Basic .NET: Reloaded

  8. One-Dimensional Arrays(continued) Microsoft Visual Basic .NET: Reloaded

  9. HOW TO… Microsoft Visual Basic .NET: Reloaded

  10. Storing Data in a One-Dimensional Array Microsoft Visual Basic .NET: Reloaded

  11. Manipulating One-Dimensional Arrays • Sample procedures will demonstrate how to perform tasks using a one-dimensional array • Display contents of an array • Access an array element using its subscript • Search the array • Calculate the average of the data stored in a numeric array • Find the highest value stored in an array • Update the array elements • Sort the array elements Microsoft Visual Basic .NET: Reloaded

  12. Displaying the Contents of a One-Dimensional Array Microsoft Visual Basic .NET: Reloaded

  13. Displaying the Contents of a One-Dimensional Array(continued) Microsoft Visual Basic .NET: Reloaded

  14. The For Each…Next Statement • Use to code a loop containing instructions to be processed for each element in a group • Syntax template shown in next slide contains words “element” and “group” • “element” • Declare a variable of the same data type as the elements of the array and place this variable in the “element” position of the template • “group” • The variable name of the array Microsoft Visual Basic .NET: Reloaded

  15. HOW TO… Microsoft Visual Basic .NET: Reloaded

  16. Using the Subscript to Access an Element in a One-Dimensional Array Display salary amount corresponding to code entered by user Microsoft Visual Basic .NET: Reloaded

  17. Searching a One-Dimensional Array • Determine number of salespeople selling above a certain amount Microsoft Visual Basic .NET: Reloaded

  18. Calculating the Average Amount Stored in a One-Dimensional Numeric Array • Calculate and display average test score earned by students on final exam Microsoft Visual Basic .NET: Reloaded

  19. Determining the Highest Value Stored in a One-Dimensional Array • Display highest amount earned in a week Microsoft Visual Basic .NET: Reloaded

  20. Updating the Values Stored in a One-Dimensional Array • Procedure allows manager to increase price of each item and display item’s new price Microsoft Visual Basic .NET: Reloaded

  21. Sorting the Data Stored in a One-Dimensional Array • Array.Sort(arrayName) method • Sorts the elements of a one-dimensional array in “ascending order” • First element contains smallest value • Last element contains largest value • To sort in descending order • First sort in ascending order • Then use Array.Reverse(arrayname) method to reverse the position of the elements in the array Microsoft Visual Basic .NET: Reloaded

  22. Sorting the Data Stored in a One-Dimensional Array(continued) • btnSortAscending_Click procedure sorts intNumbers array into ascending order Microsoft Visual Basic .NET: Reloaded

  23. Sorting the Data Stored in a One-Dimensional Array(continued) Microsoft Visual Basic .NET: Reloaded

  24. Sorting the Data Stored in a One-Dimensional Array (continued) • Method sorts array in descending order and displays the results Microsoft Visual Basic .NET: Reloaded

  25. Sorting the Data Stored in a One-Dimensional Array(continued) Microsoft Visual Basic .NET: Reloaded

  26. Using a Module-Level One-Dimensional Array • Application displays names contained in a sequential access file, giving user choice of ascending or descending order of display • First, declare array variable in the Form’s general declarations section • Then, fill array from data in file using code in the form’s Load event procedure • Load event occurs before the first time a form is displayed Microsoft Visual Basic .NET: Reloaded

  27. Using a Module-Level One-Dimensional Array(continued) Microsoft Visual Basic .NET: Reloaded

  28. Using a Module-Level One-Dimensional Array(continued) Microsoft Visual Basic .NET: Reloaded

  29. Parallel One-Dimensional Arrays • Two or more one-dimensional arrays whose elements are related by their position (subscript) • Called parallel because element at subscript 0 in first array corresponds to element at subscript 0 in another array • Example: • strID(0) corresponds to intPrice(0) and • strID(1) corresponds to intPrice(1), • and so on… Microsoft Visual Basic .NET: Reloaded

  30. Parallel One-Dimensional Arrays(continued) Microsoft Visual Basic .NET: Reloaded

  31. Parallel One-Dimensional Arrays(continued) Microsoft Visual Basic .NET: Reloaded

  32. Parallel One-Dimensional Arrays(continued) Microsoft Visual Basic .NET: Reloaded

  33. Two-Dimensional Arrays • Resemble a table in that variables are store in rows and columns • Each element is identified by a unique combination of two subscripts • The first subscript indicates the element’s row • The second subscript indicates the column • Each subscript begins with 0 • The element in the first row and column has a subscript of (0,0) Microsoft Visual Basic .NET: Reloaded

  34. Two-Dimensional Arrays(continued) Microsoft Visual Basic .NET: Reloaded

  35. HOW TO… Microsoft Visual Basic .NET: Reloaded

  36. Storing Data in a Two-Dimensional Array Microsoft Visual Basic .NET: Reloaded

  37. Searching a Two-Dimensional Array Microsoft Visual Basic .NET: Reloaded

  38. Programming Example – Perrytown Gift Shop Application • Application is to assist in employee weekly payroll process • Calculates and displays the federal withholding tax (FWT) • Gets and displays taxable wages • Gets and displays marital status Microsoft Visual Basic .NET: Reloaded

  39. TOE Chart Microsoft Visual Basic .NET: Reloaded

  40. User Interface Microsoft Visual Basic .NET: Reloaded

  41. Objects, Properties, and Settings Microsoft Visual Basic .NET: Reloaded

  42. Objects, Properties, and Settings(continued) Microsoft Visual Basic .NET: Reloaded

  43. Tab Order Microsoft Visual Basic .NET: Reloaded

  44. Pseudocode btnExit Click event procedure close application btnCalc Click event procedure Include the following in a Try/Catch block: remove any dollar signs and spaces from the txtTaxable control assign the taxable wages store in txtTaxable to a variable assign the marital status entered in txtStatus to a variable if marital status is not either M or S display an appropriate message else if marital status is M use the Married tax table, stored in an array else use the singles tax table, stored in an array Microsoft Visual Basic .NET: Reloaded

  45. Pseudocode (continued) end if end if Repeat while there are still rows in the tax table to search and taxable wages have not been found if taxable wages are less than or equal to value stored in the first column of the current row in tax table use information stored in 2nd, 3rd, and 4th columns in tax table to calculate the federal withholding tax indicate that the taxable wages were found by assigning the value True to blnFound variable else add 1 to the contents of the intRow variable to continue the search row in the tax table Display the federal withholding tax in the lblFwt control Use general Catch statement to handle and display any errors Microsoft Visual Basic .NET: Reloaded

  46. Code Microsoft Visual Basic .NET: Reloaded

  47. Code (continued) Microsoft Visual Basic .NET: Reloaded

  48. Summary • Programmers use arrays to temporarily store related data in computer’s internal memory • All variables in an array have the same name and data type • Each element in a one-dimensional array is identified by a unique subscript • Appears in parentheses after array’s name Microsoft Visual Basic .NET: Reloaded

  49. Summary (continued) • Each element in a two-dimensional array is identified by a unique combination of two subscripts: • Row subscript • Column subscript • Subscripts appear in parentheses after the array’s name • You list the row subscript first, followed by a comma and the column subscript • The first subscript in a one-dimensional array is 0 (zero) Microsoft Visual Basic .NET: Reloaded

  50. Summary (continued) • First row subscript in a two-dimensional array is 0 (zero) likewise, the first column subscript also is 0 (zero) • When declaring a one-dimensional array, provide either highest subscript or initial values • When declaring a two-dimensional array, you provide either the highest row and column subscripts or the initial values Microsoft Visual Basic .NET: Reloaded

More Related