1 / 61

Microsoft Visual Basic 2005: Reloaded Second Edition

Microsoft Visual Basic 2005: Reloaded Second Edition. Chapter 8 Arrays. Objectives. After studying this chapter, you should be able to: Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional array

stormy
Download Presentation

Microsoft Visual Basic 2005: Reloaded Second Edition

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. Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays

  2. Objectives After studying this chapter, you should be able to: • 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 2005: Reloaded, Second Edition

  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 2005: Reloaded, Second Edition

  4. Objectives (continued) • 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 2005: Reloaded, Second Edition

  5. Using Arrays • Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory • Array: • A group of variables with the same name and data type that are related in some way • Used to temporarily store related data in memory • Increases the efficiency of a program • Commonly used arrays: • One-dimensional • Two-dimensional Microsoft Visual Basic 2005: Reloaded, Second Edition

  6. One-Dimensional Arrays • One-dimensional array: • Can be viewed as a column of variables • Subscript: • A unique number that identifies each variable in a one-dimensional array • Starts at 0 for first element in the array • Use array name and subscript to refer to each individual variable in the array Microsoft Visual Basic 2005: Reloaded, Second Edition

  7. One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  8. One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  9. One-Dimensional Arrays (continued) • Element: an individual variable in the array • When an array is declared: • Each element in the array is initialized if no values are provided • Initial values can be specified for elements Microsoft Visual Basic 2005: Reloaded, Second Edition

  10. Storing Data in a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  11. Manipulating One-Dimensional Arrays • Array elements can be used like any other variable • Examples: • Display the contents of an array • Access an array element using its subscript • Search the array • Calculate the average of data stored in a numeric array • Find the highest value stored in an array • Update array elements • Sort array elements Microsoft Visual Basic 2005: Reloaded, Second Edition

  12. Displaying the Contents of a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  13. Displaying the Contents of a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  14. Displaying the Contents of a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  15. The For Each…Next Statement • For Each…Next statement: • Used to code a loop which processes each element in a group or array • Creates a variable used to represent each item in the group or array • Data type of the element must match the data type of the group Microsoft Visual Basic 2005: Reloaded, Second Edition

  16. The For Each…Next Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  17. Using the Subscript to Access an Element in a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  18. Using the Subscript to Access an Element … (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  19. Using the Subscript to Access an Element … (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  20. Searching a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  21. Searching a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  22. Searching a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  23. Searching a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  24. Calculating the Average Amount Stored in a One-Dimensional Numeric Array • Sample application: • Find the average from an array of test scores • Length property: number of elements in the array Microsoft Visual Basic 2005: Reloaded, Second Edition

  25. Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  26. Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  27. Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  28. Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  29. Determining the Highest Value Stored in a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  30. Determining the Highest Value Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  31. Determining the Highest Value Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  32. Updating the Values Stored in a One-Dimensional Array Microsoft Visual Basic 2005: Reloaded, Second Edition

  33. Updating the Values Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  34. Updating the Values Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  35. Updating the Values Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  36. Sorting the Data Stored in a One-Dimensional Array • Sorting: arranging data in a specific order • Ascending: first element is smallest, last element is largest • Descending: first element is largest, last element is smallest • Array.Sort method: used to sort elements in a one-dimensional array in ascending order • Array.Reverse method: used after Array.Sort method to change to descending order Microsoft Visual Basic 2005: Reloaded, Second Edition

  37. Sorting the Data Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  38. Sorting the Data Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  39. Sorting the Data Stored in a One-Dimensional Array (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  40. Parallel One-Dimensional Arrays Microsoft Visual Basic 2005: Reloaded, Second Edition

  41. Parallel One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  42. Parallel One-Dimensional Arrays (continued) • Parallel arrays: • Two or more arrays whose elements are related by their position in the arrays Microsoft Visual Basic 2005: Reloaded, Second Edition

  43. Parallel One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  44. Parallel One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  45. Parallel One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  46. Parallel One-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  47. Two-Dimensional Arrays • Two-dimensional array: • Resembles a table with rows and columns • Each element is identified by a unique combination of two subscripts: (row, column) • Subscripts are zero-relative Microsoft Visual Basic 2005: Reloaded, Second Edition

  48. Two-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  49. Two-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  50. Two-Dimensional Arrays (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

More Related