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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
Lecture Roger Sutton CO331 Visual Programming 12: One-dimensional Arrays 1.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Chapter 8 Arrays and Strings
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
Multi-Dimensional Arrays
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 Arrays and Strings
Week Arrays, Timers, and More 9. 2 Introduction Arrays are like groups of variables that allow you to store sets of similar data A single dimension array.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
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 
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Group of variables that have a similar type
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8 Arrays and More.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
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.
Visual Basic Programming I 56:150 Information System Design.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
ILM Proprietary and Confidential -
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
IS 350 Arrays.
2. Understanding VB Variables
VBScript Session 2 Dani Vainstein.
Lbound and Ubound Functions
Can store many of the same kind of data together
Arrays.
VBScript Session 2 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
JavaScript: Arrays.
Presentation transcript:

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 of an array.  Index – number that identifies each member of an array usually starting at 0 unless specified otherwise.  Option Base – Placed in the General Section and is used to change the index from 0 to 1.  Example of an Array: BobSamSueKimAbe

 Array Declaration - An array is declared with a Dim, Private, or Static statement that includes the identifier followed by the upper bound of the array in parentheses and the data type. Example: Dim strName(5) as String ‘ 5 elements  Lower Bound – When declaring arrays, you can specify the start index by using the lower bound to declare an array. Example: Dim strNames (1 to 5) as String ‘Where 1 is the lower bound instead of 0  Array initialization - When an array is declared, the element variables are set to default values. Numeric variables are set to 0. Strings are set to empty Strings and Boolean array elements to False values.

 Accessing an element -To gain access to an element of an array, you must use the parentheses. Example: lblAns.Caption = strName(4)  Display the name in the 5 th position since it starts at zero.  Assigning a value to an array - You must use the array name with the index number in the parentheses. Example: strname(3) = “Bobby”  LBound and Ubound - The LBound and UBound are built-in functions used to determine the lower and upper bound of an array. Examples: LBound(ArrayName) or UBound(ArrayName)

 For…Next Loop  The For…Next loop is often used for arrays because of the counter of the loop can be used to access the indexes.  Example: For intIndex = LBound(ArrayName) To UBound(ArrayName) ArrayName(intIndex) = InputBox(“Enter name”, “Student”) Next intIndex

 Range Errors  A run-time error occurs if code refers to an index outside the lower bound to upper bound range of an array. The error message produced is “Subscript out of range”. Subscript is the same as index.

 Arrays can be passes as arguments and declared as procedure parameters by using the array name followed by an empty set of parentheses. However, arrays are required to be passed by reference (ByRef).  Example: Function SumOfItems(ByRef intNumArray() As Integer) as Long Dim lngIndex As Long Dim lngSum As Long lngSum = 0 For lngIndex = LBound(intNumArray) To UBound(intNumArray) lngSum = lngSum + intNumArray(lngIndex) Next lngIndex SumOfItems = lngSum End Function  Notice the Function passes the array intNumArray() by reference.

 The array name followed by an empty set of parentheses is used in a procedure call. Example:  lngTotal = SumOfItems(intDataArray())  The array intDataArray() is passed through the Function Parameter list.  Single Element parameter  A single element of an array can be passed by value or reference as an argument. Example: Sub DisplayElement(ByVal intNumber As Integer, ByRef lblLabel as Label) lblLabel.Caption = intNumber End Sub Call DisplayElement(intDataArray(3)) ‘The 3 rd element of the array is passed

 It is always a good idea to use meaningful names for array indexes.

 Linear Search – Works by proceeding from one element to the next through the array until the desired value is found or the entire array has been searched.  The Function below searches an array Function FindItemIndex(ByRef intDataArray() as Integer, ByVal intSearchItem as _ Integer) as Integer Dim intIndex as Integer intIndex = LBound(intDataArray) Do While (intDataArray(intIndex) <> intSearchItem) And (intIndex <_ UBound(intDataArray)) intIndex = intIndex + 1 Loop If intDataArray(intIndex) = intSearchItem Then FindItemIndex = intIndex ‘Item found Else FindItemIndex = -1 ‘Item not found End if End Function

 A Dynamic Array – can vary in size during run time and is used when the size of an array is unknown at the start of the program or when the size of the array changes throughout the run of the program.  Declaration Example: Dim intDataArray() as Integer  Notice there is no value between the parentheses meaning the size is unknown.  ReDim – statement that allocates space for the elements of a dynamic array.  The upper bound for the array must be included. The lower bound may also be included. Example: ReDim intDataArray(1 To 5)  You can use ReDim over and over again to resize the array. However, each time you ReDim an array all data is lost.  ReDim Preserve – allows you to keep old data when resizing only changing the upper bound. You get an error if you try to change the lower bound. You cannot use ReDim Preserve until the dynamic array been dimensioned (ReDim).

 A matrix is a multidimensional variable array that is used to store related information. A 2D array can be used to represent data that relates to a grid.  Declaring a 2D Array:  A matrix is declared with a Dim, Private, or Static statement that includes the identifier following by the upper bound of the first dimensional (the rows) and the upper bound of the second dimensional (the columns) separated by a comma in parentheses and then the data type. Example: Dim strTTTBoard(2,2) As String  Declares a 3x3 matrix

 Assigning Elements of a 2D Array  Each element can be assigned a value using the equal symbol (=) with the name of 2D array and row and column number in the parentheses. Example: strTTTBoard(0,1) = cmdBoard.Caption  The lower bound of each dimension of a matrix can be specified in the matrix declaration. Example: Dim strTTTBoard(1 To 3, 1 To 3) As String LBound & UBound  The LBound and UBound VB Built-in functions may also be used with matrices to determine lower and upper bounds of the dimensions. Examples:‘Dimension is 1 for row dimension or 2 for column dimension LBound(MatrixName, Dimension) UBound(MatrixName, Dimension)

 Nested Loops  Nested For…Next loops are often used to access the elements of a matrix because the loop counter indicates the row of the matrix and the other counter indicates the columns.  Example: Dim strTTTBoard(2,2) as String For lngRow = LBound(strTTTBoard, 1) To UBound(strTTTBoard, 1) For lngRow = LBound(strTTTBoard, 2) To UBound(strTTTBoard, 2) lstMoveAddItem strTTTBoard(lngRow, lngCol) Next lngCol Next lngRow

 A matrix may also be dynamic. As with a one-dimensional dynamic array, a dynamic matrix is declared in a Dim statement with an empty set of parentheses and then a ReDim statement is later used to change the dimensions.