Arrays Part 2
Objectives Search a one-dimensional array Compute the average of a one-dimensional array’s contents Find the highest entry in a one-dimensional array Update the contents of a one-dimensional array Sort 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
The For Each…Next 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 Syntax: For each element [as datatype} in group Statement(s) Next
The For Each…Next Statement (cont’d) Hands-on Example
Searching a One-Dimensional Array Hands-on Example
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
Determining the Highest Value Stored in a One-Dimensional Array Hands-on Example
Updating the Values Stored in a One-Dimensional Array Hands-on Example
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 (con’d) Hands-on Example