7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Arrays. Overview Overview of Arrays Creating Arrays Using Arrays.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Passing Arrays to Procedures: ByVal vs. ByRef. Passing Arrays to Procedures Passing the Array – Specify the name of the array without using parentheses.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2009 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2002 Prentice Hall. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 16: Searching, Sorting, and the vector Type.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
Arrays 1.
16 Searching and Sorting.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Data Structures I (CPCS-204)
Arrays 2.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
IS 350 Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2   Arrays 7.3   Declaring and Allocating Arrays 7.4   Examples Using Arrays   Allocating an Array.
Visual Basic 2010 How to Program
Chapter 7 Arrays.
JavaScript: Functions.
Siti Nurbaya Ismail Senior Lecturer
Advanced Programming Chapter 8: Arrays
C Arrays.
Java How to Program, Late Objects Version, 10/e
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Review of Arrays and Pointers
Object Oriented Programming in java
JavaScript Arrays.
Starting Out with Programming Logic & Design
CS2011 Introduction to Programming I Arrays (I)
24 Searching and Sorting.
Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
JavaScript: Arrays.
10 JavaScript: Arrays.
Visual Programming COMP-315
Presentation transcript:

7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are created

7.2 Arrays Array Position number Length property GetUpperBound method Group of contiguous memory locations that have the same name and the me type Position number Values that indicate specific locations within arrays The first element in every array is the zeroth element Length property Every array in Visual Basic “knows” its own length through the Length property GetUpperBound method Returns the index of the last element in the array The value returned by this GetUpperBound is one less than the value of the array’s Length property

7.2 Arrays Name of array (note that all elements of this array have the same name, numberArray) numberArray(0) -45 numberArray(1) 6 numberArray(2) numberArray(3) 72 numberArray(4) 1543 numberArray(5) -89 numberArray(6) numberArray(7) 62 numberArray(8) -3 Position number (index or subscript) of the element within array numberArray numberArray(9) 1 numberArray(10) 6453 numberArray(11) 78 Fig. 7.1 Array consisting of 12 elements.

7.3 Declaring and Allocating Arrays Memory The amount of memory required by an array depends on the length of the array and the size of the data type of the elements in the array Keyword New It is used to specify the size of the array and allocate memory for the array Array bounds Determine what indices can be used to access an element in the array Initializer list Specify the initial values of the elements in the array Keyword Nothing Denotes an empty reference

Several examples that demonstrate 7.4 Examples Using Arrays Several examples that demonstrate Declaration Allocation Initialization of arrays

The length property returns the number of elements in the array 1 ' Fig. 7.2: CreateArray.vb 2 ' Declaring and allocating an array. 3 4 Imports System.Windows.Forms 5 6 Module modCreateArray 7 8 Sub Main() 9 Dim output As String 10 Dim i As Integer 11 12 Dim array As Integer() ' declare array variable 13 array = New Integer(9) {} ' allocate memory for array 14 15 output &= "Subscript " & vbTab & "Value" & vbCrLf 16 17 ' display values in array 18 For i = 0 To array.GetUpperBound(0) 19 output &= i & vbTab & array(i) & vbCrLf 20 Next 21 22 output &= vbCrLf & "The array contains " & _ 23 array.Length & " elements." 24 25 MessageBox.Show(output, "Array of Integer Values", _ 26 MessageBoxButtons.OK, MessageBoxIcon.Information) 27 End Sub ' Main 28 29 End Module ' modCreateArray CreateArray.vb A variable capable of storing a reference to an array of Integer elements Allocate an array of 10 elements using New and assigns it to array Appends to output the headings for the columns displayed by the program For structure is used to append the index number and value of each array element to output The length property returns the number of elements in the array

7.4 Examples Using Arrays CreateArray.vb

One statement is used to declare the two arrays 1 ' Fig. 7.3: InitArray.vb 2 ' Initializing arrays. 3 4 Imports System.Windows.Forms 5 6 Module modInitArray 7 8 Sub Main() 9 Dim output As String 10 Dim i As Integer 11 12 Dim array1, array2 As Integer() ' declare two arrays 13 14 ' initializer list specifies number of elements 15 ' and value of each element 16 array1 = New Integer() {32, 27, 64, 18, 95, _ 17 14, 90, 70, 60, 37} 18 19 ' allocate array2 based on length of array1 20 array2 = New Integer(array1.GetUpperBound(0)) {} 21 22 ' set values in array2 by a calculation 23 For i = 0 To array2.GetUpperBound(0) 24 array2(i) = 2 + 2 * i 25 Next 26 27 output &= "Subscript " & vbTab & "Array1" & vbTab & _ 28 "Array2" & vbCrLf 29 30 ' display values for both arrays 31 For i = 0 To array1.GetUpperBound(0) 32 output &= i & vbTab & array1(i) & vbTab & array2(i) & _ 33 vbCrLf 34 Next 35 InitArray.vb One statement is used to declare the two arrays Allocates the 10 elements of array1 with New and initialize the values in the array, using an initializer list Allocates array2, whose size is determined by arry1.GetUpperBound(0), so that array1 and array2 have the same upper bound Initializes each element in array2 to the even integers Uses the values in the arrays to build String output, which is displayed in a MessageBox

InitArray.vb 36 MessageBox.Show(output, "Array of Integer Values", _ 37 MessageBoxButtons.OK, MessageBoxIcon.Information) 38 End Sub ' Main 39 40 End Module ' modInitArray InitArray.vb

Declares, allocates and initializes the 10-element array, array 1 ' Fig. 7.4: SumArray.vb 2 ' Computing sum of elements in array. 3 4 Imports System.Windows.Forms 5 6 Module modSumArray 7 8 Sub Main() 9 Dim array As Integer() = New Integer() _ 10 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 11 12 Dim total As Integer = 0, i As Integer = 0 13 14 ' sum array element values 15 For i = 0 To array.GetUpperBound(0) 16 total += array(i) 17 Next 18 19 MessageBox.Show("Total of array elements: " & total, _ 20 "Sum the elements of an Array", MessageBoxButtons.OK, _ 21 MessageBoxIcon.Information) 22 End Sub ' Main 23 24 End Module ' modSumArray Declares, allocates and initializes the 10-element array, array SumArray.vb Performs the addition

7.5 Passing Arrays to Procedures Passing the Array Specify the name of the array without using parentheses Every array object “knows” its own upper bound Do not need to pass the upper bound of the array as a separate argument In Visual Basic, arrays always are passed by reference Receiving the array The procedure’s parameter list must specify that an array will be recieved

Appends the five elements of array1 to String output 1 ' Fig. 7.8: PassArray.vb 2 ' Passing arrays and individual array elements to procedures. 3 4 Imports System.Windows.Forms 5 6 Module modPassArray 7 Dim output As String 8 9 Sub Main() 10 Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5} 11 Dim i As Integer 12 13 output = "EFFECTS OF PASSING ENTIRE ARRAY " & _ 14 "BY REFERENCE:" & vbCrLf & vbCrLf & _ 15 "The values of the original array are:" & vbCrLf 16 17 ' display original elements of array1 18 For i = 0 To array1.GetUpperBound(0) 19 output &= " " & array1(i) 20 Next 21 22 ModifyArray(array1) ' array is passed by reference 23 24 output &= vbCrLf & _ 25 "The values of the modified array are:" & vbCrLf 26 27 ' display modified elements of array1 28 For i = 0 To array1.GetUpperBound(0) 29 output &= " " & array1(i) 30 Next 31 PassArray.vb Appends the five elements of array1 to String output Passes array1 to procedure ModifyArray Appends the elements of array1 to output

Multiplies the elements of arrayParameter by 2 32 output &= vbCrLf & vbCrLf & _ 33 "EFFECTS OF PASSING ARRAY ELEMENT " & _ 34 "BY VALUE:" & vbCrLf & vbCrLf & "array1(3) " & _ 35 "before ModifyElementByVal: " & array1(3) 36 37 ' array element passed by value 38 ModifyElementByVal(array1(3)) 39 40 output &= vbCrLf & "array1(3) after " & _ 41 "ModifyElementByVal: " & array1(3) 42 43 output &= vbCrLf & vbCrLf & "EFFECTS OF PASSING " & _ 44 "ARRAY ELEMENT BY REFERENCE: " & vbCrLf & vbCrLf & _ 45 "array1(3) before ModifyElementByRef: " & array1(3) 46 47 ' array element passed by reference 48 ModifyElementByRef(array1(3)) 49 50 output &= vbCrLf & "array1(3) after " & _ 51 "ModifyElementByRef: " & array1(3) 52 53 MessageBox.Show(output, "Passing Arrays", _ 54 MessageBoxButtons.OK, MessageBoxIcon.Information) 55 End Sub ' Main 56 57 ' procedure modifies array it receives (note ByVal) 58 Sub ModifyArray(ByVal arrayParameter As Integer()) 59 Dim j As Integer 60 61 For j = 0 To arrayParameter.GetUpperBound(0) 62 arrayParameter(j) *= 2 63 Next 64 65 End Sub ' ModifyArray 66 PassArray.vb Multiplies the elements of arrayParameter by 2

PassArray.vb 67 ' procedure modifies integer passed to it 68 ' original is not be modified (note ByVal) 69 Sub ModifyElementByVal(ByVal element As Integer) 70 71 output &= vbCrLf & "Value received in " & _ 72 "ModifyElementByVal: " & element 73 element *= 2 74 output &= vbCrLf & "Value calculated in " & _ 75 "ModifyElementByVal: " & element 76 End Sub ' ModifyElementByVal 77 78 ' procedure modifies integer passed to it 79 ' original is be modified (note ByRef) 80 Sub ModifyElementByRef(ByRef element As Integer) 81 82 output &= vbCrLf & "Value received in " & _ 83 "ModifyElementByRef: " & element 84 element *= 2 85 output &= vbCrLf & "Value calculated in " & _ 86 "ModifyElementByRef: " & element 87 End Sub ' ModifyElementByRef 88 89 End Module ' modPassArray PassArray.vb

7.6 Passing Arrays: ByVal vs. ByRef Visual Basic.NET A variable that “stores” an object, such as an array, does not actually store the object itself The variable stores a reference to the object Location in memory where the object is already stored ByVal Causes the value of the argument to be copied to a local variable in the procedure Changes to the local variable are reflected in the local copy of that variable, not in the original variable in the calling program But if the argument is of a reference type, like an array, passing it ByVal actually passes it by reference, so changes to the object affect the original objects in the callers

7.6 Passing Arrays: ByVal vs. ByRef When an array is passed with ByRef the called procedure gains control over the passed reference itself This allows the called procedure to replace the original reference in the object with another object or even Nothing

firstArray is passed to FirstDouble 1 ' Fig. 7.9: ArrayReferenceTest.vb 2 ' Testing the effects of passing array references using 3 ' ByVal and ByRef. 4 5 Module modArrayReferenceTest 6 7 Sub Main() 8 Dim i As Integer 9 10 ' declare array references 11 Dim firstArray As Integer() 12 Dim firstArrayCopy As Integer() 13 14 ' allocate firstArray and copy its reference 15 firstArray = New Integer() {1, 2, 3} 16 firstArrayCopy = firstArray 17 18 Console.WriteLine("Test passing array reference " & _ 19 "using ByVal.") 20 Console.Write("Contents of firstArray before " & _ 21 "calling FirstDouble: ") 22 23 ' print contents of firstArray 24 For i = 0 To firstArray.GetUpperBound(0) 25 Console.Write(firstArray(i) & " ") 26 Next 27 28 ' pass firstArray using ByVal 29 FirstDouble(firstArray) 30 31 Console.Write(vbCrLf & "Contents of firstArray after " & _ 32 "calling FirstDouble: ") 33 ArrayReferenceTest.vb Copies reference firstArray to variable firstArrayCopy, now they reference the same object Prints contents first to verify that FirstDouble indeed changes the array’s contents firstArray is passed to FirstDouble

Compares references firstArray and firstArrayCopy 34 ' print contents of firstArray 35 For i = 0 To firstArray.GetUpperBound(0) 36 Console.Write(firstArray(i) & " ") 37 Next 38 39 ' test whether reference was changed by FirstDouble 40 If firstArray Is firstArrayCopy Then 41 Console.WriteLine(vbCrLf & "The references are " & _ 42 "equal.") 43 Else 44 Console.WriteLine(vbCrLf & "The references are " & _ 45 "not equal.") 46 End If 47 48 ' declare array references 49 Dim secondArray As Integer() 50 Dim secondArrayCopy As Integer() 51 52 ' allocate secondArray and copy its reference 53 secondArray = New Integer() {1, 2, 3} 54 secondArrayCopy = secondArray 55 56 Console.WriteLine(vbCrLf & "Test passing array " & _ 57 "reference using ByRef.") 58 Console.Write("Contents of secondArray before " & _ 59 "calling SecondDouble: ") 60 61 ' print contents of secondArray before procedure call 62 For i = 0 To secondArray.GetUpperBound(0) 63 Console.Write(secondArray(i) & " ") 64 Next 65 66 ' pass secondArray using ByRef 67 SecondDouble(secondArray) 68 Compares references firstArray and firstArrayCopy ArrayReferenceTest.vb

Bubble Sort (a.k.a. sinking sort) 7.7 Sorting Arrays Sorting Sorting data is one of the most popular computing applications Sometimes, the simplest algorithms perform poorly Bubble Sort (a.k.a. sinking sort) Smaller values “bubble” their way to the top of the array, (i.e. toward the first element) Larger values “sink” to the bottom of the array, (i.e. toward the end) In general only n-1 passes are needed to sort an n-element array The bubble sort is easy to program, but runs slowly Becomes apparent when sorting large arrays

7.8 Searching Arrays: Linear Search and Binary Search The process of locating a particular element value in an array Linear Search Simple searching technique Works well for small or unsorted arrays On average half the elements of the array will be compared Binary Search If array is sorted, binary search is more efficient, but also a more complex technique After each comparison, the binary search algorithm eliminates half of the elements in the array The maximum number of comparisons in a binary search is the exponent of the first power of 2 that is greater than the number of elements being searched

7.9 Multidimensional Rectangular and Jagged Arrays Multidimensional arrays (multiple-subscripted) Require two or more indices to identify particular elements Rectangular arrays Two indices, first identifies the element’s row, the second the elements column A rectangular two-dimensional array with m rows and n columns is called an m-by-n array Jagged arrays Jagged arrays are maintained as arrays of arrays Rows in jagged arrays can be of different lengths

7.9 Multidimensional Rectangular and Jagged Arrays Column 0 Column 1 Column 2 Column 3 Row 0 a(0, 0) a(0, 1) a(0, 2) a(0, 3) Row 1 a(1, 0) a(1, 1) a(1, 2) a(1, 3) Row 2 a(2, 0) a(2, 1) a(2, 2) a(2, 3) Column index Row index (or subscript) Array name Fig. 7.15 Two-dimensional array with three rows and four columns.

Allocates array1 with six initializers in two sublists 1 ' Fig. 7.16: MultidimensionalArrays.vb 2 ' Initializing multi-dimensional arrays. 3 4 Imports System.Windows.Forms 5 6 Module modMultidimensionalArrays 7 8 Sub Main() 9 Dim output As String 10 Dim i, j As Integer 11 12 ' create rectangular two-dimensional array 13 Dim array1 As Integer(,) 14 array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}} 15 16 ' create jagged two-dimensional array 17 Dim array2 As Integer()() = New Integer(2)() {} 18 19 array2(0) = New Integer() {1, 2} 20 array2(1) = New Integer() {3} 21 array2(2) = New Integer() {4, 5, 6} 22 23 output = "Values in array1 by row are " & vbCrLf 24 25 For i = 0 To array1.GetUpperBound(0) 26 27 For j = 0 To array1.GetUpperBound(1) 28 output &= array1(i, j) & " " 29 Next 30 31 output &= vbCrLf 32 Next 33 MultidimensionalArrays.vb Allocates array1 with six initializers in two sublists The declaration and allocation of array2 creates a jagged array of 3 arrays Traverses the array in two dimensions

In a jagged two-dimensional array, the second dimension is actually the first dimension of a separate array 34 output &= vbCrLf & "Values in array2 by row are " & _ 35 vbCrLf 36 37 For i = 0 To array2.GetUpperBound(0) 38 39 For j = 0 To array2(i).GetUpperBound(0) 40 output &= array2(i)(j) & " " 41 Next 42 43 output &= vbCrLf 44 Next 45 46 MessageBox.Show(output, _ 47 "Initializing Multi-Dimensional Arrays", _ 48 MessageBoxButtons.OK, MessageBoxIcon.Information) 49 End Sub ' Main 50 51 End Module ' modMultidimensionalArrays MultidimensionalArrays.vb

7.10 Variable-Length Parameter Lists Keyword ParamArray Makes it possible to create procedures that receive a variable number of arguments You can not use ParamArray with a multidimensional array You can not use ByRef with ParamArray All arguments passed to the ParamArray array must be of the same type as the array

Applies keyword ParamArray to array1 1 ' Fig. 7.18: ParamArrayTest.vb 2 ' Using ParamArray to create variable-length parameter lists. 3 4 Module modParamArrayTest 5 6 Sub Main() 7 AnyNumberArguments() 8 AnyNumberArguments(2, 3) 9 AnyNumberArguments(7, 8, 9, 10, 11, 12) 10 11 End Sub ' Main 12 13 ' receives any number of arguments in array 14 Sub AnyNumberArguments(ByVal ParamArray array1 _ 15 As Integer()) 16 17 Dim i, total As Integer 18 total = 0 19 20 If array1.Length = 0 Then 21 Console.WriteLine("Procedure AnyNumberArguments" & _ 22 " received 0 arguments.") 23 Else 24 Console.Write("The total of ") 25 26 For i = 0 To array1.GetUpperBound(0) 27 Console.Write(array1(i) & " ") 28 total += array1(i) 29 Next 30 31 Console.WriteLine("is {0}.", total) 32 End If 33 Calls procedure AnyNumberArguments, passing a different number of arguments each time ParamArrayTest.vb Applies keyword ParamArray to array1 Determines whether or not zero arguments where passed, if not displays array1’s elements and their sum

34 End Sub ' AnyNumberArguments 35 36 End Module ' modParamArrayTest ParamArrayTest.vb Procedure AnyNumberArguments received 0 arguments. The total of 2 3 is 5. The total of 7 8 9 10 11 12 is 57.

7.11 For Each/Next Repetition Structure Provided to iterate through the values in a data structure, such as an array Instead of a counter, For Each/Next uses a variable to represent the value of each element Useful when the indices of the elements are not important Particularly useful for looping through arrays of objects

1 ' Fig. 7.19: ForEach.vb 2 ' Program uses For Each/Next to find a minimum grade. 3 4 Module modForEach 5 6 Sub Main() 7 Dim gradeArray As Integer(,) = New Integer(,) _ 8 {{77, 68, 86, 73}, {98, 87, 89, 81}, {70, 90, 86, 81}} 9 10 Dim grade As Integer 11 Dim lowGrade As Integer = 100 12 13 For Each grade In gradeArray 14 15 If grade < lowGrade Then 16 lowGrade = grade 17 End If 18 19 Next 20 21 Console.WriteLine("The minimum grade is: {0}", lowGrade) 22 End Sub ' Main 23 24 End Module ' modForEach ForEach.vb Specifies a variable grade and an array gradeArray. The structure iterates through all the elements in gradeArray, sequentially assigning each value to variable grade The values are compared to variable lowGrade, which stores the lowest grade in the array The minimum grade is: 68