0% found this document useful (0 votes)
9 views

Lecture 8

Information Technology - Introduction about arrays

Uploaded by

prabhath mahela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 8

Information Technology - Introduction about arrays

Uploaded by

prabhath mahela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/23/2021

Information Technology IIA


Lecture 08
Arrays

By Pramitha Kalansuriya

Array Characteristics

• An array stores multiple values of same type


• Like a group of variables with a single name
• For example, the days of the week might be:
– a set of 7 string variables
– with a maximum length of 9 characters
• All variables within an array are called elements and
must be of the same data type
• You access the elements in an array through a
subscript

1
1/23/2021

Subscript Characteristics

• A subscript, also called an index, is a number that


identifies a specific element within an array
• Subscript numbering works like a list box index:
– Subscript numbering begins at 0
– 1st element in an array is always subscript 0
– Last element is total number of elements – 1
• An array with 7 elements refers to the 1st element as
subscript 0 and the last element as subscript 6

Declaring a One-Dimensional Array


Dim ArrayName(UpperSubscript) As DataType

• ArrayName is the variable name of the array


• UpperSubscript is the value of the array's
highest subscript
– Must be a positive whole number
• DataType may be any Visual Basic data type
• VB knows an array is declared when name is followed
by parentheses with number of elements

2
1/23/2021

Array Declaration Example

Dim intHours(5) As Integer

intHours(0) intHours(1) intHours(2) intHours(3) intHours(4) intHours(5)

• Note that 6 elements are available when an


array with an upper subscript of 5 is declared
• Each element is an integer type

Default Initialization

• All elements of an Integer array are initialized


to zero
– Same initialization as an integer variable
• Each array element is initialized exactly the
same as a simple variable of that data type
– A Single is initialized to zero (0.0)
– A String is initialized to Nothing

3
1/23/2021

Array Sizing & Initialization

• An array can be initialized when declared


• Example:
Dim intNumbers(1) As Integer
intNumbers(0)=3
intNumbers(1)=4

• Elements are assigned the values shown

Arrays and Loops


• Loops are frequently used to process arrays
• Use a loop to prompt for 10 values and
display total of that
Dim intNbrs(9) as Integers
Dim intCount,total As Integer
total=0
For intCount = 0 To 9
intNbrs(intCount) = InputBox("Enter number")
Next intCount

For i=0 to 9
total=total+ intNbrs(i)
Next i

Msgbox total

4
1/23/2021

Dim intNbrs(9) as Integers


Dim intCount,total As Integer
total=0
For intCount = 0 To 9
intNbrs(intCount)= InputBox("Enter number")
Next intCount

For i=0 to 9
total=total+ intNbrs(i)
Next i

Msgbox total

You might also like