ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

31
ISAT 252 Introduction to Arrays

Transcript of ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Page 1: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

ISAT 252

Introduction to Arrays

Page 2: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Should have read

2

Chapter 8 –pp. 473-498, and pp. 506-513

Page 3: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Learning ObjectivesBased on this lecture and your own study, you must be able to….

Define the following with respect to arraysElementSubscript or IndexLower bound and Upper bound

Properly declare arrays for use in VB

Distinguish between 1D and 2D arrays

Use single and nested loops to access and process data stored in 1D and 2D arrays

Write VB code to process data stored in arrays

Utilize variable scope

3

Page 4: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Overview/Review of topics

4

Concepts from previous Lectures

Software development process

Decisions and Loops

Coming up later in semester

Procedures and functions - ByRef and ByVal

Other Modularization - Multiple forms and Classes

Page 5: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Problem

5

If we are averaging 3 grades, we will have a variable for each grade:Dim dblGrade1 As DoubleDim dblGrade2 As DoubleDim dblGrade3 As Double

What if we need to average 10 grades? Or 100 grades?

PROBLEM!!! – How do we handle it?

Page 6: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Examples

6

We can have arraysArray of Students’ GradesArray of MonthsArrays of States

85

92

100

100

100

92.5

Students’ Grades0

1

2

3

4

5

Page 7: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Definitions

7

ArrayAn indexed series of individual elements of the same data type, all referenced by the same name plus its index

ElementAn individual element or entry in an array

Subscript or index An integer number that identifies an element’s position in the array

Page 8: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Scope of Variables

Local Level – what we have done so farDeclared within Sub/FunctionAvailable only within Sub/Function

Class/Form Level – what we will use for Lab 10 Declared below Public Class frmNameAvailable to all Subs/Functions in the class/form

Project Level – only needed if a multiform projectDeclared in separate .vb fileAvailable to all Subs/Functions in the project

Page 9: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Declaring Arrays

9

Arrays can be declared at form-level scope (in class) or at local level (in sub)

Declared the same way as single variables (scalar variables)DIM statement for local or form-level scopeIn the declaration specify name, type, and the

subscript range for the array

Example: Dim sngCost(10) As Singleallocates 11 memory locations

sngCost(0) sngCost(10)

Page 10: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

SubscriptsSubscripts

10

The subscript range can be specified using integer constants, integer variables, integer expressions

Const intArraySize As Integer = 10Dim sngCost(10) As SingleDim dblProfit(intArraySize) As Double

Page 11: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Subscripts

11

Each element in an array is identified by its subscript

Dim sngGrade(5) As SinglesngGrade

85

92

100

100

100

92.5sngGrade(0)sngGrade(1)sngGrade(2)

sngGrade(3)sngGrade(4)sngGrade(5)

Name of the Array

Subscript: Identifies the

element in the array

Page 12: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Subscripts

12

Subscripts can be referenced as constants, variables, or numeric expressions

intVar = 4

intIndex1 = 3intIndex2 = 1sngTotal(intVar) = 26.3 ‘Sets sngTotal(4) equal to 26.3sngTotal(4) = 26.3 ‘ Does the same thingsngTotal(intIndex1+intIndex2) =26.3 ‘Also does same thing

sngVar=sngTotal(intIndex1+intIndex2) + sngTotal(intVar) + _ sngTotal(4)

Page 13: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Example

13

Dim sngGrade(5) as SinglesngGrade(1) = 85sngGrade(5) = 92For intIndex = 2 To 4 sngGrade(intIndex) = 100Next intIndexsngGrade(0) = (sngGrade(1) +

sngGrade(2) )/2

0intIndex

5

4

3

2

1

0sGrade

85

92

100

100

100

92.5

234

Page 14: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Why use arrays?

14

To store and process LISTS or TABLES of data

To provide easy access via loops

To allow processing of groups of data, where the groups must be “remembered”

Page 15: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Example: computing average grade

15

Dim sngGrade(29) as Single ‘space for 30 studentsDim sngAverage as SingleDim sngTotal as Single

Dim intI as Integer

‘read in student’s grades (sngGrade) from a file

sngAverage=0.0

For intI =0 to 29 sngTotal += sngGrade(intI)

Next intIsngAverage = sngTotal/30

Page 16: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Example: computing wages

16

Dim strName(9) as StringDim sngHours(9) as SingleDim sngPay(9) as SingleDim intPerson as Integer

‘read in employee names (strName) and hours worked (sngHours) code to read in names and hours goes here.

‘Calculate wages for each personsngWage = 8.75For intPerson = 0 To 9

sngPay(intPerson) = sngWage * sngHours(intPerson)txtOutput.text += strName(intPerson) + _

formatCurrency(sngPay(intPerson)) + _

vbNewLineNext intPerson

Page 17: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Example: Tracking Your GPA

17

Two arraysintCreditHrs: an integer array storing the

credit hours for each coursestrGrade: a string array storing the letter

grade you received in each course (no +’s or –’s to keep it simple)

Task: Calculate your GPA

Page 18: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Algorithm: Pseudocode

18

For each course…

Convert the letter grade to a numeric grade (1-4 scale)

Multiply the numeric grade * the number of credit hrs

Add the product into a running total of grade points

Add the course credit hrs into a running credit hr total

Calculate final GPA as the total grade points divided by the credit hr total

Page 19: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Calculate GPA Case statement

19

Dim strGrade(19) as String ‘Array of course letter gradesDim dblGrade (19) as Double ‘Array of point scalesDim intCounter as Integer ‘ Index for looping thru arrays‘First enter all grades into strGrade array

‘intNumGrades counts as grades are entered

‘Section to convert the letter grade to the 4-point scale

For intCounter = 0 To intNumGrades - 1

Select Case strGrade(intCounter)Case “A”

dblGrade(intCounter) = 4Case “B”

dblGrade(intCounter) = 3Case “C”

dblGrade(intCounter) = 2Case “D”

dblGrade(intCounter) = 1Case Else

dblGrade(intCounter) = 0End Select

Next intCounter

Page 20: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

The Rest of the Sub

20

Dim intCrHrs(19) as Integer ‘Array of course credit hrsDim strGrade(19) as String ‘Array of course letter gradesDim sngGPA as Single ‘Computed GPADim intTotalCrHrs as Integer ‘Total Credit Hrs takenDim intCounter as Integer ‘ Index for looping thru arrays

‘Code for inputing the entries in strCName, intCrHrs, and strGrade arrays goes here

‘Add the credit hrs and calculate the points toward ‘the GPA for each course

sngGPA = 0intTotalCrHrs = 0For intCounter = 0 to intNumGrades - 1

sngGPA += (dblGrade(intCounter)) * intCrHrs(intCounter)intTotalCrHrs+=intCrHrs(intCounter)

Next intCountersngGPA /= intTotalCrHrs ‘Final GPA calculation

Page 21: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Multi-Dimensional Arrays

Page 22: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

One-Dimensional vs Two-Dimensional Arrays

22

1-Dimensional array – has only one subscript

2-Dimensional arrays – have two subscriptsAnalogous to a table

1st subscript represents the “row”2nd subscript represents the “column”

All elements in a given array must be of the same data type, regardless of dimension

Page 23: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Two-Dimensional Arrays

23

Correspond to a matrix

all elements must be of the same data typeuse two subscripts to access an element

1st: row number2nd: column number

1st dimension (row)

2nd dimension (col)

Page 24: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Declaring 2-D arrays

24

Dim intArray (1, 4) as Integer

Dim sngGrades (29, 9) as SingleEither, an array of numeric grades on 10

items for each of 30 studentsOR

An array of grades on 30 items for each of 10 students(it’s up to you to decide!)

Page 25: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Accessing Array Elements

25

Dim intArray (1, 4) As Integer

1

intArray

0

0 1 2 3 4

intArray(0,0)intArray(0,1) intArray(0,2) intArray(0,3)intArray(0,4)

intArray(1,0)intArray(1,1) intArray(1,2)

intArray(1,3)

intArray(1,4)

Name of the Array

Row SubscriptColumn

Subscript

Page 26: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Accessing Array Elements

26

intArray (0, 1) = 10

intArray (1, 4) = intArray(1, 3) + intArray(1, 4)

intI = 2intJ = 1 sngGrades (intI, intJ) = 93

intX = 1intY = 2intArray (1, intX + intY) = 15

Page 27: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

2D Array Declaration

27

Dim sngGrades (29, 9) as Singlearray of grades for 30 students in 10 tests1st Dimension: Students2nd Dimension: Test scores

0 1 2 3 4 5 6 7 8 9

0

1

2

.

.

27

28

29

……………………• sngGrades(i,j) : score of student i in test j

sngGrades(1,6) Row i : all scores for student i

0 1 2 3 4 5 6 7 8 9

0

1

2

.

.

27

28

29

……………………

Row 1 : scores for student #1

• Column j : Scores of all students in test j

0 1 2 3 4 5 6 7 8 9

0

1

2

.

.

27

28

29

……………………

Column 6: Scores of all students in test 6

Page 28: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Using Nested Loops to do work in 2-D arrays

28

For intRow = 0 To 1 For intCol = 0 To 4

intArray(intRow,intCol) = intRow + intCol Next intColNext intRow

For intRow = 0 To 1 intRowSum = 0 For intCol = 0 To 4

intRowSum = intRowSum + intArray(intRow,intCol) Next intCol txtOutput.text += intRowSum.toString()+ vbNewLineNext intRow

Page 29: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Using Single Loops to access a single row or column an a 2-D array

29

For intRow = 0 to 29 txtOutput.text += sngGrade(intRow, 9).ToString() +_

vbNewLineNext intRow

sngTotal = 0For intRow = 0 to 29 sngTotal = sngTotal + sngGrade(intRow, 9)Next intRowsngAverage = sngTotal/30

sngColTotal = 0For intCol = 0 to 9

sngColTotal = sngColTotal + sngGrade(2, intCol)Next intCol

Page 30: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Example: Calculate and print gas mileage for 50 different cars

Model names stored in a 1D string array file

Miles and gasoline consumed stored in 2D single precision array

1st Col = miles driven2nd Col = gasoline

consumed (in gallons)…..

30

strName sngDrivenData

0

X X

1

2

0

1

2

0 1

.

.

.

.

.

.

Data for Car #2

Miles Driven by Car #2 : sngDriven(2,0)

Car #2’s Name : strName(2)

Gas consumed by Car #2 : sngDriven(2,1)

X

Page 31: ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Assignment

31

Do Lab 10

Read Chapter 6 on Sub and Function procedures for next week