Arrays Group of variables that have a similar type

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

ISOM3230 Business Applications Programming
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
Arrays.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Chapter 7: Working with Arrays
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
 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.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic Games: Prepare for Hangman
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.
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.
© 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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
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.
Pointers OVERVIEW.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Research Topics in Computational Science. Agenda Survey Overview.
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.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CN2180 Chapter 4 Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
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.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
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.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays 1.
Chapter 11 - JavaScript: Arrays
VBA - Excel VBA is Visual Basic for Applications
Chapter 7: Working with Arrays
IS 350 Arrays.
Visual Basic 2010 How to Program
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
VBScript Session 2 Dani Vainstein.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
VBScript Session 2 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Arrays Group of variables that have a similar type Integers Strings Doubles An array of states, tax rates, social security numbers

Arrays Arrays can be in one dimension or two dimensions (actually you can have up to 60 dimensions)   1D --- myarray(4) as string can contain: NJ(0), NY(1), DE(2), PA(3)

Arrays 2D --- myarray(3,3) can contain a tic-tac-toe board: X (0,0) O (0,1) X (0,2) O X O X (2,0) O (2,1) X (2,2)

Arrays 1D Array: Dim arrayname(lower subscript TO upper subscript) as datatype   *** lower subscript is set to 0

Arrays - Examples Dim statearray(0 to 4) as string Creates a string array that will contain state ids Statearray(3) equals ‘DE’

Arrays - Examples Dim montharray(0 to 11) as string Creates a string array that will contain months in the year

Arrays - Examples Dim numberarray(1 to 10) as Integer Creates an integer array that will contain 10 numbers  

Arrays - Examples no set range of elements Dim ArrayVariable() as Integer no set range of elements   Elements in this array are from 0 to n

Arrays - Examples For x = 0 to 4 myarray(x) = x+1 Dim myarray(5) as Integer   For x = 0 to 4 myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x

Arrays - Examples Dim myarray(50) as string   myarray(0) = “hello”

Arrays - Examples 0 for numeric arrays spaces for string arrays Valuing an array: Default values of arrays are: 0 for numeric arrays spaces for string arrays false for boolean arrays  

Arrays - Examples Montharray(11) = “December” Numberarray(5) = 21 Valuing an array: (element by element hard coded) Montharray(11) = “December”   Numberarray(5) = 21

Arrays - Examples For x = 1 to 10 Valuing an array: (use a loop) For x = 1 to 10 Myarray(x) = Val(Inputbox(“Enter a _ Number”)) Next x  

Arrays - Examples Dim myarray(5) as Integer For x = 1 to 5   For x = 1 to 5 Myarray(x) = x+1 Form1.Print “array(“ & x & “) = “ & array(x) Next x

Arrays - Examples Getting array data: Txt1.text = montharray(5) ‘ displays ‘ MAY   Text2.text = numberarray(5) ‘display 21

Arrays - Examples PRACTICE: ZAK p. 724 “The Presidents” Write an application that allows a user to guess the first 10 US Presidents. Create a string array of the presidents (form load) and randomly ask the user to guess the 1st, 2nd, etc… president. Continued...

Arrays - Examples PRACTICE: ZAK p. 724 “The Presidents” Have them select from a radio button control array of 15 unordered presidents. One they click on a “GUESS” button, examine their choice and match it to the string array. Let them know if they are right or wrong and keep track of their choices. Once they successfully selected a president, disable that choice and DO NOT allow that number to be used again

Arrays - RANGES Lbound --- identifies and returns the lowest index in the array (usually 0)   Lbound(arrayname, DIM) ‘ DIM identifies the dimension of the array 1 is ‘ default  EXAMPLE: Result = Lbound(myarray)

Arrays - RANGES Ubound --- identifies and returns the highest index in the array   Ubound(arrayname, DIM) ‘ DIM identifies the dimension of the array 1 is default EXAMPLE: Result = Ubound(myarray) for x = 1 to Result … Next x

Arrays - RANGES EXAMPLE: Dim mystring(10) as String For count = Lbound(mystring) TO ubound(mystring) mystring(count) = InputBox(“Enter a Name:”) Next count

Arrays - RANGES EXAMPLE: RANGE Errors: Dim myarray(5) as integer Myarray(6) = 200 ‘ SUBSCRIPT ERROR

Arrays - Unbounded Unbounded/Dynamic Array: Declare an array with no predefined size. It can vary in size during run time. Dim myarray() as Integer

Arrays - Redim REDIM --- allocates/resets the size of an unbounded/dynamic array Redim myarray(10) ---- clears out the array and resizes it to the number of Elements ONLY USE THIS ON LOCALLY SCOPED ARRAYS !!!

Arrays - Redim REDIM To Preserve the existing array when redimming: Keeps existing values when redimming. Can only do when resetting the UPPERBOUNDRY of the array.

Arrays - Redim REDIM Redim Preserve myarray(10) preserves the existing contents of the array Redim Preserve myarray Ubound(myarray) + 1) dynamically adds 1 element to the array size

Arrays - Redim REDIM Erase --- deallocates an array space if its dynamically allocated. Reinitializes the elements of a fixed sized array. Erase myarray

Arrays - Redim PRACTICE: Thompson (p. 9-6) Count the freequency of dice roll outcomes

Arrays - Searching Searching and Array: Linear Iterate thru each array element looking for a specifice value, Example: ‘ find a given number in an integer array searchnum = 5 dim intarray(20) as integer do while (intarray(I) <> searchnum) AND (I < Ubound(intarray)) I = I + 1 loop

Arrays - 2 D Array 2 Dimensional Array: a matrix is a multidimensonal array used to store related information. Checkerboard, tic-tac-toe board.

Arrays - 2 D Array Dim my2darray(3,3) as Integer (X,Y) X = rows Y = columns R 0,0 0,1 0,2 0,3 O 1,0 1,1 1,2 1,3 W 2,0 2,1 2,2 2,3 S 3,0 3,1 3,2 3,3 COLUMNS (Y)

Arrays - 2 D Array EXAMPLE: Dim a2d(3, 3) a2d(0, 0) = 1 a2d(2, 2) = 5

Bubble Sort For x = Lbound(array) to Ubound(array) For y = Lbound(array) to Ubound(array)-1 if array(y) > array(y+1) then temp = array(y) array(y) = array(y+1) array(y+1) = temp end if Next

Binary Search Dim lowi ,highi, midi as integer Lowi = Lbound(array) Highi = Ubound(array)

Binary Search Do While Not boolfound AND lowi <= highi midi = Int((highi + lowi) / 2) if array(midi) = TARGETNUM then boolfound = TRUE elseif array(midi) > TARGETNUM then highi = midi -1 else lowi = mid + 1 end if loop

Binary Search If boolfound = TRUE then “FOUND” else “NOT FOUND” End if

ArrayList The ArrayList is a “class” that allows you to maintain an “array” of objects and have the ArrayList do all of the organizing of the data.

ArrayList You can perform the following types of actions with an ArrayList: Add Count Remove Sort Search

ArrayList You can perform the following types of actions with an ArrayList: Get an Item from the list (Item) Clear the list Determine if 2 elements are EQUAL (equal) Obtain the value at a specific index in the arraylist (IndexOf)

ArrayList The following are examples on how to use each of these actions:

ArrayList Create the ArrayList: Dim myAL As ArrayList = New ArrayList

ArrayList Add elements to the ArrayList: Dim cc As Integer = 10 myAL.Add(cc) myAL.Add(20)

ArrayList iterate thru all elements of the ArrayList (Count and Item): Dim z As Integer z = 0 Do While z < myAL.Count Button3.Text = Val(Button3.Text) + myAL.Item(z) z += 1 Loop

ArrayList Remove elements from the ArrayList: myAL.Remove(20)

ArrayList Check the list to see if a specific ob ject is in the ArrayList: TextBox1.Text = myAL.Contains(15)

ArrayList Return a specific element from the ArrayList: myAL.Item(0) = 40

ArrayList Sort elements of the ArrayList: myAL.Sort()

ArrayList Search for an element in the ArrayList: TextBox2.Text = myAL.BinarySearch(40)

ArrayList Check to see if 2 objects are equal: TextBox3.Text = myAL(0).Equals(myAL(0))

ArrayList Empty the ArrayList: myAL.Clear()

PRACTICE: Thompson (p. 9-26) Exercise 3 “Generated Numbers” PRACTICE: Thompson (p. 9-27) Exercise 5a “Generated Numbers” (5b advanced) PRACTICE: Thompson (p. 9-28) Exercise 8 “Duplicate Values” PRACTICE: Thompson (p. 9-29) Exercise 10 “Lockers” continued...

PRACTICE: Thompson (p. 9-30) Exercise 12 “Game Board 16” (12b advanced) PRACTICE: Thompson (p. 9-31 to 33) Exercises 13, 14, 15 and 16 (ADVANCED)