Loading...

PPT – Microsoft Visual Basic 2005: Reloaded Second Edition PowerPoint presentation | free to download - id: 7a4e45-NjgwN

The Adobe Flash plugin is needed to view this content

Microsoft Visual Basic 2005 Reloaded Second

Edition

- Chapter 8
- Arrays

Using Arrays

- Simple variable (or scalar variable) a variable

that is unrelated to any other variable in memory - Array
- A group of variables with the same name and data

type that are related in some way - Used to temporarily store related data in memory
- Increases the efficiency of a program
- Commonly used arrays
- One-dimensional
- Two-dimensional

One-Dimensional Arrays

- One-dimensional array
- Can be viewed as a column of variables
- Subscript
- A unique number that identifies each variable in

a one-dimensional array - Starts at 0 for first element in the array
- Use array name and subscript to refer to each

individual variable in the array

One-Dimensional Arrays

Score(0)

Score(4)

One-Dimensional Arrays (continued)

One-Dimensional Arrays (continued)

One-Dimensional Arrays (continued)

- Element an individual variable in the array
- When an array is declared
- Each element in the array is initialized if no

values are provided - Initial values can be specified for elements

Storing Data in a One-Dimensional Array

Manipulating One-Dimensional Arrays

- Array elements can be used like any other

variable - Examples
- Display the contents of an array
- Access an array element using its subscript
- Search the array
- Calculate the average of data stored in a numeric

array - Find the highest value stored in an array
- Update array elements
- Sort array elements

Displaying the Contents of a One-Dimensional

Array

Displaying the Contents of a One-Dimensional

Array (continued)

Displaying the Contents of a One-Dimensional

Array (continued)

The For EachNext Statement

- For EachNext statement
- Used to code a loop which processes each element

in a group or array - Creates a variable used to represent each item in

the group or array - Data type of the element must match the data type

of the group

The For EachNext Statement (continued)

Using the Subscript to Access an Element in a

One-Dimensional Array

Using the Subscript to Access an Element

(continued)

Subscript to Access an Element

"C0" Currency, no DP.

Searching a One-Dimensional Array

Searching a One-Dimensional Array (continued)

Searching a One-Dimensional Array (continued)

How many elements?

Highest Subscript?

Searching a One-Dimensional Array

Could this be done with a For/Next loop?

Whats Wrong with a For/Next loop?

Calculating the Average Amount Stored in a

One-Dimensional Numeric Array

- Sample application
- Find the average from an array of test scores
- Length property number of elements in the array

Calculating the Average Amount Stored in a

One-Dimensional Numeric Array (continued)

Calculating the Average Amount Stored in a

One-Dimensional Numeric Array (continued)

Calculating the Average Amount Stored in a

One-Dimensional Numeric Array (continued)

Calculating the Average Amount Stored in a

One-Dimensional Numeric Array (continued)

Could this have been done another way?

Determining the Highest Value Stored in a

One-Dimensional Array

Determining the Highest Value Stored in a

One-Dimensional Array (continued)

Determining the Highest Value Stored in a

One-Dimensional Array (continued)

Updating the Values Stored in a One-Dimensional

Array

Updating the Values Stored in a One-Dimensional

Array (continued)

Updating the Values Stored in a One-Dimensional

Array (continued)

Updating the Values Stored in a One-Dimensional

Array (continued)

Sorting the Data Stored in a One-Dimensional

Array

- Sorting arranging data in a specific order
- Ascending first element is smallest, last

element is largest - Descending first element is largest, last

element is smallest - Array.Sort method used to sort elements in a

one-dimensional array in ascending order - Array.Reverse method used after Array.Sort

method to change to descending order

Sorting the Data Stored in a One-Dimensional

Array (continued)

Sorting the Data Stored in a One-Dimensional

Array (continued)

Parallel One-Dimensional Arrays

Parallel One-Dimensional Arrays (continued)

Parallel One-Dimensional Arrays (continued)

- Parallel arrays
- Two or more arrays whose elements are related by

their position in the arrays

Parallel One-Dimensional Arrays (continued)

Parallel One-Dimensional Arrays (continued)

Parallel One-Dimensional Arrays (continued)

Two-Dimensional Arrays

- Two-dimensional array
- Resembles a table with rows and columns
- Each element is identified by a unique

combination of two subscripts (row, column) - Subscripts are zero-relative

Two-Dimensional Arrays (continued)

Two-Dimensional Arrays (continued)

Two-Dimensional Arrays (continued)

Two-Dimensional Arrays (continued)

- Two-dimensional array
- Declared with highest row subscript and highest

column subscript (zero-relative) - Number of rows highest row subscript 1
- Number of columns highest column subscript 1
- Can specify initial values for array elements
- If no initial values are declared, array elements

are automatically initialized

Storing Data in a Two-Dimensional Array

Storing Data in a Two-Dimensional Array

(continued)

Searching a Two-Dimensional Array

Searching a Two-Dimensional Array (continued)

Searching a Two-Dimensional Array (continued)

Searching a Two-Dimensional Array (continued)

Programming Tutorial

Programming Example

Summary

- Arrays a group of related variables with the

same name and same data type - Array element one item in the array
- One-dimensional array each element is uniquely

identified by its position (subscript) in the

array - Two-dimensional array each element is uniquely

identified by its position (row and column

subscripts) in the array - Subscripts are zero-relative

Summary (continued)

- Number of elements in an array is the highest

subscript value 1 - For Each...Next statement used to loop through

all the elements in an array - Length property returns the number of elements

in an array - Array.Sort method sorts the elements in a

one-dimensional array in ascending order - Array.Reverse method reverses the order of

elements in a one-dimensional array