Download
array random n.
Skip this Video
Loading SlideShow in 5 Seconds..
Array & Random PowerPoint Presentation
Download Presentation
Array & Random

Array & Random

103 Views Download Presentation
Download Presentation

Array & Random

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Array & Random Skill Area 306.3 Materials Prepared by DhimasRuswanto, BMm

  2. Lecture Overview • Array • Random-Number Generator

  3. Array • An array is a group of memory locations all containing data items of the same name and type. • Array names follow the same conventions that apply to other identifiers.

  4. DimmyNumber(4) AsInteger Declaring Array type name of array index • An array starts counting at zero, and the first position in your array will be zero • An index must be either zero, a positive integer or an integer expression that yieldsa positive result

  5. DimmyNumbers(4) AsInteger Allocating Array myNumbers(0) = 1 myNumbers(1) = 2 myNumbers(2) = 3 myNumbers(3) = 4 myNumbers(4) = 5 value

  6. DimmyNumbers(4) AsInteger Displaying Array myNumbers(0) = 1 myNumbers(1) = 2 myNumbers(2) = 3 myNumbers(3) = 4 myNumbers(4) = 5 MsgBox("First Number is: " & myNumbers(0)) MsgBox("Second Number is: " & myNumbers(1)) MsgBox("Third Number is: " & myNumbers(2)) MsgBox("Fourth Number is: " & myNumbers(3)) MsgBox("Fifth Number is: " & myNumbers(4))

  7. DimMyNumbers(4) AsInteger DimiAsInteger MyNumbers(0) = 10 MyNumbers(1) = 20 MyNumbers(2) = 30 MyNumbers(3) = 40 MyNumbers(4) = 50 Fori = 0 To 4 ListBox1.Items.Add(MyNumbers(i)) Next Example Array

  8. DimMyText(4) AsString DimiAsInteger MyText(0) = "This" MyText(1) = "is" MyText(2) = "a" MyText(3) = "String" MyText(4) = "Array" Fori = 0 To 4 ListBox1.Items.Add(MyText(i)) Next Example Array and Strings

  9. Non-Fixed size Array or Dynamic Array Dim numbers() AsInteger DimstartAtAsInteger DimendAtAsInteger Dim times AsInteger DimstoreAnswerAsInteger DimiAsInteger times = Val(TextBox1.Text) startAt = Val(TextBox2.Text) endAt = Val(TextBox3.Text) ReDim numbers(endAt) Fori = startAtToendAt storeAnswer = i * times numbers(i) = storeAnswer ListBox1.Items.Add(times & " times " & i & " = " & numbers(i)) Next Assigning Values to Array Assigning values from TextBox ReDim = reset an array then specify the new values

  10. Random-Number Generator DimobjRandomAs Random = New Random() DimintRandomAsInteger =() • The first statement declares objRandomas a reference of type of Random and assigns it a Random object. • A reference is a variable to which you assign an object. • Recall that keyword New creates a new structure value or a new instance of an object in memory. • The second statement declares Integer variable intRandom. • It then assigns to it the value returned by calling Random ’s Next method on objRandomusing the dot operator

  11. Method Call and Resulting Range

  12. DimobjRandomAs Random = New Random() DimintRandomAsInteger = objRandom.Next(3) TextBox1.Text = intRandom Examples Random Example 1: Example 2: DimobjRandomAs Random = New Random() DimintRandomAsInteger = objRandom.Next(3) Dim number AsInteger() = NewInteger() {1, 5, 10} TextBox1.Text = number(intRandom) Example 3: DimobjRandomAs Random = New Random() DimintRandomAsInteger = objRandom.Next(3) Dim name AsString() = NewString() {"Hafiz", "Syazwan", "Irfan"} TextBox1.Text = name(intRandom)

  13. --- continue to next slide ---