Download presentation
Presentation is loading. Please wait.
Published byGyles Hodge Modified over 9 years ago
1
ISAT 252 Introduction to Arrays
2
Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513
3
Learning Objectives Based on this lecture and your own study, you must be able to…. Define the following with respect to arrays Element Subscript or Index Lower 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
4
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
5
Problem 5 If we are averaging 3 grades, we will have a variable for each grade: Dim dblGrade1 As Double Dim dblGrade2 As Double Dim dblGrade3 As Double What if we need to average 10 grades? Or 100 grades? PROBLEM!!! – How do we handle it?
6
Examples 6 We can have arrays Array of Students’ Grades Array of Months Arrays of States 85 92 100 92.5 Students’ Grades 012345012345
7
Definitions 7 Array An indexed series of individual elements of the same data type, all referenced by the same name plus its index Element An individual element or entry in an array Subscript or index An integer number that identifies an element’s position in the array
8
Scope of Variables Local Level – what we have done so far Declared within Sub/Function Available only within Sub/Function Class/Form Level – what we will use for Lab 10 Declared below Public Class frmName Available to all Subs/Functions in the class/form Project Level – only needed if a multiform project Declared in separate.vb file Available to all Subs/Functions in the project
9
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 scope In the declaration specify name, type, and the subscript range for the array Example: Dim sngCost(10) As Single allocates 11 memory locations sngCost(0)sngCost(10)
10
Subscripts 10 The subscript range can be specified using integer constants, integer variables, integer expressions Const intArraySize As Integer = 10 Dim sngCost(10) As Single Dim dblProfit( intArraySize ) As Double
11
Subscripts 11 Each element in an array is identified by its subscript Dim sngGrade(5) As Single sngGrade 85 92 100 92.5 sngGrade(0) sngGrade(1) sngGrade(2) sngGrade(3) sngGrade(4) sngGrade(5) Name of the Array Subscript: Identifies the element in the array
12
Subscripts 12 Subscripts can be referenced as constants, variables, or numeric expressions intVar = 4 intIndex1 = 3 intIndex2 = 1 sngTotal(intVar) = 26.3 ‘Sets sngTotal(4) equal to 26.3 sngTotal(4) = 26.3 ‘ Does the same thing sngTotal(intIndex1+intIndex2) =26.3 ‘Also does same thing sngVar=sngTotal(intIndex1+intIndex2) + sngTotal(intVar) + _ sngTotal(4)
13
Example 13 Dim sngGrade(5) as Single sngGrade(1) = 85 sngGrade(5) = 92 For intIndex = 2 To 4 sngGrade(intIndex) = 100 Next intIndex sngGrade(0) = (sngGrade(1) + sngGrade(2) )/2 0 intIndex 5 4 3 2 1 0 sGrade 85 92 100 92.5 23 4
14
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”
15
Example: computing average grade 15 Dim sngGrade(29) as Single‘space for 30 students Dim sngAverage as Single Dim 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 intI sngAverage = sngTotal/30
16
Example: computing wages 16 Dim strName(9) as String Dim sngHours(9) as Single Dim sngPay(9) as Single Dim 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 person sngWage = 8.75 For intPerson = 0 To 9 sngPay(intPerson) = sngWage * sngHours(intPerson) txtOutput.text += strName(intPerson) + _ formatCurrency(sngPay(intPerson)) + _ vbNewLine Next intPerson
17
Example: Tracking Your GPA 17 Two arrays intCreditHrs: an integer array storing the credit hours for each course strGrade: a string array storing the letter grade you received in each course (no +’s or –’s to keep it simple) Task: Calculate your GPA
18
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
19
Calculate GPA Case statement 19 Dim strGrade(19) as String‘Array of course letter grades Dim dblGrade (19) as Double ‘Array of point scales Dim 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) = 4 Case “B” dblGrade(intCounter) = 3 Case “C” dblGrade(intCounter) = 2 Case “D” dblGrade(intCounter) = 1 Case Else dblGrade(intCounter) = 0 End Select Next intCounter
20
The Rest of the Sub 20 Dim intCrHrs(19) as Integer‘Array of course credit hrs Dim strGrade(19) as String‘Array of course letter grades Dim sngGPA as Single‘Computed GPA Dim intTotalCrHrs as Integer‘Total Credit Hrs taken Dim 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 = 0 intTotalCrHrs = 0 For intCounter = 0 to intNumGrades - 1 sngGPA += (dblGrade(intCounter)) * intCrHrs(intCounter) intTotalCrHrs+=intCrHrs(intCounter) Next intCounter sngGPA /= intTotalCrHrs‘Final GPA calculation
21
Multi-Dimensional Arrays
22
One-Dimensional vs Two-Dimensional Arrays 22 1-Dimensional array – has only one subscript 2-Dimensional arrays – have two subscripts Analogous to a table 1 st subscript represents the “row” 2 nd subscript represents the “column” All elements in a given array must be of the same data type, regardless of dimension
23
Two-Dimensional Arrays 23 Correspond to a matrix all elements must be of the same data type use two subscripts to access an element 1st: row number 2nd: column number 1 st dimension (row) 2 nd dimension (col)
24
Declaring 2-D arrays 24 Dim intArray (1, 4) as Integer Dim sngGrades (29, 9) as Single Either, an array of numeric grades on 10 items for each of 30 students OR An array of grades on 30 items for each of 10 students (it’s up to you to decide!)
25
Accessing Array Elements 25 Dim intArray (1, 4) As Integer 1 intArray 0 01234 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 Subscript Column Subscript
26
Accessing Array Elements 26 intArray (0, 1) = 10 intArray (1, 4) = intArray(1, 3) + intArray(1, 4) intI = 2 intJ = 1 sngGrades (intI, intJ) = 93 intX = 1 intY = 2 intArray (1, intX + intY) = 15
27
2D Array Declaration 27 Dim sngGrades (29, 9) as Single array of grades for 30 students in 10 tests 1 st Dimension: Students 2 nd 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
28
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 intCol Next intRow For intRow = 0 To 1 intRowSum = 0 For intCol = 0 To 4 intRowSum = intRowSum + intArray(intRow,intCol) Next intCol txtOutput.text += intRowSum.toString()+ vbNewLine Next intRow
29
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() +_ vbNewLine Next intRow sngTotal = 0 For intRow = 0 to 29 sngTotal = sngTotal + sngGrade(intRow, 9) Next intRow sngAverage = sngTotal/30 sngColTotal = 0 For intCol = 0 to 9 sngColTotal = sngColTotal + sngGrade(2, intCol) Next intCol
30
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 1 st Col = miles driven 2 nd Col = gasoline consumed (in gallons) ….. 30 strName sngDrivenData 0 XX 1 2 0 1 2 01............ 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
31
Assignment 31 Do Lab 10 Read Chapter 6 on Sub and Function procedures for next week
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.