1 / 35

Chapter 11 One-Dimensional Arrays

Chapter 11 One-Dimensional Arrays. Chapter 11 Topics. Atomic and composite data types Declaring and instantiating an array The length of an array Manipulating the elements in an array Using an array to count frequencies Passing an array to a method. VB .NET Data Types.

harken
Download Presentation

Chapter 11 One-Dimensional 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. Chapter 11 One-Dimensional Arrays

  2. Chapter 11 Topics • Atomic and composite data types • Declaring and instantiating an array • The length of an array • Manipulating the elements in an array • Using an array to count frequencies • Passing an array to a method

  3. VB .NET Data Types VB .NET Data Types primitive Reference Integral Boolean Floating Point Array Interface Class Byte Char Short Integer Long Single Double

  4. 11.1 Scalar Data Type A scalar data type is a type in which • the values are ordered and each value is atomic (indivisible(不可分割)) • Integer, Single, Double and Char data types are scalar.

  5. Declare variables to store and total 3 blood pressures Dim bp0, bp1, bp2 As Integer Dim total As Integer bp0 bp1 bp2 Total = bp0 + bp1 + bp2

  6. 11.2 Composite Data Type A composite data type(複合資料型態) is a type that • allows a collection of values to be associated with an identifier of that type. • In VB .NET, composite types are either classes, interfaces, or arrays. • There are 2 forms of composite types: unstructured and structured.

  7. Structured Data Type(結構化) A structured data type is a type which • is an organized collection of components; and allows individual components to be stored and retrieved. • The organization determines the method used to access individual components. • An array is a structured data type whose components are accessed by position.

  8. What if you wanted to store and total 1000 values? Dim value(1000) As Integer ' declares and instantiates (creates) ' an array of 1000 int values ' and initializes all 1000 elements to zero 0 0 0 . . . . 0 value(0) value(1) value(2) . . . . value(999)

  9. 11.3 Arrays(陣列) Arrays are data structures(資料結構) consisting of related data items all of the same type(相同資料型態). • An array type is a reference type. Contiguous memory locations(連續記憶體位置) are allocated for the array, beginning at the base address of the array. • A particular element in the array is accessed by using the array name together with the position of the desired element in square brackets. The position is called the index or subscript(索引).

  10. Dim angle(4) As Single angle

  11. Dim angle(4) As Single angle angle ( 0) angle ( 1) angle ( 2 ) angle ( 3 )

  12. Array Definition An array is a collection of elements, all of the same data type(相同資料型態), given a single name. The subscript (or index) must have an integral value. In VB .NET, the first array element always has subscript 0. The second array element has subscript 1, etc. When allocated, the elements are automatically initialized to 0 for numeric primitive data type values, to False for Boolean variables, or to Nothing for references (non-primitive type values).

  13. Another Example • Declare and instantiate an array called angle to hold 4 individual double values. Dim angle ( 4 ) As Single ' declares and allocates memory number of elements in the array 0.0 0.0 0.0 0.0 angle(0) angle(1) angle(2) angle(3) indexes or subscripts

  14. Using an initializer list in a declaration Dim age( ) As Integer = { 23, 10, 16, 37, 12} Dim i As Integer For i = 0 to ages.Length – 1 Console.WriteLine("age("& i & ") = "& age(i) ) Next i 23 10 16 37 12 age(0) age(1) age(2) age(3) age(4)

  15. Declaring and Allocating an Array • An array can be declared and allocated memory in one statement or an array can be declared in one statement and another statement ca be written later to allocate memory for the array, using Redim SYNTAX FORMS Dim ArrayName(Int Expression) As DataType

  16. Assigning values to individual array elements Dim angle(4) As Single ' creates array Dim m As Integer = 3 angle(0) = 4.93 angle(1) = -15.2 angle(2) = 0.5 angle(3) = 1.67 angle(m) = angle (3) – 1.2 4.93 -15.2 0.5 1.67 angle(0) angle(1) angle(2) angle(3)

  17. Exmples angle(2) = 9.6 angle(2) = CDlb(inFile.ReadLine()) outFile.WriteLine(angle(2)) y = Math.Sqrt(angle(2)) x = 6.8 * angle(2) + 7.5

  18. What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = 100.0 + m * 0.2 Next ? ? ? ? ? temps(0) temps(1) temps(2) temps(3) temps(4)

  19. What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = 100.0 + m * 0.2 Next ? ? ? ? ? temps(0) temps(1) temps(2) temps(3) temps(4)

  20. Now what values are printed? Const ARRAY_SIZE As Integer = 5 ' named constant Dim temps( ) As Double Redim temps(ARRAY_SIZE) Dim m As Integer For m = temps.Length – 1 To 0 Step –1 Console.WriteLine ("temps(“ & m & ") = " & temps(m)) 100.0 100.2 100.4 100.6 100.8 temps(0) temps(1) temps(2) temps(3) temps(4)

  21. Variable subscripts Dim temps(4) As Double Dim m As Integer = 3 . . . . . . What is temps( m + 1 ) ? What is temps( m ) + 1 ? 100.0 100.2 100.4 100.6 100.8 temps(0) temps(1) temps(2) temps(3) temps(4)

  22. More about array index • Array index can be an integral expression of type Char,Short,Byte, or Integer. • It is programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the array’s length minus 1. • Using an index value outside this range throws an IndexOutOfBoundsException. Prevent this error by using public instance method length.

  23. Aggregate Array Operations Dim numbers( ) As Integer = {2, 4, 6} Dim values(3) As Integer values(0) = 2 values(1) = 4 values(2) = 6 If (numbers = values) then … numbers values numbers( 0) numbers( 1) numbers( 2 ) values( 0) values( 1) values( 2 ) 2 2 4 4 6 6

  24. Aggregate Array Operations numbers = values If (numbers = values) then … values numbers numbers( 0) numbers( 1) numbers( 2 ) values( 0) values( 1) values( 2 ) 2 2 4 4 6 6

  25. 11.4 Examples of Declaring and Processing Arrays occupants • Occupancy Rates Const BUILDING_SIZE as Integer = 350 Dim occupants(BUILDING_SIZE) AS Integer Dim totalOccupants As Inteer totalOccupants = 0 For counter = 0 To occupants.Length – 1 totalOccupants = totalOccupants +occupants(counter) Next counter occupants( 0) occupants( 1) occupants( 2 ) . . . . occupants( 349 )

  26. Sales Figures Dim gourmetBurgers (5) As Double gourmetBurgers gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) 246.41 271.04 350.13 640.69 177.42

  27. Using arrays for counters • Write a program to count the number of each alphabet letter in a text file. letter ASCII ‘A’ 65 ‘B’ 66 ‘C’ 67 ‘D’ 68 . . . . . . ‘Z’ 90 datafile.dat This is my text file. It contains many things! 5 + 8 is not 14. Is it?

  28. Dim letterCount(25) As Integer letterCount ( 0 ) 2 letterCount ( 1 ) 0 . . . . . . letterCount ( 24) 1 letterCount ( 25 ) 0 counts ‘A’ and ‘a’ counts ‘B’ and ‘b’ . . . counts ‘ Y’ and ‘y’ counts ‘Z’ and ‘z’

  29. Counting Frequency of Alphabetic Characters Dim letter As Integer While (dataFile.Peek<> -1) letter = (Char(dataFile.Read ( ) ) If ((letter >= "A"c And letter <= "Z"c ) Or _ ((letter >= "a"c And letter <= "z"c ) Then index = (Int (Asc (letter.ToUpper(letter)) – Asc ("A")) letterCount(index) = letterCount (index) + 1 End If End While

  30. Printing Frequency of Alphabetic Characters ' print each alphabet letter and its frequency count For index = 0 To letterCount.Length – 1 outFile.WriteLine ("The total number of " & _ Char(index + (Int (Asc ("A"))) & _ " " & letterCount (index)) Next

  31. Dim groceryItems (10) As String 11.5 Array Of Objects (Strings) groceryItems ( 0 ) “cat food” ( 1 ) “rice” . . . . . . ( 8 ) “spinach” ( 9 ) “butter”

  32. Dim groceryItems(10) As String groceryItems Expression Class/Type groceryItems Array groceryItems(0) String groceryItem(0).Chars(0) Char ( 0 ) “cat food” ( 1 ) “rice” . . . . . . ( 8 ) “spinach” ( 9 ) “butter”

  33. 11.6 Passing Arrays as Arguments • In VB .NET an array is a reference type. What is passed to a method with an array parameter is the address of where the array object is stored. • The name of the array is actually a reference to an object that contains the array elements and the public instance variable Length.

  34. Passing an Array as Parameter Public Function sumSales (ByRef data( ) As Integer) As Integer Dim sum As Double = 0 Dim index As Integer For index = 0 To data.Length – 1 sum = sum + data ( index ) Next index Return sum End Function

  35. Passing an Array as Parameter Dim gourmetBurgers (5) As Double outFile.WriteLine (sumSales(gourmetBurgers)) gourmetBurgers gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) 246.41 271.04 350.13 640.69 177.42

More Related