1 / 27

Tutorial 11 Arrays

Tutorial 11: Arrays. 2. Using a One-Dimensional Array Lesson A Objectives. After completing this lesson, you will be able to:Declare and initialize a one-dimensional arrayAssign data to a one-dimensional arrayDisplay the contents of a one-dimensional arrayAccess an element in a one-dimensional a

fiorello
Download Presentation

Tutorial 11 Arrays

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. Tutorial 11: Arrays 1 Tutorial 11 Arrays

    2. Tutorial 11: Arrays 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to: Declare and initialize a one-dimensional array Assign data to a one-dimensional array Display the contents of a one-dimensional array Access an element in a one-dimensional array 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

    3. Tutorial 11: Arrays 3 Arrays A simple variable, also called a scalar variable, is one that is unrelated to any other variable in memory An array is a group of variables that have the same name and data type and are related in some way Although arrays in Visual Basic .NET can have as many as 60 dimensions, the most commonly used arrays are one-dimensional and two-dimensional Programmers use arrays to store related data in the internal memory of the computer

    4. Tutorial 11: Arrays 4 One-Dimensional Arrays A one-dimensional array is simply a row (or column) of variables A two-dimensional array resembles a table in that it has rows and columns Each element in an array is identified by a subscript, which Visual Basic .NET assigns to the variable when the array is created You refer to an array element by the array’s name followed by the element’s subscript

    5. Tutorial 11: Arrays 5 One-Dimensional Array

    6. Tutorial 11: Arrays 6 Declaring an Array Version 1 accessibility arrayname(highestSubscript) As datatype Version 2 accessibility arrayname() As datatype = {initialValues} These statements create and initialize the array variables in memory accessibility is Dim, Public, or Private

    7. Tutorial 11: Arrays 7 Declaring an array Dim strCitys(3) As String Private intNumbers(5) As Integer Private udtItems(4) As ItemStruc Private strStates() As String = {“Hawaii”, “Alaska”, “Maine”} Dim intScores() As Integer = {75, 9, 23, 6}

    8. Tutorial 11: Arrays 8 Storing Data in a One-Dimensional Array You can use a variety of ways to enter data into an array strMonthArray(0) = “Jan” strMonthArray(1) = “Feb” strMonthArray(2) = “Mar” strMonthArray(3) = “Apr” strMonthArray(4) = “May” strMonthArray(5) = “June”

    9. Tutorial 11: Arrays 9 Assigning Values to Array Elements For intNum = 1 to 6 intSquareArray(intNum - 1) = intNum * intNum Next intNum For intNum = 0 to 10 sngNum(intNum) = Val(InputBox(“Enter number”)) Next intNum udtItems(0).sngPrice = udtItems(0).sngPrice * 1.1

    10. Tutorial 11: Arrays 10 One-Dimensional Array Dim intX As Integer Do While (intX <= 5 AndAlso Not EOF(1)) FileGet(1, udtItems(intX), intX + 1) intX += 1 Loop

    11. Tutorial 11: Arrays 11 Display the Contents of a One-Dimensional Array Dim strMonths() As String = {“JAN”, “FEB”, “MAR”, “APR”, “MAY”, “JUN”, “JUL”, “AUG”, “SEP”, “OCT”, “NOV”, “DEC”} Dim intX As Integer For intX = 0 To strMonths.Length - 1 Me.MonthListBox.Items.Add(strMonths(intX)) Next intX

    12. Tutorial 11: Arrays 12 Searching a One-Dimensional Array Dim intSales() As Integer = {45000, 35000, 25000, 60000, 23000} Dim intX As Integer ‘keeps track of subscripts Dim intCount As Integer ‘counter variable Dim intSearchFor As Integer ‘number to search for intSearchFor = Val(InputBox(“Enter sales to search for:”, _ “Sales”)) For intX = 0 To 4 If intSales(intX) > intSearchFor Then intCount += 1 End If Next intX MessageBox.Show(“Count: ” & intCount, “Sales”, _ MessageBoxButtons.OK, MessageBoxIcon.Information)

    13. Tutorial 11: Arrays 13 Calculating the Average Amount Stored in a One-Dimensional Numeric Array Declare variables Repeat for each score add array score to intTotal variable End repeat for intNum Calculate the average by dividing intTotal by number of scores Display the average

    14. Tutorial 11: Arrays 14 Calculating the Average Amount Stored in a One-Dimensional Numeric Array Dim intScores() As Integer = {98, 100, 56, 74, 35} Dim intX As Integer ‘keeps track of subscripts Dim intTotal As Integer ‘accumulator variable Dim sngAvg As Single ‘average score For intX = 0 To intScores.Length - 1 intTotal += intScores(intX) Next intX sngAvg = intTotal / intScores.Length MessageBox.Show(“Average: ” & sngAvg, “Average”, _ MessageBoxButtons.OK, MessageBoxIcon.Information)

    15. Tutorial 11: Arrays 15 Determining the Highest Value Stored in a One-Dimensional Array Declare variables Assign first array value (zero) to intHigh variable Initialize the intX variable to 1 (second subscript) Repeat while intX is less than the number of elements in the array If current array value > intHigh value then assign current array value to intHigh End If End repeat for intNum Display the highest value(stored in intHigh)

    16. Tutorial 11: Arrays 16 Updating the Values Stored in a One-Dimensional Array Declare variables Prompt user for the value to add Repeat for each price in the array add value to current array value display the contents of the current array element End repeat

    17. Tutorial 11: Arrays 17 Sorting the Data Stored in a One-Dimensional Array declare an Integer array named intNumbers declare an Integer variable named intX open the random access file named nums.data repeat while intX is less than or equal to 5 and it is not the end of the nums.data file read a number from the file and store it in the current array element display the contents of the current array element in a message box add 1 to the intX variable end repeat close the nums.data file sort the intNumbers array in ascending order using the Array.Sort method repeat for each element in the intNumbers array display the contents of the current array element in a message box end repeat

    18. Tutorial 11: Arrays 18 Summary of Array Methods

    19. Tutorial 11: Arrays 19 More on One-Dimensional Arrays Lesson B Objectives After completing this lesson, you will be able to: Create and manipulate parallel one-dimensional arrays Create and manipulate a one-dimensional array of structures

    20. Tutorial 11: Arrays 20 Parallel Arrays Arrays that are related by an element’s position (subscript) Searching one array gives you the subscript for the other array Dim strId() As String = {"BX35”, “CR20", “FE15”, “KW10”, “MM67”} Dim intPrice() As Integer = {13, 10, 12, 24, 4} Dim intX As Integer, strSearchFor As String strSearchFor = UCase(IdTextBox.Text) ‘search the array Do While intX < strId.Length AndAlso strSearchFor <> strId(intX) intX += 1 Loop

    21. Tutorial 11: Arrays 21 An Array of Structures Declare an ItemStruc array named udtPriceList Declare variables intX and strSearchFor Assign IDs and prices to the udtPriceList array Assign the product ID entered in the IdTextBox control, converted to uppercase, to the strSearchFor variable Repeat while intX is less than the list length and the value of strSearchFor variable is not equal to the value stored in the current array element’s strId field Increment intX End repeat If the intX variable contains a number that is less than the lists length display the appropriate price from the intPrice field in the array Else display the message “Product ID is not valid” End if

    22. Tutorial 11: Arrays 22 An Array of Structs

    23. Tutorial 11: Arrays 23 Using a Two-Dimensional Array Lesson C Objectives After completing this lesson, you will be able to: Create and initialize a two-dimensional array Store data in a two-dimensional array Manipulate a two-dimensional array

    24. Tutorial 11: Arrays 24 Two-Dimensional Arrays A two-dimensional array resembles a table in that the variables are in rows and columns

    25. Tutorial 11: Arrays 25 Two-Dimensional Arrays

    26. Tutorial 11: Arrays 26 Storing data in a Two-dimensional Array

    27. Tutorial 11: Arrays 27 Calculating the Total in a Two-Dimensional Array

    28. Tutorial 11: Arrays 28 Multidimensional Array Properties Length will tell you the total number of elements intSales.Length will return 12 Use GetLength(dimension) to get the size of a particular dimension intSales.GetLength(0) will return 6 intSales.GetLength(2) will return 2

More Related