1 / 20

# Chapter 7 – Arrays - PowerPoint PPT Presentation

Chapter 7 – Arrays. 7.1 Creating and Accessing Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays 7.5 A Case Study: Analyze a Loan. 7.4 Two-Dimensional Arrays. Declaring a Two-Dimensional Array Variable Implicit Array Sizing and Initialization

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

## PowerPoint Slideshow about ' Chapter 7 – Arrays' - lundy

An Image/Link below is provided (as is) to download presentation

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

7.1 Creating and Accessing Arrays

7.2 Using LINQ with Arrays

7.3 Arrays of Structures

7.4 Two-Dimensional Arrays

7.5 A Case Study: Analyze a Loan

• Declaring a Two-Dimensional Array Variable

• Implicit Array Sizing and Initialization

• The ReDim Statement

• Filling a Two-Dimensional Array with a Text File

• Outputting an Array

• Two-dimensional arrays store a table of items of the same type.

• Consider the rows of the table as numbered 0, 1, 2, … m and the columns numbered 0, 1, 2, … n.

• The array is declared with

DimarrayName(m, n) AsDataType

• The item in ith row, jth column is denoted

arrayName(i,j)

An unsized two-dimensional array can be declared with a statement of the form

DimarrayName(,) As varType

• An already-created array can be resized with

ReDimarrayName(r, c)

• which loses the current contents.

• When Preserve is used …

ReDim PreservearrayName(r, c)

• Only the columns can be resized.

• ReDim cannot change the number of rows in the array.

Implicit Array Sizing and Initialization

Arrays can be initialized when they are declared.

DimarrayName(,) As DataType=

{{ROW0},{ROW1}, {ROW2}, ..., {ROWN}}

declares a two-dimensional array where ROW0 consists of the entries in the top row of the corresponding table delimited by commas, and so on.

Road-Mileage Table

Dim rm(3, 3) As Double

rm(0,0)=0, rm(0,1)=2054, rm(1,2)=2786

Implicit Array AssignmentRoad-Mileage

Dimrm(,) As Double = {{0, 2054, 802, 738},

{2054, 0, 2786, 2706},

{802, 2786, 0, 100},

{738, 2706, 100, 0}}

declares and initializes an array of road-mileages.

After execution of the statement

DimarrayName(r, c) As varType

• the value of arrayName.GetUpperBound(0) is r,

• and the value of arrayName.GetUpperBound(1) is c.

Text File Distances.txt

0,2054,802,738

2054,0,2786,2706

802,2786,0,100

738,2706,100,0

In the next set of code we will read the values of this text file into a two dimensional array.

Dimrm(3, 3) As Double 'road mileage

DimrowOfNums() AsString=

IO.File.ReadAllLines("Distances.txt")

Dimline, data() As String

ForiAs Integer = 0To 3

line = rowOfNums(i)

data = line.Split(""c)

For jAs Integer= 0 To3

rm(i, j) = CDbl(data(j))

Next

Next

Private Sub PrintMatrix(rm() as Double)

‘Print a 3 x 3 matrix

Dim build as String

Forrow As Integer = 0 Torm.getupperbound(0)

build = “”

Forcol As Integer = 0 Torm.getupperbound(1) build = build &Cstr(rm(row, col)) & “ “

Next

lstbox.items.add(build)

Next

End Sub

'Print a 3 x 3 matrix

Dim rm(,) AsDouble = {{2, 3, 5},

{1, 4, 6},

{0, 7, 9}}

Dim frtstr AsString = "{0,-15} {1,-15} {2,-15}"

For row AsInteger = 0 To 2

ListBox1.Items.Add(String.Format(frtstr, rm(row, 0), rm(row, 1), rm(row, 2)))

Next

First create the columns for your matrix

Dim example(,) AsInteger = {{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}}

For r AsInteger = 0 To 2

DataGridView1.Rows.Add()

For c AsInteger = 0 To 3

DataGridView1.Item(c, r).Value = example(r, c)

Next

Next

Matrices rows first, columns second

DataGridView columns are listed first …Blah

DataGridView Column Headers

Dim example(,) AsInteger = {{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}}

For c AsInteger = 0 To 3

DataGridView1.Columns.Add(CStr(c), "Column " & c)

Next

For r AsInteger = 0 To 2

DataGridView1.Rows.Add()

For c AsInteger = 0 To 3

DataGridView1.Item(c, r).Value = example(r, c)

Next

Next

Column name

DataGridView Row Headers

Dim columns() AsString = {"Column1", "Column2", "Column3"}

Dim Rows() AsString = {"Row1", "Row2", "Row3"}

Dim array2d(,) AsInteger = {{1, 2, 3},

{4, 5, 6},

{7, 8, 9}}

For c AsInteger = 0 To columns.Count - 1

DataGridView1.Columns.Add(columns(c), columns(c))

Next

For r AsInteger = 0 To Rows.Count - 1

DataGridView1.Rows.Add()

DataGridView1.Rows(r).HeaderCell.Value = Rows(r)

For c AsInteger = 0 To columns.Count - 1

DataGridView1.Item(c, r).Value = array2d(r, c)

Next

Next

1st Add the row

2nd Assign the row array to the dgv

Dim nums(,) AsDouble = {{7, 3, 1, 0},

{2, 5, 9, 8},

{0, 6, 4, 10}}

'use a For Each loop

Dim total AsDouble = 0

ForEach num AsDoubleIn nums

If num Mod 2 = 0 Then

total += num

EndIf

Next

lstOutput.Items.Add(total)

Finding the sum of the even numbers in a

Matrix.

Dimnums(,) AsDouble = {{7, 3, 1, 0},

{2, 5, 9, 8},

{0, 6, 4, 10}}

'use a For Each loop

'use LINQ

Dim query = FromnumInnums.Cast(OfDouble)()

Where (numMod 2 = 0)

Selectnum

lstOutput.Items.Add(query.Sum)

Can you create a For Loop to add up the columns?

2 3 6 8

7 1 4 5

0 2 -2 9

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

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

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