IE 212: Computational Methods for Industrial Engineering

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
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.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
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.
Using Arrays and File Handling
Multi-Dimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Chapter 8 Arrays and Strings
Microsoft Visual C++.NET Chapter 61 Memory Management.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 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 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 
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved 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.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
© 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.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
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 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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Arrays 1.
Computer Programming BCT 1113
IS 350 Arrays.
Spreadsheet-Based Decision Support Systems
JavaScript: Functions.
ARRAYS.
VBScript Session 2 Dani Vainstein.
7 Arrays.
Lbound and Ubound Functions
Arrays.
VBScript Session 2 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
Presentation transcript:

IE 212: Computational Methods for Industrial Engineering Lecture Notes #6: Function Procedures & Arrays Saeed Ghanbartehrani Summer 2015

Overview Organizing Sub Procedures Creating Function Procedures Methods to Pass Variables in Sub and Function Procedures Public and Private Procedures Using Arrays in Excel VBA Summary

Organizing Sub Procedures You should group various actions into several smaller sub procedures rather than having one large sub procedure in which the entire program is written Allows for programs to run more efficiently Promotes code reusability The ideal module structure for your program is to have one Main() sub procedure from which other sub procedures are called This Main macro will usually be assigned to a “Start” button on the “Welcome” sheet To call another sub procedure, we use the command Call followed by the sub procedure name

Organizing Sub Procedures (cont.) For example, consider three different sub procedures, called GetInput(), Calculations(), and DisplayResults() We can then call these three sub procedures from the Main() sub procedure as follows Sub Main() Call GetInput Call Calculations Call DisplayResults End Sub You can also call other sub procedures from these three sub procedures

Function Procedures A function procedure is a type of procedure that can take arguments and execute a series of statements A function procedure differs from a sub procedure in that It is invoked through its name It returns a single value Critical elements when writing a function procedure The name of the function The type of the function

Function Procedures (cont.) Syntax of a function procedure Function name ([arglist]) [statements] [name = expression] [Exit Function] End Function A function procedure can be called from any sub procedure or any other function procedure Function FunctionName() ….

Function Procedures (cont.) To illustrate the proper use of a function in Excel VBA, we will use an example where the sum of two values is calculated We will use AddTwoNumbers as the name of the function This would imply that we have declared x and y as variables in the procedure from which we call our function

Function Procedures (cont.) The function itself is quite simple in this case To return a value from a function in Excel VBA, you should assign a value to the name of the function procedure The variables x and y in this example do not need to be declared They are the variables used throughout the function procedure This would imply that we have declared x and y as variables in the procedure from which we call our function

Function Procedures (cont.) The variables x and y in the function AddTwoNumbers will assume the respective data types assigned to a and b If we had defined the function AddTwoNumbers with data types in the argument, we would be restricted to only passing variables of that data type Function AddTwoNumbers(x As Integer, y As Integer) Notice also that the variable names used as arguments when the function is called and the variable names used in the function procedure statement do not need to be the same a and b in Sub procedure x and y in Function procedure

Methods to Pass Variables There are two methods to pass variables in sub procedures and function procedures By reference (default) By value Passing a value by reference implies that the procedure (or function) can access and possibly modify the original value assigned to the variable Passing by value provides a copy of the value of the variable for the procedure or function to use Therefore, the original value of the variable remains intact

Methods to Pass Variables (cont.) To pass a variable by value, we must use the keyword ByVal when defining the arguments that the function will take Function AddTwoNumbers(ByVal x, ByVal y) AddTwoNumbers = x + y End Function Alternatively, a variable can be passed by value by enclosing it in parentheses when the sub procedure or function is called Call Multiply((i), j)  Sub procedure result = Multiply((a), (b))  Function procedure

Public and Private Procedures The scope of a sub procedure, like a variable, can also be defined as Public or Private A private sub procedure is declared by putting the word Private before the Sub statement Private sub procedures can only be called from procedures in the same module Private sub procedures are not listed when you try to run a macro in Excel A public sub procedure can be called from any other procedure The word Public can be put in front of the Sub statement, or the Sub statement can be written without a preceding statement

Using Arrays in Excel VBA

Introduction to Arrays in Excel VBA Arrays store series of data elements that can be manipulated or referred to later Arrays are very useful when we need to perform the same operation (or a series of operations) on a group of values Arrays may also make programs more compact (and possibly more efficient) For example, if we know a program’s output will be 3 integer variables, we can store these values in an integer array of size 3 The set of data elements stored in an array must all be of the same data type A note on efficiency: arrays can be very efficient tools for programming systems with multiple series of data. However, you should also compare the benefit of using arrays with the option of printing values to the spreadsheet for storage in a table. In some cases, as we have seen earlier, there was no need to store the data in an array variable. We feel, however, that using arrays will usually be the more beneficial method.

Declaring Arrays To declare an array, the variable declaration keywords Dim, Private, or Public can be used VBA will recognize a variable as an array and not a scalar variable because a set of parentheses (and, in some cases, the size of the array) is included in the variable declaration Dim stdNames(10) As String Dim stdGrades() As Integers This is how the one-dimensional array tempData, containing 10 elements of data type Double, is declared Dim tempData(9) As Double

Declaring Arrays (cont.) For a one-dimensional array, a single number is all that is necessary to specify the size To declare multi-dimensional arrays, you need to specify the size of each dimension (i.e., rows and columns), separated by a comma Dim data(2, 3) As Double A multi-dimensional array can be defined for up to 60 dimensions in VBA

Arrays’ Indices You can refer to the individual elements in an array using an index value When an array is declared in Excel VBA, a default index is assigned to each element The default index of the first element in a one-dimensional array is 0 The default index of the first element in a two-dimensional array is 0,0

Arrays’ Indices (cont.) Examples expData( ) = myOSUCourses ( , ) =

Arrays’ Indices (cont.) You can instruct Excel VBA to change the default index values of all arrays in your module with the instruction Option Base 1 One dimensional arrays will begin at 1 Two-dimensional arrays will begin at 1,1 Option Base 1 Dim data(10) As Double, results(12) As Double If you want to keep the default initial index as 0 but would like a specific array to start with a different index, you can specify the starting index value in the array declaration Dim data(1 To 10) As Long, results(12) As Double Dim matrix(1 To 20, 1 To 10) As Double

Arrays’ Indices (cont.) In the last case, just be aware of the size of your array Dim results(2 to 13) As Double size = upper index bound – lower index bound + 1 Whichever initial index value is chosen, it should be coordinated with the counter variable used in For…Next loops For i = 1 To 13 results(i) = value Next i The results array is still of size12, it is just indexed starting with 2 Here, results(1) would cause an error since there is no such index for the array.

Arrays’ Indices (cont.) LBound Function Returns a Long containing the smallest available subscript for the indicated dimension of an array Syntax  LBound(arrayname [, dimension]) UBound Function Returns a Long containing the largest available subscript for the indicated dimension of an array Syntax  UBound(arrayname [, dimension]) The results array is still of size12, it is just indexed starting with 2 Here, results(1) would cause an error since there is no such index for the array. Dim A(1 To 100, 0 To 3) as Integer LBound(A, 1) UBound(A, 1) LBound(A, 2) UBound(A, 2)

Populating Arrays with Values To assign a value of a specific element of the array, we use the index of the element For example, to assign a value of 10.5 to the third member of array data(9), we would type data(2) = 10.5 To set multiple values of an array, it is more practical to use a For…Next loop with a counter variable For example, to set each element in the array data(9) equal to its index number, we would type For i = LBound(data) To UBound(data) data(i) = i Next i The default initial index value is 0; however, to keep in line with our example, let us assume the initial index is 1 (we discuss indices in a later section).

Populating Arrays with Values (cont.) To assign values to the elements of a multi-dimensional array or to search for a value, use nested For…Next loops with different counter variables For example, to set the value of each element in the two-dimensional array data(4, 9) equal to the product of its indices, you would do the following: For i = 1 to 5 For j = 1 to 10 data(i, j) = i*j Next j Next i 1 2 3 4 5 6 7 8 9 10 12 14 16 18 15 21 24 27 20 28 32 36

Dynamic Arrays If you are not sure about the size of an array at declaration time, you can use a dynamic array declaration For example, sometimes we expect the user to tell our application how many elements the array should have When declaring a dynamic array, the array size is not specified and the parentheses are left empty Dim input() As Double However, before this array or any of its elements is used, we must (eventually) know its size and declare it accordingly (Arrays whose size is known are called fixed-size arrays.)

Dynamic Arrays (cont.) To set the size of a dynamic array at some later point in the code, we use the ReDim statement The ReDim statement can also be used to set or change the number of dimensions and the indexing bounds Suppose we want to ask the user to provide input values, which we will store in our array input

Dynamic Arrays (cont.) If you want to change the size of a dynamic array but do not want to reset its values, then use the statement ReDim Preserve Suppose we have a dynamic array of size 9 which has already been populated with values and we need to add one more value To keep the current values in the array but add one more element, we type ReDim Preserve input(10) input(10) = InputBox(“Please enter new value.”)

Summary To call another sub procedure, use the command Call followed by the sub procedure name Function procedures are similar to sub procedures and follow this basic structure: Function FunctionName() …. End Function

Summary (cont.) To pass a variable in Excel VBA, you should insert the variable as an argument/parameter of the function when it is called A sub procedure, like a variable, can also be defined as Public or Private

Summary (cont.) Arrays store series of data that we can manipulate or refer to later To define an array, use the Dim, Private, or Public variable declarations For a one-dimensional array, we just need a single number to specify the size To define multi-dimensional arrays, we must specify the size of each dimension, separated by a comma The default initial index value of arrays in VBA is 0 To change the initial index value of all arrays in our module to 1, type Option Base 1 at the top of the module

Summary (cont.) To set the size of a dynamic array at some later point in the code, use the ReDim statement The ReDim Preserve statement retains any previously assigned element values Only works with dynamic arrays