Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.

Similar presentations


Presentation on theme: "Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales."— Presentation transcript:

1 Arrays One dimensional arrays

2 Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales

3 arrays Arrays are multi-entry data structures. One dimensional arrays have a fixed length associated with the array name, once they have been built. –For this reason (one-dim) arrays are called “linear” data storage –For this reason arrays are called “static” data storage – the allocation may not be changed later. One dimensional arrays have a datatype associated with them, the type they may hold. For this reason arrays are called “homogeneous”.

4 Arrays Arrays are declared specifying a size. Examples might be: Dim calendarEntries(365) as Date Dim bestFriends(3) as String Dim list(5) as integer Dim values(25) as Double Arrays can be allocated at compile time, in which case their size is calculated based on how many entries are provided, as in Dim numbers as double()={1.1,2.2,3.3}

5 arrays Because arrays have fixed size, once allocated, loops are useful for processing them. Array indices are zero-based (count subscripts starting at zero) just as String indices are in VB. There are a number of fields/methods useful for arrays such as: length, getType, initialize(), reverse(), lastIndexOf

6 A loop is generally used to process an array ‘ if we have built an array: Dim list(5),i as integer ‘ then later…. For i=0 to list.length-1 ‘ process elements here… for example List(i)=0 ‘a typical initialization Next

7 We could “glue together” names Dim longstring as string=“ “ Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_,"booper", "shakespeare", "nerd"} For i=0 to picnames.length-1 Longstring = longstring & “ “ & picnames(i) Next

8 We could search for a name Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_,"booper", "shakespeare", "nerd"} Dim name as string Dim found as boolean=false Name=txtinput.text For i=0 to picnames.length-1 If name= picnames(i) then Found=true End if Next

9 A form which displays random pictures Uses our array of names since I found those gif files. Uses techniques we used in our “flags-of- the-world” application to display pictures

10 A form which displays random pictures

11 Clicking random button

12 This form uses A string array declared as a field value and initialized at time of declaration A random object to pick random entries – remember parens are used when allocating an object with the new keyword A picture box to display pictures Gets the image from a file using system.io Dim randomguy As Random = New Random() Dim picnames As String() = {"lisa", "bart", "dollars", "robot", "booper", "shakespeare", "nerd"}

13 Random class The next (or nextint()) function gets the next random value. You can give the next a start and final value and it will pick a random int from start to final-1 If you use random class to generate reals, it gives reals in the range (0,1). To get other reals multiply and shift.

14 Code for btnrandom Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click Dim val As Integer val = randomguy.Next(0, picnames.Length) picture.Image = Image.FromFile(directory.GetCurrentDirectory & picnames(val) & ".gif") End Sub

15 Debugger note In VB 2003 the debugger expects files to be named “bin”&name.gif since the debug version of your project is in a bin subdirectory We noticed in VB2005 it didn’t work the same.

16 More on arrays and the debug window

17 For each item in array…next For each is a useful mechanism when the number of items in an array is not known: Private Sub Btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click Dim Name As String For Each Name In names Debug.WriteLine(Name) ’write to debug window Next End Sub

18 Now display to listbox

19 Array allocation and button code Dim names As String() = {"bob", "sue", "mary", "jeremiah", "opie", "jezebel"} ‘put on one line or use line continue ‘this is compile time array allocation Private Sub Btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click Dim Name As String For Each Name In names Debug.WriteLine(Name) lstdisplay.Items.Add(Name) Next End Sub

20 Common activites Arrays often need to be searched for a specific value Largest or smallest value may be sought. Arrays may need to be summed (if contents are numeric)

21 Allocate an array and sum its contents Dim values As Double() = {1.2, 2.3, 3.4, 5.6, 6.7, 7.8, 8.9, 9.0} Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sum As Double = 0 'set sum to zero…additive identity element Dim guy As Double For Each guy In values sum += guy Next MessageBox.Show("sum is" & sum, "The sum", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

22 Find the largest (or smallest)

23 Revised loadform code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim big As Double = values(0) 'set big to first one Dim guy As Double For Each guy In values If guy > big Then big = guy End If Next MessageBox.Show("largest is" & big, "findbiggest", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

24 Look for mary

25 Mary found

26 Look for bozo

27 Can’t find him

28 One version of search code: remember, the user may need a reminder about case-sensitivity Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim found As Boolean = False search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then MessageBox.Show(search & "is found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) found = True End If Next 'after loop you'll know if he's not there If Not found Then MessageBox.Show(search & "is not found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End If End Sub

29 Another version of search code Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim found As Boolean = False search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then found = True End If Next 'after loop you'll know if he's not there If found Then MessageBox.Show(search & "is found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) else MessageBox.Show(search & "is not found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End If End Sub

30 Yet another version of search code Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim message as String = “Not there” ‘ must assume won’t be found search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then Message=“Is there” End If Next 'after loop you'll know if he's not there…no if needed MessageBox.Show(search & message, "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

31 Car sales

32 We have several employees and car models This application uses a combobox You can populate it from the properties window by clicking the ellipsis and entering strings into the dialog box

33 combobox

34

35 Arrays needed for car sales ‘which one? Dim list As Integer() = {0, 0, 0, 0} ‘ name of car sold Dim values(4) As String ’car costs Dim prices as double={50000,200000,30000,12000} ‘worker names Dim employees() as String={“names”,”go”,”here”}

36 Keep track of individual employee sales


Download ppt "Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales."

Similar presentations


Ads by Google