Visual C 2005 Using Arrays Objectives Declare an

Visual C# 2005 Using Arrays

Objectives • Declare an array and assign values to array elements • Initialize an array • Use subscripts to access array elements • Use the Length property • Use foreach to control array access Visual C# 2005 2

Objectives (continued) • • Search an array to find an exact match Search an array to find a range match Use the Binary. Search() method Use the Sort() and Reverse() methods • Use multidimensional arrays Visual C# 2005 3

Declaring an Array and Assigning Values to Array Elements • Array – List of data items that all have the same data type and the same name – Each item is distinguished from the others by an index • Declaring and creating an array double[] sales; sales = new double[20]; • new operator – Used to create objects • You can change the size of an array Visual C# 2005 4

Declaring an Array and Assigning Values to Array Elements (continued) • Array element – Each object in an array • Subscript (or index) – Integer contained within square brackets that indicates the position of one of an array’s elements – Array’s elements are numbered beginning with 0 • “Off by one” error – Occurs when you forget that the first element in an array is element 0 Visual C# 2005 5

Declaring an Array and Assigning Values to Array Elements (continued) Visual C# 2005 6

Declaring an Array and Assigning Values to Array Elements (continued) • Assigning a value to an array element sales[0] = 2100. 00; • Printing an element value Console. Write. Line(sales[19]); Visual C# 2005 7

Initializing an Array • In C#, arrays are objects – Arrays are instances of a class named System. Array • Initializing objects – Numeric fields: 0 – Character fields: ‘ ’ or null – bool fields: false • Initializer list – List of values provided for an array Visual C# 2005 8
![Initializing an Array (continued) • Initializer list examples int[] my. Scores = new int[5] Initializing an Array (continued) • Initializer list examples int[] my. Scores = new int[5]](http://slidetodoc.com/presentation_image/c30669f64e0a467f4cae86d0d30164e1/image-9.jpg)
Initializing an Array (continued) • Initializer list examples int[] my. Scores = new int[5] {100, 76, 88, 100, 90}; int[] my. Scores = new int[] {100, 76, 88, 100, 90}; int[] my. Scores = {100, 76, 88, 100, 90}; Visual C# 2005 9

Using Subscripts to Access Array Elements • The power of arrays becomes apparent when you use subscripts – Can be variables rather than constant values • Using a loop to perform arithmetic on each element for (int sub = 0; sub < 5; ++sub) my. Scores[sub] += 3; Visual C# 2005 10

Using the Length Property • Length property – Member of the System. Array class – Automatically holds an array’s length • Examples int[] my. Scores = {100, 76, 88, 100, 90}; Console. Write. Line(“Array size is {0}”, my. Scores. Length); for (int x = 0; x < my. Scores. Length; ++x) Console. Write. Line(my. Scores[x]); Visual C# 2005 11

Using foreach to Control Array Access • foreach statement – Cycles through every array element without using a subscript – Uses a temporary iteration variable • Automatically holds each array value in turn • Example double[] pay. Rate = {6. 00, 7. 35, 8. 12, 12. 45, 22. 22}; foreach(double money in pay. Rate) Console. Write. Line(“{0}”, money. To. String(“C”)); Visual C# 2005 12

Using foreach to Control Array Access (continued) • When to use – When you want to access every array element – Since the iteration variable is read-only • You cannot assign a value to it Visual C# 2005 13

Searching an Array for an Exact Match • Searching options – Using a for loop – Using a while loop Visual C# 2005 14

Using a for Loop to Search an Array • Use a for statement to loop through the array – Set a Boolean variable to true when a match is found • Solution is valid even with parallel arrays Visual C# 2005 15

Using a for Loop to Search an Array (continued) Visual C# 2005 16

Using a for Loop to Search an Array (continued) Visual C# 2005 17

Using a while Loop to Search an Array Visual C# 2005 18

Searching an Array for a Range Match • Range match – Determines the pair of limiting values between which a value falls Visual C# 2005 19

Searching an Array for a Range Match (continued) Visual C# 2005 20

Using the Binary. Search() Method • Binary. Search() method – Finds a requested value in a sorted array – Member of the System. Array class • Do not use Binary. Search() under these circumstances – If your array items are not arranged in ascending order – If your array holds duplicate values and you want to find all of them – If you want to find a range match rather than an exact match Visual C# 2005 21

Using the Binary. Search() Method (continued) Visual C# 2005 22

Using the Sort() and Reverse() Methods • Sort() method – Arranges array items in ascending order – Use it by passing the array name to Array. Sort() • Reverse() method – Reverses the order of items in an array – Element that starts in position 0 is relocated to position Length – 1 – Use it by passing the array name to the method Visual C# 2005 23

Using the Sort() and Reverse() Methods (continued) Visual C# 2005 24

Using the Sort() and Reverse() Methods (continued) Visual C# 2005 25

Using Multidimensional Arrays • One-dimensional or single-dimensional array – Picture as a column of values – Elements can be accessed using a single subscript • Multidimensional arrays – Require multiple subscripts to access the array elements – Two-dimensional arrays • Have two or more columns of values for each row • Also called rectangular array, matrix, or a table Visual C# 2005 26

Using Multidimensional Arrays (continued) Visual C# 2005 27

Using Multidimensional Arrays (continued) Visual C# 2005 28

Using Multidimensional Arrays (continued) Visual C# 2005 29

Using Multidimensional Arrays (continued) • Three-dimensional array Visual C# 2005 30

Using Multidimensional Arrays (continued) • Jagged array – One-dimensional array in which each element is another one-dimensional array – Each row can be a different length Visual C# 2005 31

Using Multidimensional Arrays (continued) Visual C# 2005 32

You Do It • Activities to explore – – – Creating and using an array Initializing an array Using a for loop with an array Using the Length property with an array Using the Sort() and Reverse() methods Visual C# 2005 33

Summary • An array is a list of data items – All of which have the same type and the same name – Items are distinguished using a subscript or index • In C#, arrays are objects of a class named System. Array • The power of arrays becomes apparent when you begin to use subscripts • Subscript you use remains in the range of 0 through length -1 Visual C# 2005 34

Summary (continued) • foreach statement cycles through every array element without using subscripts • You can compare a variable to a list of values in an array • You can create parallel arrays to more easily perform a range match • The Binary. Search() method finds a requested value in a sorted array • The Sort() method arranges array items in ascending order Visual C# 2005 35

Summary (continued) • The Reverse() method reverses the order of items in an array • Multidimensional arrays require multiple subscripts to access the array elements • Types of multidimensional arrays – Two-dimensional arrays (rectangular arrays) – Three-dimensional arrays – Jagged arrays Visual C# 2005 36
- Slides: 36