Arrays - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Arrays

Description:

teamName(1) = 'Red Sox' teamName(2) = 'Giants' teamName(3) = 'White Sox' teamName(4) = 'Cubs' ... Red Sox Giants White Sox Cubs Cubs. teamName(1) Array Name. Index ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 15
Provided by: fay46
Category:
Tags: arrays | red | sox

less

Transcript and Presenter's Notes

Title: Arrays


1
Chapter 7
  • Arrays

2
Outline and Objective
  • Arrays in Visual Basic
  • One- dimensional arrays
  • Control arrays
  • Two-dimensional arrays
  • Searching
  • Sorting

3
Array verses Simple Variable
  • Simple variable is used to store a single value.
  • Array variable is used to represent many values
    of the same type with one variable name.

4
Elements of an Array
  • Array Name A valid variable name for the
    structure.
  • Subscript or Index A value that refers to a
    particular array element.
  • Element An individual data item within an array.

5
Array Declaration
  • Syntax
  • Dim arrayname ( 1 To n) As VarType

6
Examples of arrays
  • Dim month ( 1 To 12) As String
  • Dim score (1 To 30) As Single
  • Dim students (1 To 30 ) As String

7
The Dim Statement
  • Used when you need to define an array .
  • The Dim statement sets up storage for array.
  • A Dim statement must occur before the first
    reference to the array elements.

8
Initializing an Array
  • Private Sub cmdWhoWon_Click()
  • Dim teamName( 1To 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"
  • n Val(txtNumber.Text)
  • picWinner.Print "The " teamName(n) " won
    World Series number" n
  • End Sub

9
Array teamName()
Array Name
  • teamName( 1 To 5) As String

Red Sox Giants White Sox Cubs Cubs
teamName(1)
Index
10
Initializing an Array by Reading from a File
  • Dim student ( 1 To 30) As String
  • Open STUDENTS.TXT For Input As 1
  • For count 1 To 30
  • Input 1, student ( count )
  • Next count

11
Adding Rows in an Array
  • Dim score( 1 To 30) As Single, student (1 To 30)
    As String
  • Open STUDENT.TXT For Input As 1
  • For count 1 To 30
  • Input 1, student (count), score (count)
  • Next count
  • sum 0
  • For count 1 To 30
  • sum sum score(count)
  • Next count
  • average sum/30

12
Parallel Arrays
  • Two arrays are referred to as parallel if
    subscripted variables having the same subscript
    are related.

13
Example of Parallel Arrays
  • Dim nom ( 1 To 8) As String , score ( 1 To 8 ) As
    Integer
  • Open SCORE.TXT For Input As 1
  • For student 1 To 8
  • Input 1, nom(student), score(student)
  • Next student
  • Close 1

14
Homework
  • P. 323 2, 4, 6, 8, 10, 12, 14
  • 30, turn in printout of code
Write a Comment
User Comments (0)
About PowerShow.com