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.

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
1 CS 106, Winter 2009 Class 19, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
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.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
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
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.
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.
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.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Spreadsheet Models for Managers: Session 14 14/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 14 Using Macros II Function.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 CS 106 Computing Fundamentals II Chapter 75 “Arrays” Herbert G. Mayer, PSU CS Status 7/31/2013 Initial content copied verbatim from CS 106 material developed.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Ranges. Unlike many of our programming concepts, the idea of a Range is particular to Excel The ideas and code discussed in these slides can be found.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
2. Understanding VB Variables
EKT150 : Computer Programming
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
CIS16 Application Development and Programming using Visual Basic.net
Arrays.
Introduction to Computer Programming IT-104
Presentation transcript:

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 In this way they resemble Excel ranges As with ranges, you can perform operations on an array in an efficient way using loops 2

Why use arrays when you have ranges? You might not want to do the calculations on a sheet in the workbook; the array is self- contained and you don’t risk wiping out other data or revealing intermediate computations to the user It can be very much faster to manipulate data in an array rather than in a range, especially if you are dealing with large amounts of data You might want three or more dimensions

VBA Arrays VBA arrays are declared much like variables: Dim name As String ‘creates a normal variable Dim nameArray(20) As String ‘creates an array The second Dim creates an array of 20 elements, numbered from 1 to 20, similar to part of a row or column You can optionally set your module to start numbering at 0 instead of 1; this is better for some kinds of code, but we’ll stick with the default 4

Alternate Array Declarations Instead of using the simplest form of declaration, as on the previous slide, you can use Dim testArray(1 To 20) As Double Or use a different lower bound: Dim anotherTestArray(1955 To 2020) As String

Arrays vs Variables A variable is a place to store a value; a place in the computer’s memory You reference the memory location by using the name of the variable An array stores a sequence of values You reference an element by using the array name and an index 6

Arrays vs Variables 7 Variable num of type Double occupies some location in memory num 3.2 Array numArray of type Double has 6 elements and occupies 6 times the memory of one variable of type Double. Here, numArray(3) has value 3.5. numArray

Things to watch out for with arrays You must be careful not to try to put more items in the array than it will hold (overflow). Doing so should cause a runtime error but may instead just result in mysterious bugs You should not try to index beyond the end of an array. This should also cause a runtime error, but might just result in getting a strange answer back. 8

Array Size to Use Try to make your array big enough to handle any reasonable expectation of how your program will be used In many languages you are out of luck if you guess wrong and run out of space in your array. In VBA though we have the ReDim feature 9

ReDim This is a very unusual feature of VB It lets you change the size of an array while the program is running! Example: ReDim Preserve exampleArray(1 To newSize) Without the Preserve, all the data in the array is lost when you ReDim. 10

Dynamic Arrays You can also declare an array without giving it an initial size. This is called a dynamic array. (The empty parens tell VBA it’s an array) Dim testDynamic() As String Later in the program, when you know how big you need it to be, you can use ReDim to set the size: ReDim testDynamic (1 to size) [this slide used info from

More Dimensions We’ve been looking at one-dimensional arrays, but an array can have two (or more!) dimensions This is the array version of nesting. In some languages you literally have an array of arrays, but VBA uses more dimensions to achieve the same effect Example: Dim arrayName (1 To 3, 1 To 4) As Integer arrayName(1, 2) = 20 12

Showing the whole array Dim arrayName (1 To 3, 1 To 4) As Integer arrayName(1, 2) = 20‘row 1, column

The ArrayRangeDemo You will find most of the code in this presentation in the workbook called ArrayRangeDemo.xlsm Indexing is important when working with both arrays and ranges The demo illustrates indexing in two dimensional arrays and ranges; since it is easiest to see what is happening with ranges, most of the code modifies them

Copying A Range to an Array This is something you might want to do if you are going to do extensive computations on the values in the range It is possible to do this directly with one statement, if everything is declared just right (for info see msdn.microsoft.com/en-us/library) We’ll do it cell by cell using a nested loop

Declarations Here is our array declaration: Const DIM1 As Integer = 8 'length Const DIM2 As Integer = 10 'width Dim demo2DArray(1 To DIM1, 1 To DIM2) As Double And the range: Dim twoDArea As Range 'global Sub Workbook_Open() Set twoDArea = Range(Cells(1, 1), Cells(DIM1, DIM2)) End Sub

Copy Range to Array Sub RangetoTwoDArray() Dim rowNdx As Integer, colNdx As Integer For rowNdx = 1 To DIM1 For colNdx = 1 To DIM2 demo2DArray(rowNdx, colNdx) = Cells(rowNdx, colNdx).Value Next colNdx Next rowNdx End Sub

Row vs Column For some reason, in a two-dimensional array or range, it is customary to have the outer loop go row by row and the inner loop go across the columns The next few slides show you this method and also how to go column by column instead

Filling the Range by Rows Sub FillRangeByRow() Dim rowNdx As Integer, colNdx As Integer, count As Integer count = 1 For rowNdx = 1 To DIM1 For colNdx = 1 To DIM2 Cells(rowNdx, colNdx) = count count = count + 1 Next colNdx Next rowNdx End Sub

The Result Note how the numbers increase across the rows first.

Filling the Range by Columns Sub FillRangeByColumn() Dim rowNdx As Integer, colNdx As Integer, count As Integer count = 1 For colNdx = 1 To DIM2 For rowNdx = 1 To DIM1 Cells(rowNdx, colNdx) = count count = count + 1 Next rowNdx Next colNdx End Sub

The Result Note how the numbers increase down the columns first

Using Random Data The demo has examples of filling the range (or array) with random data, either the basic random numbers which range between 0 and 1, or a modified version that generates random numbers between 1 and 6 It also includes code for doing some data manipulation while the data is in the array; give it a try!