Wednesday, August 12, 2020

Array & Its Type

Arrays:

An array is defined as a group of elements of similar type. A variable can store one value at a time in it, but a single array can store multiple values at same time

The individual element of an array can be referred using an index which begins with 0

There might be an instance where you may need to store probably names of 10 students of 10 numbers in which case declaring 10 different variables would not be very practical. In such situations you can make use of arrays.

Advantages of arrays:


  1. All the array elements shares the same name
  2. Large amount can be handled in easy way as the individual elements of an array can be referred using an index which begins with zero
  3. Array elements are stored in a consecutive memory location

 

Declaration of an array:

Syntax

Dim variable name [{[subscript]}] as Data Types

Subscript: You need to specify the lower and the upper bounds for the subscript [lower to upper]

You can declare an array using a dim statement, followed by the name of the subscript value in parenthesis.

Types of arrays:

 The arrays two types in Visual Basic

1.  Fixed Size Arrays

2.  Variable Size Arrays

Fixed Size Arrays:- 

Fixed size array which all will always remain the same size and its size cannot be change at runtime.

Declaring fixed size arrays

Dim array name (upper _limit) as data type

 Or

Dim array name (lower limit to upper limit) as data type

Lower limit:- lower limit is  optional in a fixed size array, if not specified considered to be zero.

Upper limit:- the upper bound cannot exceed the range of long data type.

Example:

Dim marks(1 to 15) as integer

To specify only upper limit give this declaration

Dim marks(15) add integer


Option Base 1 statement

Default lower limit of an array is 0, if you want to make default lower limit to 1 for all the array in your form you can use following statement in general declarations

Option Base 1

Program to demonstrate the use of an array:

Private sub command1_click()

Dim num(5), i as integer

Dim sum As long

For i = 1 to 5

num(i)=Inputbox(“Enter the numbers”)

sum=sum+num(i)

Next i

Msgbox “Sum of given number is” &sum 

Msgbox “Average of given five number is” &sum/5

End sub

 

Dynamic Arrays:

The size of the dynamic array can be changed at run time of. You declare the array as a dynamic by giving it an empty dimension list. ReDim statements can be used to assign the size of the dynamic array. 

Syntax

Dim  array_name()

The actual number of element with ReDim statement

ReDim array_name()

The size of the dynamic array can be changed much time during the execution of program

Private sub command1_click()

Dim num() , i, sum ,x As  integer

x= input box(“ How many numbers you want”)

ReDim num(x)

For i = 1 to x

num(i)=Inputbox(“Enter the numbers”)

sum=sum+num(i)

Next i

Msgbox “sum of given number is” &sum 

Msgbox “Average of given five number is” &sum/5

End sub

Preserve Keyword:-

Sometimes users may want to change the size of the array without losing the data into the array. You can do this by using ReDim with the preserve keyword.

The preserve statements can be used to preserve the element of the dynamic array even if the array size is changed.

Syntax

ReDim preserve array_name (new_size) 

Private Sub Command1_click()

Dim num()  as integer, i as integer

ReDim num(5)

For i = 1 to x

num(i)=i*10

Next i

Msgbox “sum of given number is” &sum 

Msgbox “Average of given five number is” &sum/5

ReDim preserve num(10)

For i = 6 to 10

num(i)=i*100

Next i

For i = 1 to 10

Print num(i)

Next i

End sub

Output:

10 20 30 40 50 600 700 800 900 1000

If preserve statement is not used along with RiDim statement the result would have been 

 0 0 0 0 0 600 700 800 900 1000

Advantages of dynamic arrays:

  1.  Size of Array need not be  declare in advance
  2.  The size of an array is set at runtime using the ReDim statement
  3.  The size of an array can be changed many times during execution of program
  4.  The previous value of an array can be retained using the preserve statement along with the ReDim statement.

No comments:

Post a Comment