Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

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.
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.
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 9 Introduction to 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.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and 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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
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.
Chapter Six: Working With Arrays in Visual Basic.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
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.
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.
Arrays1 From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
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.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
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.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
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.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Programming Right from the Start with Visual Basic .NET 1/e
IS 350 Arrays.
Single Dimensional Arrays
Using Procedures and Exception Handling
Variables and Arithmetic Operations
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Arrays Chapter 8

Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional u Parallel arrays u Dynamic arrays

Arrays u Often a set of objects or variables of the same type are used to hold related data  Prices of food items at a fast food restaurant  Scores on programming assignments u In the GPA calculator, rather than having five textboxes with different names to enter the grades, we might better describe the situation as an array of elements  Many data containers  One name

3 Variable Arrays u A variable like Stuff defines a data object into which a single quantity can be placed u A variable like Product defines a data object into which an array of information can be placed Product Stuff

Declaring Variable Arrays u An array is declared using the following syntax: Dim ArrayName(LowerBound To UpperBound) as VariableType u The LowerBound of the array is the lowest index in the array, while the UpperBound is the highest index in the array

Variable Array – Example (1) u Arrays are useful for when there are many instances of similar data u Instead of ten integer variables holding ten values, a single array can handle all ten intd intcinte intj intf intb inta inti inth intg intArray

Variable Array – Example (2) u Each variable location in an array is called an element u Use Dim statement to define size and type Dim intArray(1 to 10) as Integer ‘creates the structure below u Treat as normal variables u Reference using subscripts: intArray(4) = 114 intArray 114

Variable Array – Example (3) u Arrays allow programmers to write code once, but have it applied to many different data elements u Compare adding all the integers stored in the variables… intd intcinte intj intf intb inta inti inth intg intArray intSum = inta + intb + intc _ + intd + inte + intf + intg _ + inth + inti + inj

Variable Array – Example (4) u Arrays allow programmers to write code once, but have it applied to many different data elements u …to adding all the elements of the array intd intcinte intj intf intb inta inti inth intg intArray For intI = 1 to 10 intSum = intSum + intArray(intI) Next intI

Control Arrays u VB allows the programmer to create arrays of controls on the form  Just as there are variable arrays, there may be arrays of objects, such as command buttons, labels, textboxes or picture boxes  An array of labels might be made with one label for each grade point  An array of textboxes might be made with one textbox for each grade, or credits u Characteristics  Name with subscripted elements .Index property is the subscript (Properties Window)

Control Arrays u Using the same procedure, you can make arrays of labels, or of pictures, or of check boxes, etc. u You can change the GPA calculator program to make arrays of grades, credits and grade points

Creating a Control Array – Example u Create an toolbox object, perhaps a command button, changing its (name) and caption.

Creating a Control Array – Example 1.Copy and paste the object (Ctrl-C)(Ctrl-V) or Edit/Copy then Edit/Paste 2.The message “Do you want to create a control array?” appears. Answer “yes” 3.Type Edit/Paste or (Ctrl-V) several more times to make an array of commands

Creating a Control Array – Example u The Properties Window shows it all. u Note that the 4th command created is shown as “cmdButn(3)”

Creating a Control Array – Example u Arrange the buttons on the form as desired u Double clicking on any one of them opens the code window

Creating a Control Array – Example u The event handler is a bit strange. There is now a “parameter” inside the normally blank parentheses: Private Sub cmdButn_Click(Index As Integer) lblOutput.Caption = "## " & Index & " ##" End Sub

Creating a Control Array – Example u Run the process u Click each button u Observe the effect

Creating Multi-dimensional variable Arrays u Arrays can come in different dimensions u You need to specify the lower and upper bounds for each dimension  Array name; Dimensions; Bounds; Type  An example of a two-dimensional array: Dim strName (1 to 3, 1 to 4) as String

Advanced Variable Arrays u Multi-dimensional arrays u Parallel arrays u Searching arrays u Dynamic Arrays

Creating Multi-dimensional variable Arrays u When accessing/storing elements in the array…  Any integer literal, variable or expression can be used to subscript  Must stay within the bounds  Must have the correct number of subscripts  If storing, must assign the correct type of data u Given: Dim strName (1 to 3, 1 to 4) as String  Is strName (0, 0) = “Jones” valid?  Is strName (2) = “Smith” valid?  Is strName (4, 3) = “Larry” valid?  Is strName(3, 4) = 1402 valid?

Two-dimensional variable Arrays u Two dimensional arrays  require two subscripts  elements are listed (row, column) u Example Dim intSales(1 to 3, 1 to 4) _ as Integer u What is intSales(2, 3)?

Three-dimensional variable Arrays u Three dimensional arrays  require three subscripts (row, column, depth) u Example  Dim intSales (1 to 3, 1 to 4, 1 to 2) as Integer So the number 2134 is found where? the number 4521 is ______ the number 1402 is ______ the number 3425 is ______

Parallel Arrays u Parallel arrays are used to hold related data with different types  Dim strName (1 to 5) as String  Dim intSales (1 to 5) as Integer  So, if we know that Smith is in strName(2), we also know his or her sales is in intSales(2) strName intSales Jones Smith Frank Able Zean

Searching Arrays strTarget = "Q" intFound = 0 For intI = 1 to 10 If strTarget = strArray(intI) Then intFound = intI Exit For End If Next intI strArray A F S V W Q Z X Y L Searching implies looking for a target A simple sequential search looks through the array for the target

Dynamic Arrays u You can re-dimension an array at run-time u Rules for resizing arrays:  Use can empty dimension list: Dim intArray() as Integer  Use ReDim to assign dimensions: ReDim intArray (1 to intN, 1 to 2)  Use ReDim to change Bounds ReDim intArray (1 to (intN + 10), 1 to 3)  Use Preserve keyword to save data in array ReDim Preserve intArray(1 to intN, 1 to 5)