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.

Slides:



Advertisements
Similar presentations
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.
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.
1 CS 106, Winter 2009 Class 19, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
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.
Multidimensional Arrays in Java Vidhu S. Kapadia.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
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.
IE 212: Computational Methods for Industrial Engineering
 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.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
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 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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
Working with Arrays in MATLAB
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.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
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.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
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.
 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.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
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.
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.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
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.
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
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
EKT150 : Computer Programming
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Starting Out with Programming Logic & Design
Herbert G. Mayer, PSU CS Status 8/2/2013
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
C++ Array 1.
Presentation transcript:

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 by CS professors: Cynthia Brown & Robert Martin

2 Syllabus What is An Array What is An Array Arrays vs. Ranges Arrays vs. Ranges VBA Arrays VBA Arrays Dynamic Arrays Dynamic Arrays More Dimensions More Dimensions ArrayRangeDemo ArrayRangeDemo Copy Range to Array Copy Range to Array Row vs. Column Row vs. Column Using Random Data Using Random Data

3 What is an Array? An array is a collection of objects of the same type, each called an array elementAn array is a collection of objects of the same type, each called an array element Each element is identified by a unique number, its index, while keeping the array nameEach element is identified by a unique number, its index, while keeping the array name The index selects one element from the collective array data structureThe index selects one element from the collective array data structure In this way arrays resemble Excel rangesIn this way arrays resemble Excel ranges As with ranges, you can perform operations on an array in an efficient way using loopsAs with ranges, you can perform operations on an array in an efficient way using loops 3

4 Arrays vs. Ranges Array indexing is faster than range access, especially if dealing with large amounts of dataArray indexing is faster than range access, especially if dealing with large amounts of data Arrays offer more than three dimensionsArrays offer more than three dimensions

5 VBA Arrays VBA arrays are declared like variables:VBA arrays are declared like variables: Dim name As String ‘creates 1 string Dim a(20) As String ‘array of 20 strings The second Dim creates an array of 20 elements, each a string, numbered from 1 to 20The second Dim creates an array of 20 elements, each a string, numbered from 1 to 20 This numbering is called: indexingThis numbering is called: indexing Plausible indexed references are a(3) or a(j+3*i)Plausible indexed references are a(3) or a(j+3*i) You can optionally set your module to start numbering at 0 instead of 1; we’ll stick with default low index 1You can optionally set your module to start numbering at 0 instead of 1; we’ll stick with default low index 1 5

6 Alternate Array Declarations Instead of using the simplest form of declaration, you can use the following in VBA:Instead of using the simplest form of declaration, you can use the following in VBA: Dim testArray( 1 To 20 ) As Double Or even use a different lower bound:Or even use a different lower bound: Dim anotherTestArray(1955 To 2020) As String

7 Arrays vs. Scalar Variables A variable is a named place to store a valueA variable is a named place to store a value The compiler/interpreter selects the address = location in the computer’s memoryThe compiler/interpreter selects the address = location in the computer’s memory You reference the memory location by using the name of the variableYou reference the memory location by using the name of the variable An array stores a sequence of values, conceptually in sequential orderAn array stores a sequence of values, conceptually in sequential order You reference an element by using the array name and an indexYou reference an element by using the array name and an index 7

8 Arrays vs Variables 8 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

9 Things to Watch Careful not to try to put more items in the array than it will hold (bounds violation)Careful not to try to put more items in the array than it will hold (bounds violation) Doing so causes a runtime errorDoing so causes a runtime error Don’t index outside the allowable range of an array. This should also cause a runtime errorDon’t index outside the allowable range of an array. This should also cause a runtime error 9

10 Array Size to Use Declare your array big enough to handle any reasonable index rangeDeclare your array big enough to handle any reasonable index range In many languages you are out of luck if you guess wrong and run out of space in your arrayIn many languages you are out of luck if you guess wrong and run out of space in your array VBA offers an unusual ReDim featureVBA offers an unusual ReDim feature 10

11 ReDim This is a handy and quite unusual feature of VBThis is a handy and quite unusual feature of VB It lets you change the size of an array while the program is running!It lets you change the size of an array while the program is running! Example:Example: Dim exampleArray( small_Size )... ‘ now small_Size ends up being to small, then: ReDim Preserve exampleArray( 1 To big_Size ) ‘ big_Size being an integer value greater than small_Size Without the Preserve, data in original the array would be lost when you ReDimWithout the Preserve, data in original the array would be lost when you ReDim 11

12 Dynamic Arrays You can also declare an array without giving it an initial size. This is called a dynamic array. Empty parens convey this to VBAYou can also declare an array without giving it an initial size. This is called a dynamic array. Empty parens convey this to VBA Dim testDynamic() As String ‘ dynamic array Later in the program, when you know how big you need it to be, you can use ReDim to set the size: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)

13 More Dimensions We’ve been looking at one-dimensional arrays, but an array can have more dimensionsWe’ve been looking at one-dimensional arrays, but an array can have more dimensions In some languages, a multi-dimensional array type is literally an array of arrays, but VBA uses multiple dimensions to the same effectIn some languages, a multi-dimensional array type is literally an array of arrays, but VBA uses multiple dimensions to the same effect Example:Example: Dim arrayName ( 1 To 3, 1 To 4 ) As Integer ‘ how many elements total? 13

14 The Whole Array Dim arrayName( 1 To 3, 1 To 4 ) As Integer arrayName( 1, 2 ) = 20‘row 1, column

15 Copying A Range to an Array Handy if you are going to do extensive computations on the values in the rangeHandy 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)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 loopWe’ll do it cell by cell using a nested loop

16 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)) Set twoDArea = Range(Cells(1, 1), Cells(DIM1, DIM2)) End Sub

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

18 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 columnsFor 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 insteadThe next few slides show you this method and also how to go column by column instead

19 Filling the Range by Rows Sub FillRangeByRow() Dim rowNdx As Integer Dim rowNdx As Integer Dim colNdx As Integer Dim 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

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

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

22 The Result Note how the numbers increase down the columns first

23 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 6The 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!It also includes code for doing some data manipulation while the data is in the array; give it a try!