Arrays - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Arrays

Description:

refer to an array element by the name of the array followed ... teamName(1) = 'Red Sox' teamName(2) = 'Giants' teamName(3) = 'White Sox' teamName(4) = 'Cubs' ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 10
Provided by: scie248
Learn more at: http://www.cs.uwyo.edu
Category:
Tags: arrays | red | sox

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
2
Variable Arrays
  • a group of variables that have the same name and
    data type
  • each element in a variable array is identified by
    a subscript
  • refer to an array element by the name of the
    array followed by its subscript
  • can have as many as 60 dimensions

3
Declaring One-dimensional Arrays
  • Syntax
  • Dim arrayname(lower subscript To upper subscript)
    As datatype
  • Examples
  • Dim Friend(1 to 5) As String
  • Dim Sums(20) As Double

4
Private Sub cmdWhoWon_Click() 'Create array for
five strings Dim teamName(1 to 5) As String Dim n
As Integer 'Fill array with World Series
Winners teamName(1) "Red Sox" teamName(2)
"Giants" teamName(3) "White Sox"
teamName(4) "Cubs" teamName(5) "Cubs"
'Access array of five strings n
Val(txtNumber.Text) picWinner.Cls
picWinner.Print "The " teamName(n) " won World
Series number" n End Sub
5
Private Sub cmdShow_Click() Dim total As
Integer, student As Integer, average As Single
Dim nom(1 To 8) As String, score(1 To 8) As
Integer Open App.Path "\SCORES.TXT" For Input
As 1 For student 1 To 8 Input 1,
nom(student), score(student) Next student
Close 1 total 0 For student 1 To 8
total total score(student) Next student
average total / 8 picTopStudents.Cls For
student 1 To 8 If score(student) gt average
Then picTopStudents.Print nom(student)
End If Next student End Sub
6
Static and Dynamic Arrays
  • Static arrays have the dimensions specified in
    the array declaration. They cannot be
    redimensioned.
  • A dynamic array can have it's size changed after
    it has been declared. Dynamic arrays are declared
    without dimensions specified in the parenthesis.
  • Dim arrayname( ) As datatype

7
Redimensioning Arrays
  • Use ReDim to change the size of a dynamic array
  • If you use the ReDim command in conjunction with
    the word Preserve, you can change the size of the
    array without destroying the data contained
    inside it.

8
ReDim Example
  • Dim arrNums( ) As Integer
  • Dim intArraySize As Integer
  • intArraySize 10
  • ReDim arrNums(intArraySize)
  • For i 0 to 9
  • arrNums(i) i
  • Next
  • ReDim Preserve arrNums(20)
  • For i 10 to 19
  • arrNums(i) i
  • Next

9
Private Sub cmdShow_Click() Dim numStudents As
Integer, nTemp As String, sTemp As Integer Dim
student As Integer, total As Integer, average As
Single Dim nom() As String, score() As Integer
numStudents 0 Open App.Path "\SCORES.TXT"
For Input As 1 Do While Not EOF(1) Input
1, nTemp, sTemp numStudents numStudents
1 Loop Close 1 ReDim nom(1 To numStudents)
As String, score(1 To numStudents) As Integer
Open App.Path "\SCORES.TXT" For Input As 1
For student 1 To numStudents Input 1,
nom(student), score(student) Next student
Close 1 total 0 For student 1 To
numStudents total total score(student)
Next student average total / numStudents
picTopStudents.Print average End Sub
Write a Comment
User Comments (0)
About PowerShow.com