310 likes | 403 Views
Introduction to Arrays. ISAT 252. Should have read. Chapter 8 –pp. 473-498, and pp. 506-513. 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
E N D
Introduction to Arrays ISAT 252
Should have read • 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 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
Overview/Review of topics • 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
Problem • 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?
92.5 85 100 100 100 92 Examples • We can have arrays • Array of Students’ Grades • Array of Months • Arrays of States Students’ Grades 0 1 2 3 4 5
Definitions • 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 • SubscriptorindexAn integer number that identifies an element’s position in the array
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
sngCost(0) sngCost(10) Declaring Arrays • 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 Singleallocates 11 memory locations
Subscripts • The subscript range can be specified using integer constants, integer variables, integer expressions Const intArraySize As Integer = 10 DimsngCost(10)As Single DimdblProfit(intArraySize)As Double
sngGrade sngGrade(0) 92.5 sngGrade(1) 85 sngGrade(2) 100 sngGrade(3) 100 sngGrade(4) 100 sngGrade(5) 92 Subscripts • Each element in an array is identified by its subscript Name of the Array Dim sngGrade(5) As Single Subscript: Identifies the element in the array
Subscripts • 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)
sGrade 0 1 2 3 4 5 Example 0 2 3 intIndex 4 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 92.5 85 100 100 100 92
Why use arrays? • 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”
Example: computing average grade DimsngGrade(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 ForintI =0 to 29 sngTotal += sngGrade(intI) NextintI sngAverage = sngTotal/30
Example: computing wages 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
Example: Tracking Your GPA • 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
Algorithm: Pseudocode 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
Calculate GPA Case statement DimstrGrade(19) as String ‘Array of course letter grades DimdblGrade (19) as Double ‘Array of point scales DimintCounteras 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 CasestrGrade(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
The Rest of the Sub DimintCrHrs(19) as Integer‘Array of course credit hrs DimstrGrade(19) as String ‘Array of course letter grades DimsngGPAas Single‘Computed GPA DimintTotalCrHrsas Integer‘Total Credit Hrs taken DimintCounteras 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 ForintCounter = 0 tointNumGrades - 1 sngGPA += (dblGrade(intCounter)) * intCrHrs(intCounter) intTotalCrHrs+=intCrHrs(intCounter) Next intCounter sngGPA /= intTotalCrHrs‘Final GPA calculation
One-Dimensional vs Two-Dimensional Arrays • 1-Dimensional array – has only one subscript • 2-Dimensional arrays – have two subscripts • Analogous 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
2nd dimension (col) 1st dimension (row) Two-Dimensional Arrays • 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
Declaring 2-D arrays • 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 studentsOR • An array of grades on 30 items for each of 10 students(it’s up to you to decide!)
intArray 0 1 2 3 4 0 1 Accessing Array Elements • DimintArray (1, 4)As Integer intArray(0,4) intArray(0,3) intArray(0,1) intArray(0,2) intArray(0,0) intArray(1,1) intArray(1,0) intArray(1,4) intArray(1,2) intArray(1,3) Name of the Array Row Subscript Column Subscript
Accessing Array Elements • 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
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 . . 27 28 29 0 1 2 . . 27 28 29 0 1 2 . . 27 28 29 …………………… …………………… …………………… sngGrades(1,6) • Row i : all scores for student i Row 1 : scores for student #1 • Column j : Scores of all students in test j Column 6: Scores of all students in test 6 2D Array Declaration • Dim sngGrades (29, 9) as Single • array of grades for 30 students in 10 tests • 1st Dimension: Students • 2nd Dimension: Test scores • sngGrades(i,j) : score of student i in test j
Using Nested Loops to do work in 2-D arrays 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
Using Single Loops to access a single row or column an a 2-D array 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
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 driven • 2nd Col = gasoline consumed (in gallons) • ….. sngDrivenData strName 0 1 0 0 1 1 Data for Car #2 Car #2’s Name : strName(2) 2 X X 2 X . . . . . . Miles Driven by Car #2 : sngDriven(2,0) Gas consumed by Car #2 : sngDriven(2,1)
Assignment • Do Lab 10 • Read Chapter 6 on Sub and Function procedures for next week