1 / 22

# Arrays 1 - PowerPoint PPT Presentation

Is there a better way?. Arrays 1. Motivation: to deal with large amounts of data. e.g sorting values :. Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21 Want the Output: 3, 4, 9, 10, 12, 15, 17, 21, 25, 32, 36, 48. e.g Compute the average of 7 grades:.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Arrays 1' - jael

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Arrays1

Motivation: to deal with large amounts of data

e.g sorting values:

Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21

Want the Output:

3, 4, 9, 10, 12, 15, 17, 21, 25, 32, 36, 48

e.g Compute the average of 7 grades:

Dim dblTotal, dblG1,dblG2, dblG3 As Double

Dim dblG4, dblG5, dblG6, dblG7 As Double

dblTotal = dblG1+dblG2+dblG3+dblG4 + dblG5+dblG6

MessageBox.Show(“Avg” & FormatNumber(dblTotal/7.0))

• List or series of values OF THE SAME TYPE all referenced by the same name

• Similar to list of values for list boxes and combo boxes - without the box

• Use an array to keep a series of values for later processing such as

• Reordering

• Calculating

• Printing

ArrayTerms

• Element

• Individual item in the array

• Index (or subscript)

• Zero based number used to reference the specific elements in the array

• Must be an integer

• Boundaries

• Lower Subscript, 0 by default

• Upper Subscript

George Lee

Sue Li

Samuel Hoosier

Sandra Weeks

William Macy

Andy Harrison

Ken Ford

Denny Franks

Shawn James

(0)

(1)

(2)

(3)

(4)

(5)

(6)

(7)

(8)

(9)

Simple Array Example

strName Array

• Use Dim statement to declare

• Specify the number of elements in the array as the UpperSubscript

General form:

DimArrayName(UpperSubscript) asDatatype

Example:

• Each element of the array will be assigned a default value

• Numeric ==> 0

• String ==> empty string, 0 characters

• You cannot declare the Upper Subscript. The number of elements will be determined by your entry.

• Optionally, the elements in the array may be assigned values in the Dim statement (initialized at declaration time)

General form:

DimArrayName() asDatatype = {InitialValueList}

Example:

Value. 0 is the lower index

Value. There are 4 elements

Dim Statement for Arrays Examples - Default Values

DimstrName(3) asString

Results in an array of 4 elements: strName(0), strName(1),

strName(2), strName(3)

All initialized to an empty string

DimdecBalance(99) as Decimal

Results in an array of 100 elements: decBalance(0), . . . , decBalance(99)

All initialized to an zero

DimstrDept( ) as String = {"ACT", "MKT", "HR"}

DimintActCode( ) as Integer = {10, 20, 30, 40}

(0)

(1)

(2)

(3)

What does VB do with the array?

• When the DIM statement for the array is processed VB sets aside room for it in memory.

• Ex: DimstrName(3) as String

• VB sets aside a memory location for 4 strings

(0)

(1)

(2)

(3)

Referencing Array Elements

• Use the Index(s) of the Element

strName(0) : "Sam Smith"

strName(1) : "Jill Creech"

strName(2) : "Paul Fry"

strName(3) : "Rich Wells"

Sam Smith

Jill Creech

Paul Fry

Rich Wells

No Exceptions

OK if 0 iVar1+3+iVar2  7

Index rule:

An array index must evaluate to an

Integerbetween the lower and upper

bounds of the array. If the index is not an

Integer, VB rounds it to an Integer.

Example:

...

Can have complicated expressions:

No Exceptions

Element rule:

An array element can be used wherever

a simple variable of the same type can be used

Example

• Use Loops to reference each element in the array

• For / Next

• For Each / Next

For EachElementName In ArrayName

Statements to execute

Next [ElementName]

• VB references EACH element of the array

• VB assigns its value to ElementName

• The variable used for ElementName must be same datatype as the array elements or an Object datatype

• Makes one pass through the loop per element

• Use Exit For statement within loop to exit early

For Each / Next Examples

' Assumes array strName previously dimensioned

Dim strOneName As String‘same type as the array

For Each strOneName In strName

Messagebox.Show(strOneName) ' Write one array element

Next strOneName

' Assumes array intTotal previously dimensioned

Dim intOneTotal As Integer

ForEach intOneTotal In intTotal

intOneTotal=0 ' reinitialize the array

Next intOneTotal

Goal:

• Get the grades of a class

• (the class size may vary)

How?

• Have an array

• Ask for the size of the class: intClass

• Dimension the Array to be big enough

• compute the average (loop)

strSize contains 110

110

strSize contains ""

Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the content of the text box.

strSize = InputBox("Enter class Size","Input")

Dim strSize AsString

Dim intCounter AsInteger

Dim intClassSize AsInteger

'ask user size of the class (number of students)

Do

strSize = InputBox("Please Enter Size", "Input")

IfNot IsNumeric(strSize) Then

MessageBox.Show("enter a numeric value")

EndIf

LoopUntil IsNumeric(strSize)

intClassSize = CInt(strSize)

'now create the Array.All elements in the array are initialized to zero

'Use a loop to get all of the grades

For intCounter = 0 To dblGrades.Length - 1

'ask the user for the grades and place them in the array

Try

Catch

'Decrement to use the same index value at the next iteration

intCounter = intCounter - 1

EndTry

Next

Code for the example (4)

'now we have the array all populated with datawe can calculate the average

‘we are going to use a For Each Next Loop

Dim dblTotalSum AsDouble

Dim dblAverage AsDouble

MessageBox.Show("Average: " & _ FormatNumber(dblAverage, 2))

'If you wanted to use a normal For Next Loop

For intCounter = 0 To _ dblGrades.Length - 1

dblTotalSum = _ dblTotalSum + _ dblGrades(intCounter)

Next

some constant

previously defined

But can do this on array elements