Title: Chapter 7: Working with Arrays
1Chapter 7 Working with Arrays
- Visual Basic .NET Programming
- From Problem Analysis to Program Design
2Objectives
- Define one-dimensional arrays
- Create String arrays
- Declare multi-dimensional arrays
- Search an array
3Arrays
- Array is a collection of elements with each
element behaves as a variable does -
4Arrays
- To declare array syntax
- Dim arrayName (index of last element) As DataType
- Example
5Arrays (continued)
- Arrays could be multi-dimensional
-
- An array is managed as an object (array class)
-
- Arrays are static
-
6Using a Loop to Iterate an Array
- Use loop to process array elements
- Repeat program code for each element
Dim examScores(4) As Integer, sum, average As
Double Dim index As Integer ' loop to enter and
sum the exam scores For index 0 To
examScores.Length 1 Console.WriteLine("Enter
an Exam Score ") examScores(index)
Convert.ToInt32(Console.ReadLine()) sum
examScores(index) Next average sum /
examScores.Length Console.WriteLine("The average
is " Math.Round(average, 1))
7Array Methods
- Sort Array.Sort( examScores )
- Reverse Array.Reverse( examScores )
Dim examscores() As Integer 85, 90, 94, 89,
91 Dim index As Integer Console.writeLine(Unsort
ed array) For index 0 To examScores.Length
1 Console.Write(examScores(index) ,
) Next Array.Sort(examScores) Console.WriteLine()
Console.WriteLine(Sorted array) For index 0
To examScores.Length 1 Console.Write(examScores
(index) , ) Next
8The String Array
- Each element in a string array is a string
- To extracts substrings separated by a character
value (empty space). Letter c indicates the
data type - Dim stringArray() As String stringValue.Split(
c)
Dim stringValue As String Hello world Wide
web, i As Integer Dim stringArray() As String
stringValue.Split ( c) For i 0 To
stringArray.Length 1 Console.writeLine(
stringArray(i) ) Next Console.writeLine(
stringArray.Length elements
) Console.writeLine( length of Web is
stringArray(3).Length ) Console.writeLine(
stringArray(0).ToUpper() )
9(No Transcript)
10Multi-Dimensional Arrays
- Two-dimensional array is like a table with rows
and columns - Dim testScoreTable(4, 1) As Integer
-
- Accessing elements in two-dimensional array
example
11Example Compute the First Exam Average
- Dim testScoreTable(,) As Integer _
- 85, 88, 90, 85, 94, 60, 89, 95, 91,
100 - ' compute Exam 1 average using a loop
- Dim sum As Double, row As Integer
- For row 0 To 4
- sum testScoreTable(row, 0)
- Next
- Console.WriteLine( Exam 1 average is sum / 5
)
12(No Transcript)
13Searching an Array
- Search array to see if it contains specific value
- Use loop to compare each value to target
Dim stringValue As String Hello orld Wide
web Dim stringArray() As String
stringValue.Split( c) Dim searchValue As String
Web, found As Boolean False Do While i lt
stringArray.Length And Not found If
stringArray(i).Equals(searchValue) Then found
True Else i 1 End If Loop
14Example Employee Payroll
- Problem to be solved to input employees taxable
wages and marital status, and to compute and
display amount of federal tax (in currency
format) to be withheld - Use arrays to hold tax tables
- Input
- Taxable wages
- Marital status
- Output
- Amount of federal tax to be withheld
15Example Employee Payroll
- Imports System.Collections.ArrayList
- ...
- single employee table
- Dim singleTaxTable(,) As Double
- 51, 0, 0, 0, 187, 0, 0.1, 51, 592, 13.6,
0.15, 187, _ - 1317, 74.35, 0.25, 592, 2860, 255.6, 0.28,
1317, _ - 6177, 687.64, 0.33, 2860, 99999, 1782.25,
0.35, 6177 - ' married employee table
- Dim marriedTaxTable(,) As Double
- 154, 0, 0, 0, 429, 0, 0.1, 154, 1245,
27.5, 0.15, 429, _ - 2270, 149.9, 0.25,
1245, 3568, 406.15, 0.28, 2270, _ - 6271, 769.59, 0.33,
3568, 99999, 1661.58, 0.35, 6271 - Dim taxTable(,) As Double,
marriedOrSingle As Char - Dim msIndex, rowIndex As Integer, wages,
fedWithholding As Double - Dim found As Boolean
16Example Employee Payroll
- ' use 99999 as sentinal value to terminate
- Console.Write("Enter wage amount (99999
to stop)") - wages Convert.ToDouble(Console.ReadLine(
)) - ' begin the loop for each employee
- Do Until wages 99999
- Console.Write("Enter M for married, S
for single") - marriedOrSingle Convert.ToChar(Cons
ole.ReadLine()) - ' assign taxTable reference to
appropriate table - If marriedOrSingle "M" Then
- taxTable marriedTaxTable
- Else
- taxTable singleTaxTable
- End If
- rowIndex 0
- found False
17Example Employee Payroll
- ' loop to find the appropriate row for the
wages - Do While rowIndex lt 6 And found
False - If wages lt
taxTable(rowIndex, 0) Then - ' compute the withholding
amount - fedWithholding
taxTable(rowIndex, 1) _ - taxTable(rowIndex, 2) (wages -
taxTable(rowIndex, 3)) - found True ' stop the loop
- Else
- rowIndex 1
- End If
- Loop
- Console.WriteLine("Federal
Withholding " fedWithholding.ToString("C")) - ' input wage amount for next
employee - Console.Write("Enter wage amount
(99999 to stop)") - wages Convert.ToDouble(Console.Read
Line()) - Loop