skill area 306.3. array random-number generator an array is a group of memory locations all...

13
Array & Random Skill Area 306.3 Materials Prepared by Dhimas Ruswanto, BMm

Upload: claud-james

Post on 05-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Array & RandomSkill Area 306.3Materials Prepared by Dhimas Ruswanto, BMm

Page 2: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Lecture Overview

• Array• Random-Number

Generator

Page 3: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

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.

Page 4: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Declaring Array

Dim myNumber(4) As Integer

name of array index type

• 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 yields a positive result

Page 5: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Allocating Array

Dim myNumbers(4) As Integer

myNumbers(0) = 1myNumbers(1) = 2myNumbers(2) = 3myNumbers(3) = 4myNumbers(4) = 5

value

Page 6: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Displaying Array

Dim myNumbers(4) As Integer

myNumbers(0) = 1myNumbers(1) = 2myNumbers(2) = 3myNumbers(3) = 4myNumbers(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))

Page 7: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Example Array

Dim MyNumbers(4) As IntegerDim i As Integer MyNumbers(0) = 10MyNumbers(1) = 20MyNumbers(2) = 30MyNumbers(3) = 40MyNumbers(4) = 50 For i = 0 To 4  ListBox1.Items.Add(MyNumbers(i))Next

Page 8: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Example Array and Strings

Dim MyText(4) As StringDim i As Integer MyText(0) = "This"MyText(1) = "is"MyText(2) = "a"MyText(3) = "String"MyText(4) = "Array" For i = 0 To 4 ListBox1.Items.Add(MyText(i))Next

Page 9: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Assigning Values to ArrayDim numbers() As IntegerDim startAt As IntegerDim endAt As IntegerDim times As IntegerDim storeAnswer As IntegerDim i As Integer times = Val(TextBox1.Text)startAt = Val(TextBox2.Text)endAt = Val(TextBox3.Text) ReDim numbers(endAt) For i = startAt To endAt  storeAnswer = i * times numbers(i) = storeAnswer ListBox1.Items.Add(times & " times " & i & " = " & numbers(i))Next

Assigning values from TextBox

Non-Fixed size Arrayor

Dynamic Array

ReDim = reset an array then specify the new values

Page 10: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Random-Number GeneratorDim objRandom As Random = New Random()Dim intRandom As Integer =()

• The first statement declares objRandom as 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 objRandom using the dot operator

Page 11: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Method Call and Resulting Range

Method Call Resulting Range

objRandom.Next() 0 to one less than Int32.MaxValue

objRandom.Next(30) 0 to 29

10 + objRandom.Next(10) 10 to 19

objRandom.Next(10,20) 10 to 19

objRandom.Next(5,100) 5 to 99

objRandom.NextDouble() 0.0 to less than 1.0

8 * objRandom.NextDouble()

0.0 to less than 8.0

Page 12: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

Examples RandomDim objRandom As Random = New Random()Dim intRandom As Integer = objRandom.Next(3) TextBox1.Text = intRandom

Dim objRandom As Random = New Random()Dim intRandom As Integer = objRandom.Next(3)Dim name As String() = New String() {"Hafiz", "Syazwan", "Irfan"} TextBox1.Text = name(intRandom)

Dim objRandom As Random = New Random()Dim intRandom As Integer = objRandom.Next(3)Dim number As Integer() = New Integer() {1, 5, 10} TextBox1.Text = number(intRandom)

Example 1:

Example 3:

Example 2:

Page 13: Skill Area 306.3. Array Random-Number Generator An array is a group of memory locations all containing data items of the same name and type. Array names

--- continue to next slide ---