Lecture 9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DATA STRUCTURES: ARRAYS AND RECORDS 

Computer Programming for Civil Engineering 

Instructor: Nikol O. Telen 


May 14, 2023 
Mindanao State University - General Santos City
Table of Contents 

1. Data Structuring 

2. Arrays 

3. Storing Data in an Array 

4. Multi-Dimensional Arrays 

5. Dynamic Arrays 

6. Records 

7. Thank You! 

Data Structuring
Data Structuring 

ˆ Data structuring refers to organizing data in a way that enhances its efficiency and ease of use,
similar to how structured programming (decisions and loop structures) constructs make
computer code easier to comprehend 
ˆ The most basic approach to data structuring is through variable type declarations using Option
Explicit and Dim, Const and Public 
statements 
ˆ In addition, two additional features facilitate more efficient and concise data organization,
Arrays and Records. 

2
Data Structuring 
ˆ An Array is a collection of values that are accessed using a single variable name and
subscripts, with all values being of the same variable type 
ˆ Records, on the other hand, allow for the storage of different variable types using a
single variable name, which is useful for 
managing databases with varying types of information. 

Arrays
Arrays 

ˆ An Array is a set of sequentially indexed elements having the same data type.  
ˆ Each element of an array has a unique identifying index number, starting at zero.  
ˆ Changes made to one element of an array don’t affect the other elements.  
ˆ Arrays function similarly as variables. As with variables, they are required to be declared.
The general syntax is: 
Dim array_name ( array_index ) As data_type

where array name is the array’s name, array index indicates the size of the array, and data type
is the data type 

4
Arrays 

Examples of Array declarations 


’Sample declaration of a 1 dimensional array 
Dim array1 (4) 

’Sample declaration of a 2 dimensional array 


Dim array2 (6 , 1) 

’Sample declaration of a dynamic array 


Dim array3 () 

Declaring the data type is optional, although in many cases this will slow down the execution of
your procedure 

5
Arrays 
Sometimes, an array is desired to start at an index number other than zero. This can be
achieved by using the To keyword and stating the index number of the first and final
elements: 
Dim array_name ( first_index To final_index ) As data_type

In cases where index numbering should start at one, the following line is added at the very start
of the module, before any other code: 
Option Base 1

6
Arrays 

Optionally, an array declaration may omit an argument from its parentheses to create a
“dynamic” array. The size of that array can be established later using the ReDim keyword before
its elements are assigned values. The array can be resized repeatedly using the ReDim
keyword, but its element values will be lost unless the statement includes the Preserve
keyword: 
Dim array_name () As data_type 
ReDim array_name ( first_index To final_index ) 
ReDim Preserve first_index To final_index

Storing Data in an Array


Storing Data in an Array 

Begin a VBA macro module with a subroutine that declares a string array of three elements 
Sub FruitArray () 

Dim Fruit (2) as String 


’statements to be inserted here 
End Sub

8
Storing Data in an Array 
Next, insert statements to assign values for each array element Sub FruitArray () 
Dim Fruit (2) as String 
Fruit (0) = " Apple " 
Fruit (1) = " Banana " 
Fruit (2) = " Cherry " 

End Sub 

Each element in the array will now receive its value 

9
Storing Data in an Array 

Display the value of the element at any index. For example, for the first fruit (index 0): 
Sub FruitArray () 

Dim Fruit (2) as String 


Fruit (0) = " Apple " 
Fruit (1) = " Banana " 
Fruit (2) = " Cherry " 

MsgBox Fruit (0) & " is Fruit No. 1" 


End Sub

10
Storing Data in an Array 

Declare another array assigning 1 as the first index and 3 as the final index: 
Sub VegArray () 

Dim Veggie (1 To 3) as String 


Veggie (1) = " Ampalaya " 
Veggie (2) = " Broccoli " 
Veggie (3) = " Cabbage " 

MsgBox Veggie (1) & " is Veggie No. 1" 


End Sub

11
Storing Data in an Array 
Next, declare a Dynamic array, then ReDim by specifying the first and final index numbers as
1 and 3 respectively: 
Sub CountryArray () 

Dim Country () as String 


ReDim Country (1 To 3) 

’insert elements 
Country (1) = " China " 
Country (2) = " Philippines " 
Country (3) = "USA " 

’display values 
MsgBox Country (1) & " Number 1" 
End Sub 

12
Using For Loop with Arrays 

A For-Next loop can be used to enter values for each element as shown below: 
Sub CountryArray () 

Dim Country () as String 


ReDim Country (1 To 3) 

’insert elements 
For i = 1 To 3 
Country (i) = InputBox (" Enter a Country ") 
Next 

’display values 
MsgBox Country (1) & " Number 1" 
End Sub 

13

Multi-Dimensional Arrays
Multi-Dimensional Arrays 

ˆ An array created with a single index is a one-dimensional array, in which elements appear in
a single row. 
ˆ Multi-dimensional arrays contain two or more indices, in which elements appear in
multiple rows. 
ˆ Two-dimensional arrays are useful in Excel VBA to store row and column information, in which
the first index represents rows and the second index represents columns. 
ˆ Multi-dimensional arrays are created in the same way as 
one-dimensional arrays, except they must specify the size of each index as comma-
separated arguments in the declaration, like this: 
Dim array_name ( final_index1 , final_index2 , final_index3 ) As data_type
14
Multi-Dimensional Arrays 

Create a VBA macro module by adding an initial directive to start index numbering at one: 
Option Base 1

Next, add a Subroutine that declares a two-dimensional array of three elements 


Option Base 1 

Sub Array2D () 

Dim Basket (3 ,3) As String 


’insert statements below 

End Sub

15
Multi-Dimensional Arrays 

Next, insert statements to initialize each array element in the first index Option Base 1 
Sub Array2D () 

Dim Basket (3 ,3) As String 


’first index 
Basket (1 ,1) = " Apple " 
Basket (1 ,2) = " Banana " 
Basket (1 ,3) = " Cherry " 

End Sub 

16
Multi-Dimensional Arrays 

Next, insert statements to initialize each array element in the first index Option Base 1 
Sub Array2D () 

Dim Basket (3 ,3) As String 


’first index 
Basket (1 ,1) = " Apple " 
Basket (1 ,2) = " Banana " 
Basket (1 ,3) = " Cherry " 

End Sub 

17
Multi-Dimensional Arrays 

Next, insert statements to initialize each array element in the second index 
Option Base 1 

Sub Array2D () 

Dim Basket (3 ,3) As String 


’first index 
Basket (1 ,1) = " Apple " 
Basket (1 ,2) = " Banana " 
Basket (1 ,3) = " Cherry " 

’second index 
Basket (2 ,1) = " Ampalaya " 
Basket (2 ,2) = " Broccoli " 
Basket (2 ,3) = " Cabbage " 

End Sub 

18
Multi-Dimensional Arrays 

Finally, insert statements to display the array elements 


Option Base 1 

Sub Array2D () 

Dim Basket (3 ,3) As String 


’first index 
Basket (1 ,1) = " Apple " 
Basket (1 ,2) = " Banana " 
Basket (1 ,3) = " Cherry " 

’second index 
Basket (2 ,1) = " Ampalaya " 
Basket (2 ,2) = " Broccoli " 
Basket (2 ,3) = " Cabbage " 

Msgbox Basket (2 ,1) 

End Sub 

19

Dynamic Arrays
Dynamic Arrays 
ˆ Dynamic arrays can be created by omitting the argument between the parentheses in an
array declaration. 
ˆ Useful when a an array involves a set of data without a fix number of values  
ˆ For this type of array, we can determine its indeces using the LBound and UBound methods,
with syntax: 
LBound ( array_name , dimension ) 
UBound ( array_name , dimension )

where array name is the name of the array and dimension is a whole number indicating which
dimension’s lower or upper bound is returned. 

20
Dynamic Arrays 

Option Explicit 

Option Base 1 ’ Set default array subscripts to 1. 

Sub Test () 

Dim x (20) As Double , y(4 , 3 To 5) As Double 


Dim t(0 To 5) As Double ’ Overrides default base subscript . 

’ Use LBound function to test lower bounds of arrays . 


MsgBox LBound (x) ’ Returns 1. 
MsgBox LBound (y, 1) ’ Returns 1. 
MsgBox LBound (y, 2) ’ Returns 1. 
MsgBox LBound (t) ’ Returns 0. 

End Sub 

21

Records
Records 

Records store any type of information in a single variable. To create a Record, a special
variable type must first be established using the Type statement: 
Type usertype 
itemname As type 
itemname As type 
itemname As type 

End Type

After establishing the type, the record can then be dimensioned: 


Dim record_name (n) As
usertype
Individual elements of the record can then be identified by appending a period followed by the
type, as in: 
varname (i). type

22
Records 

Example: 
Option Explicit 

’Establish a variable of type ChemData 


Type ChemData 
Elname As String 
Symbol As String * 3 
AtomNumber As Integer 
AtomWeight As Double 
End Type 

23
Records 

Example: 
Sub Periodic () 

Dim Chem (118) As ChemData 


Dim Msg As String , element_number As Integer 

’Identify and fill values of individual elements 


Chem (6) . Elname = " Carbon " 
Chem (6) . Symbol = "C" 
Chem (6) . AtomNumber = 6 
Chem (6) . AtomWeight = 12.011 

element_number = InputBox (" Enter the atomic number : ") 


Msg = " Atomic number = " & Chem ( element_number ). AtomNumber 
MsgBox Chem ( element_number ). Elname & vbCrLf & Chem ( element_number ). Symbol & vbCrLf & Msg & vbCrLf & Chem (
element_number ). 
AtomWeight , , Chem ( element_number ) . Elname 
End Sub 

24

Thank You!

You might also like