You are on page 1of 9

Arrays Lab 2

OBJECTIVES
Recall the array declaration
Demonstrate types of arrays
Explain the for loop procedure used in arrays
Syntax for declaring an array in Visual Basic
Dim GreekLetter(4) As String

Dim – keyword to define the declaration


GreekLetter – name of the array
(4) – size of the array
As String – data type of the array items
In Visual Basic the size of the array is defined by stating the final index
number. The first index position is zero is assumed. The range of this
array is [0 : 4], representing five individual values.
Features:
An Array variable can store more than one value in it.
Each array has its unique index, therefore the values can only be called by the
index of array. Only one attribute/data type can be set for an array variable,
means all the data stored in it must of same data type.

1. Array Declaration:
Pseudocode: Set num[1:5] OR num[1:5]
VB : Dim num(5) as integer

Initializing an Array or assigning values to an array :

Dim GreekLetter() As String = { “a”, “b”, “c”, “d”, “e”}


Declaring a One – Dimensional Arrays
Dim numbers(5) As Integer
In the above snippet, numbers is the name of the array, and the number 5 included in the parentheses is the upper
limit of the array. The above declaration creates an array with 6 elements, with index numbers running from 0 to 5.
If we want to specify the lower limit, then the parentheses should include both the lower and upper limit along with
the To keyword. An example for this is given below.
Dim numbers (1 To 6 ) As Integer
In the above statement, an array of 6 elements is declared but with indexes running from 1 to 6.

Multidimensional Arrays
Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables of values
consisting of information arranged in rows and columns. To identify a particular table element, we must specify two
indexes: The first (by convention) identifies the element's row and the second (by convention) identifies the
element's column. Note that multidimensional arrays can have more than two dimensions
Tables or arrays that require two indexes to identify a particular element are called two dimensional arrays. . Visual
Basic supports at least 60 array dimensions, but most people will need to use more than two or three dimensional-
arrays.
The following statement declares a two-dimensional array 50 by 50 array within a procedure.
Dim twoDarray(10,
Dim AvgMarks ( 50, 20) 50
As )
Integer 'a two dimensional array of integers Dim ranges(10,
100) 'a two dimensional array
Program to input an array value at a given index
Public Class Form1
Dim Task(3) As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim Index As Integer = 0
Dim DataItem As Integer = 0
Index = TBIndex.Text
DataItem = TBDataItem.Text
Task(Index) = DataItem

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Dim IndexOut As Integer = 0
IndexOut = TBIndexOut.Text
TBDisplay.Text = Task(IndexOut)
End Sub
7

For Loop
✖ Repeats a selection of code predetermined number of times. When the number of repetition
is known or can be calculated.
✖ These are also known as ‘Count-Controlled’ loops because the number of iteration is
controlled by a loop counter. Commonly variable ‘i’ is used for the counter variable.
✖ A code is placed within the For loop which will be repeated.
✖ Syntax:
For i = 1 To 10
‘ Code to execute’
Next
✖ The loop counter may have reached the final value, but Next will increase it to next number
before passing to For. The value will be outside of the criteria so the program will then exit
loop.
NOTE: if the array is declared as num[1:10], that means it has indexes from 0 till 10.
Then: FOR – TO – NEXT LOOP must be from 1 TO 10
it should be used as:
FOR a = 0 TO 9 if we use

Input num(1)
Input num(2)
Input num(3)
Input num(4)
Input num(5)
Input num(6)
Input num(7)
Input num(8)
Input num(9)
Input num(10)
We may alternatively use:
num[1:10]
FOR a = 1 to 10
Input num(a)
NEXT
This techniques is even convenient for multiple variables of same attribute.
Array is used in two ways:
1. Declaration
2. Manipulation (Initialization/input/calculations/output)
1. Array Declaration:

Pseudocode: Set num[1:5] OR num[1:5]


VB 2010 Program: Dim num(5) as integer

2. Use/Manipulation:
FOR a = 1 to 5
Input num(a)
NEXT

You might also like