2-1 Arrays
-
Upload
tintin-brusola-salen -
Category
Documents
-
view
18 -
download
0
description
Transcript of 2-1 Arrays
-
Arrays. Arrays. Arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays. arrays.
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Arrays. Arrays. Arrays.
o A set of variables having the same name and data type
o a data structure that contains a number of logically related variables
o Every variable in an array is called an element and every element is accessed through its index
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
What are Array Index?
o The number appended to the array name appearing between an open and close parentheses
e.g.: HourlyData(3)
o The number of indices needed to specify an element is called dimension or rank
o In each dimension, the smallest index is always 0, and the index of each element increases by one until the last element.
o In Visual Basic, an array can have up to 32 dimensions
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
What are the Advantages of
using Arrays?
o Allows creation of several variables in one declaration
o The indices can be computed during runtime allowing the programmer to use iterative statements to process each element (e.g. tracing the each element of a matrix)
o Can represent simple to complex data models in the program
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Arrays: Types
1. Fixed-size array
Has a fixed number of elements
2. Dynamic array
The number of elements can be changed at any point during runtime
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Declaring Arrays: Fixed Size
o Keyword used is Dim o Syntax:
Dim ArrayName(UBound1 [, ,UBoundN]) As Type
o Examples: Dim TempReading(9) As Double Dim HourlyData(23) As Double Dim RainfallData(11,30) As Decimal Dim myClassmates(29) As String
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Declaring Arrays: Dynamic
o Keyword used is Dim o Syntax:
Dim ArrayName( ) As Type
o Commas can be placed inside the parentheses to specify the number of dimensions
o Examples: Dim TempReading() As Double Dim RainfallData(,) As Decimal Dim MyCube(,,) As Long
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
o Equate each element to an expression
o Example:
Dim CalorieIntake(6) As Decimal assignment statements
CalorieIntake(0) = 2300 CalorieIntake(1) = 2000
CalorieIntake(2) = 1800
up to
CalorieIntake(6) = 1700
Storing Values: Fixed Size
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Storing Values: Fixed-size
o Use the For Next loops counter to represent the index for each element
Example:
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Using Values Stored in Each
Element
o Use each element in an equation
o Examples:
txtDay1.Text = "Calorie Consumed for Day 1 is " & _ CalorieIntake(0) & " Calories.
Dim TwoDaySum As Decimal
TwoDaySum = CalorieIntake(0) + CalorieIntake(1)
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Using Values Stored in Each
Element
Getting the Sum:
Dim TotalCalorie As Decimal TotalCalorie = CalorieIntake(0) + CalorieIntake(1) _ + CalorieIntake(2) + CalorieIntake(3) + _ CalorieIntake(4) + CalorieIntake(5) + _ CalorieIntake(6)
Or For iDay As Byte = 0 To 6 TotalCalorie += CalorieIntake(iDay) Next
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Storing Values: Dynamic
o The length of the arrays must be set first before it can be used. To do this, use the ReDim statement.
o Syntax: ReDim ArrayName(UBound1[,, UBoundN]) o Examples ReDim TempReading(99) or length = txtL.Text ReDim TempReading(length 1) ReDim RainfallData(Months-1, Days-1)
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Storing Values: Dynamic
Once an array has been redimensioned, it can be manipulated like a fixed-size array
For iCount As Byte = 0 To UBound(TempReading)
sum += TempReading(iCount)
Next
Note: since the array is dynamic, the UBound function was used to determine the upperbound of the index of the array.
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
ReDim Statement
o Used only at procedure-level
o Can be invoked at any time it is needed
o Cannot change the data type of an array variable or of its elements.
o Cannot change the rank (the number of dimensions) of the array
o Can resize more than one array e.g. ReDim TempReading(99), RainfallData(11,30)
o Resets all values of each element to zero.
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
o Used with the ReDim statement in order to preserve the data currently stored in the elements of the array.
o Use:
ReDim Preserve ArrayName(newbounds)
o Example:
ReDim Preserve TempReading(999)
Preserve Keyword
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Manipulating Multi-dimensional
Arrays
o Use nested For Next Loops (level of nest = number of dimensions)
o For example:
For i As Byte = 0 To 11 For j As Byte = 0 To 30
YearlyRainfall += RainfallData(i,j)
Next
Next
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering
-
Populating an Array with Initial
Values
o You can create an array that contains an initial set of values by using an array literal.
o An array literal consists of a list of comma-separated values that are enclosed in braces ({}).
o Example:
Dim TempReading() As Single = {11, 4, 15}
o This will result to:
TempReading(0) = 11 TempReading(1) = 4
TempReading(2) = 15
ES 26 | Prepared by: Paolo Rommel P. Sanchez Email: [email protected] Computer Applications in Engineering