SlideShare a Scribd company logo
LECTURE 6

2D Arrays
Algorithms
SUMMARY
Quick recap of 1D arrays
2D Arrays
 Declaring and re-declaring
 Assigning values
 Outputting values

Algorithms
 Mapping out the tasks needed for a program
REVIEW OF 1D ARRAYS

Const NSTOCKS as Integer = 2
ReDim meanRet(NSTOCKS) as Double
Const TIMEPERIODS as Integer = 4
Dim stockReturns(TIMEPERIODS) as Double
**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

stockReturns(2) = ? _

**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

stockReturns(2) = 0.0024

**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

stockReturns(4) = ? _

**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

stockReturns(4) = 0.0052

**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

meanRet (1) = ? _

**Assume we’re using Option Base 1**
REVIEW OF 1D ARRAYS

meanRet (1) = 0.00371

**Assume we’re using Option Base 1**
2D ARRAYS
ARRAYS
Hold a range (or set) of values
Basically a set of variables
Your arrays will mainly be 1 or 2 dimensions

arrayName(5)

arrayName(5)

arrayName(5, 4)

arrayName is the name of your array
ARRAYS
Hold a range (or set)about 2D Arrays
of values
Today is
Basically a set of variables
Your arrays will mainly be 1 or 2 dimensions

arrayName(5)

arrayName(5)

arrayName(5, 4)

arrayName is the name of your array
DECLARING ARRAYS
The same rules apply to 2D arrays as to 1D arrays
 Disposable, local or global? – Declare in the right place
 Must specify a data type

Public Sub DescriptiveStats()
Dim stockPrices(5, 2) As Double
Dim stockRet(4, 2) As Double
End Sub

These are
disposable
DECLARING ARRAYS
The same rules apply to 2D arrays as to 1D arrays
 Disposable, local or global? – Declare in the right place
 Must specify a data type

Also, need to specify the size of the array
 The “size” is the number of elements in the array

Public Sub DescriptiveStats()
Dim stockPrices(5, 2) As Double
Dim stockRet(4, 2) As Double
End Sub

5 rows
2 columns
4 rows
2 columns
DECLARING ARRAYS returns
5 stock prices
4 stock
2 rules
2 variables
The same stocks apply to arrays as to stocks
 Disposable, local or global? – Declare in the right place
 Must specify a data type

Also, need to specify the size of the array
 The “size” is the number of elements in the array

Public Sub DescriptiveStats()
Dim stockPrices(5, 2) As Double
Dim stockRet(4, 2) As Double
End Sub

5 prices
2 stocks
4 returns
2 stocks
DECLARING ARRAYS (1D

AND

2D)

Many times we don’t know the number of
elements that will be in the array.
So, first declare them empty, like this:
Dim stockPrices() As Double
Dim stockRet() As Double

For example,
these are local

Public Sub DescriptiveStats()
End Sub

General format
for declaration

arrayName() As DataType
arrayName:= the name of the array
DataType:= Integer, Double, String…
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure
Dim stockPrices() As Double, stockRet() As Double
Dim nDays As Integer, nStocks As Integer

Arrays will most likely need
to be local or global
These are local
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure
Dim stockPrices() As Double, stockRet() As Double
Dim nDays As Integer, nStocks As Integer
Public Sub DescriptiveStats()
nDays = ComboBox1.Value
nStocks = ComboBox2.Value

End Sub

User selects number
of days from a
ComboBox on the
userform
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure
Dim stockPrices() As Double, stockRet() As Double
Dim nDays As Integer, nStocks As Integer
Public Sub DescriptiveStats()
nDays = ComboBox1.Value
nStocks = ComboBox2.Value

End Sub

User selects number
of stocks from a
ComboBox on the
userform
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure
Dim stockPrices() As Double, stockRet() As Double
Dim nDays As Integer, nStocks As Integer
Public Sub DescriptiveStats()
nDays = ComboBox1.Value
nStocks = ComboBox2.Value

Redim both arrays
using the variables

Redim stockPrices(nDays, nStocks) As Double
Redim stockRet(nDays-1, nStocks) As Double
End Sub
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure
Dim stockPrices() As Double, stockRet() As Double
Dim nDays As Integer, nStocks As Integer
Public Sub DescriptiveStats()
nDays = ComboBox1.Value
Num Rows
Num Cols
nStocks = ComboBox2.Value
Redim stockPrices(nDays, nStocks) As Double
Redim stockRet(nDays-1, nStocks) As Double
End Sub
DECLARING ARRAYS
After the size of the array is know, re-declare it
inside a procedure

General format for
re-declaration

ReDim arrayName(nRows, nCols) As DataType
nRows:= the number of rows in the array
nCols:= the number of columns in the array
SUMMARY OF DECLARING 2D ARRAYS
If the size of the array is known and constant
 Declare the constants for nRows and nCols as global

 Declare the arrays (disposable, local or global) using the
constants as the nRows and nCols

Public Const TIMEPERIODS As Integer = 5
Public Const NSTOCKS As Integer = 2
Dim stockPrices(TIMEPERIODS, NSTOCKS) As Double
Dim stockRet(TIMEPERIODS-1, NSTOCKS) As Double
Remember to use Public instead of Dim if they are global.
SUMMARY OF DECLARING 2D ARRAYS
If the size of the array is unknown or variable
 Declare the arrays empty (disposable, local or global)
SUMMARY OF DECLARING 2D ARRAYS
If the size of the array is unknown or variable
 Declare the arrays empty (disposable, local or global)

 Declare variables for nRows and nCols (disposable, local or
global) and assign them values
SUMMARY OF DECLARING 2D ARRAYS
If the size of the array is unknown or variable
 Declare the arrays empty (disposable, local or global)

 Declare variables for nRows and nCols (disposable, local or
global) and assign them values
 Redim the arrays using the variables
2D ARRAYS
VBA counts elements of 1D and 2D arrays from 0
For example,
 This array is named stockPrices
 It has 5 rows and 2 columns: stockPrices(5, 2)

Count
0
1
2
3
4

0

1

stockPrices(0, 0)
2D ARRAYS
VBA counts elements of 1D and 2D arrays from 0
For example,
 This array is named stockPrices
 It has 5 rows and 2 columns: stockPrices(5, 2)

Count
0
1
2
3
4

0

1

stockPrices(4, 1)
2D ARRAYS
To force VBA to count from 1
 Use Option Base 1 at the top of each module

Count
1
2
3
4
5

1

2

stockPrices(1, 1)
2D ARRAYS
To force VBA to count from 1
 Use Option Base 1 at the top of each module

Count
1
2
3
4
5

1

2

stockPrices(5, 2)
EXERCISE 1. DECLARING 2D ARRAYS
Use Lecture 6 Student Example.xlsm, Module1
Declare an array to store the stock returns shown:
 The array can be declared as disposable, local or global.
 Assume you know in advance that:
 nRows is the constant TIMEPERIODS
 nCols is the constant NSTOCKS
EXERCISE 2. DECLARING 2D ARRAYS
Use Lecture 6 Student Example.xlsm, Module1
Declare an array to store the stock returns shown:
 The array can be declared as disposable, local or global.
 Assume that nRows and nCols are NOT known in advance:
 nRows is the variable nTimePeriods. Assign a value in the procedure
 nCols is the variable nStocks. Assign a value in the procedure
‘FILLING’ ARRAYS
USING NAMED RANGES
FILLING 1D ARRAYS FROM A NAMED RANGE

Range(“StockPrices”).Cells(1, 1)
Range(“StockPrices”).Cells(2, 1)

Range(“StockPrices”).Cells(3, 1)

Range(“StockPrices”).Cells(10,1)
FILLING 1D ARRAYS FROM A NAMED RANGE

For i = 1 to TIMPERIODS
stockPrices(i) = Range(“StockPrices”).Cells(i,1)
Next i
FILLING 2D ARRAYS FROM A NAMED RANGE

Range(“StockPrices”).Cells(1, 1)
Range(“StockPrices”).Cells(2, 1)

Range(“StockPrices”).Cells(3, 1)

Range(“StockPrices”).Cells(10,1)
FILLING 2D ARRAYS FROM A NAMED RANGE

Range(“StockPrices”).Cells(1, 2)
Range(“StockPrices”).Cells(2, 2)

Range(“StockPrices”).Cells(3, 2)

Range(“StockPrices”).Cells(10,2)
FILLING 2D ARRAYS FROM A NAMED RANGE

For k = 1 to NSTOCKS
For i = 1 to TIMPERIODS
stockPrices(i,k) = Range(“StockPrices”).Cells(i,k)
Next i
Next k
OUTPUTTING ARRAYS
USING NAMED RANGES
OUTPUTTING 1D ARRAY TO A NAMED RANGE

Range(“StockReturns”).Cells(1, 1)
Range(“StockReturns”).Cells(2, 1)
Range(“StockReturns”).Cells(3, 1)

Range(“StockReturns”).Cells(9,1)
OUTPUTTING 1D ARRAY TO A NAMED RANGE

For i = 1 to TIMPERIODS - 1
Range(“StockReturns”).Cells(i,1) = stockReturns(i)
Next i
OUTPUTTING 1D ARRAY TO A NAMED RANGE

Range(“StockReturns”).Cells(1, 1)

Range(“StockReturns”).Cells(2, 1)
Range(“StockReturns”).Cells(3, 1)

Range(“StockReturns”).Cells(9,1)
OUTPUTTING 1D ARRAY TO A NAMED RANGE

Range(“StockReturns”).Cells(1, 2)
Range(“StockReturns”).Cells(2, 2)
Range(“StockReturns”).Cells(3, 2)

Range(“StockReturns”).Cells(9,2)
OUTPUTTING 1D ARRAY TO A NAMED RANGE
EXERCISE 3. FILL AND OUTPUT
 Use Lecture 6 Student Example.xlsm, Module1
 Read in the prices from the range “PricesPence” to
an array with the same name
Convert the pence to pounds and assign the values
to an array called PricesPounds()
 Output the values of PricesPounds() to the named
range “PricesPounds”
REMEMBER: Generalise your code!
Ranges have been named.
ALGORITHMS
THINK ABOUT THE PROCESS
 Taking a road trip
 Just get in the car and drive, OR
 Make a plan and print directions (or set the sat nav)?

When you write a program
 Go straight to VBA and try typing some code? NO!
 Make a plan first!

Why is this so difficult?
 The plan is usually written for you (assignments, labs,
etc…). You just have to follow the steps already given.
 When you write a program/software, there are no steps
given – you have to write them.
TASK ANALYSIS

(JOHNSON, 2010)

(A few) questions you should answer first:
 What will the user want to achieve with this software?
 Portfolio optimisation, simulation, risk analysis, etc…?

 What tasks are needed in order to get the end result?
 Select stocks, assign weights, choose capital to invest, etc…?

 What are the steps for each task?
 Will they select stocks from a list, will they enter weights or
investments, navigate with command buttons, etc…?

 What tools are used to complete the steps for each task?
 Includes userform controls as well as programing tools?

 Where does the information for each task come from?
 Are stock prices already in the spread sheet or downloaded from
the internet, what information is entered by the user, etc…?
TASK ANALYSIS (CONT’D) ( J O H N S O N , 2 0 1 0 )
 What is the result/output of each task?
 How will the result of each task be used?
 Count how many stocks are chosen, output chosen stocks on a
separate sheet, etc…?

 What problems may users have in doing these tasks?
 Will they know what order to do things & understand instructions?

 What are common user mistakes?
 Entering decimal numbers instead of integers, trying to move
forward without completing all steps, etc…?

 How will you deal with these errors when they occur?
 What terminology will the user be familiar with?
 Can they understand your lingo?
BREAKING DOWN A COMPLICATED TASK
You cannot write a complicated program without
planning.

Break everything down into simple steps
You will never write a ‘perfect’ program without
first making mistakes
 Trial and error – that’s part of programming
 Just because it doesn’t work the first time, don’t give up
 If something isn’t ‘working’ it’s probably an error on your
part, not VBA.

The hardest part of creating a program is writing
out the process. Usually, the actual code is fairly
straight forward.
EXAMPLE ALGORITHM
EXAMPLE. BREAKING DOWN TASKS
Task 1: Select a time period
Task 2: Enter investments
Task 3: Click ‘Estimate
return’ button
Task 4: Review investment
on next userform.
Task 5: Click ‘Change
investment’ button
Task 6: Click ‘Quit’ button
EXAMPLE. TASK 1 – SELECT TIME
 User selects time from combobox
 Fill combobox BEFORE user sees
 Userform initialise: add items

 When the user selects a time:
 Name the range of prices
corresponding to that time
 Use that range to calculate stats
 I’ll need the means later, so assign those to
an array first
 Then use the array of means to assign
values to the labels.

 Display those stats on the userform
 Format the display to be %
 Enable the textboxes
EXAMPLE. TASK 2 - INVESTMENTS
 The user enters investments in
the textboxes.
EXAMPLE. TASK 3 – CLICK BUTTON
 Check user entries:
 Check for letters, blanks & negative
numbers

 I’ll need to use investments in a
calculation so assign them to an
array.
 Calculate total investment
 Add together all investments

 Calculate the final value
 Need means and investments

 Calculate the return
 (final value/total investment)–1

 Show the next userform
EXAMPLE. TASK 4 – REVIEW RESULTS
 Output should display BEFORE the
user sees the form (initialise sub)
 Display total investment (£)
 Assign the label the value of the total
investment variable
 Format label as £0.00

 Display portfolio return
 Assign the label the value of the portfolio
return variable
 Format label as 0.000%

 Display final investment value (£)
 Assign the label the value of the final
investment variable
 Format label as £0.00
EXAMPLE. TASK 5 – CHANGE INVESTMENT
 User wants to change their
investments:
 Unload the investment summary
userform
EXAMPLE. TASK 6 – QUIT
 User wants to quit the program:
 Ask user if they are sure they want to
quit as this will close the program
 If yes, then unload both userforms
 If no, then do nothing
LEARNING OUTCOMES
You are ready to move on when…
 LO26: You can declare a 2D array with the correct number
of elements, in the correct location within your code and
with the correct data type. In addition, you understand
when to declare an array empty and how to use the ReDim
statement.
 LO27: You can assign values to a 2D array as well as assign
the values of a 2D array to cells, a range or userform
controls.
 LO28: You can list the questions we should ask when
developing a program. You can describe what is meant by
a ‘task’ as part of a program. Lastly, you can identify the
tasks of a program in order to construct an algorithm.
THE END

More Related Content

What's hot

Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
IndicThreads
 
Pemrograman Terstruktur 4
Pemrograman Terstruktur 4Pemrograman Terstruktur 4
Pemrograman Terstruktur 4
Moch Mifthachul M
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
Dieudonne Nahigombeye
 
Stata cheatsheet transformation
Stata cheatsheet transformationStata cheatsheet transformation
Stata cheatsheet transformation
Laura Hughes
 
Stata cheat sheet: data transformation
Stata  cheat sheet: data transformationStata  cheat sheet: data transformation
Stata cheat sheet: data transformation
Tim Essam
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
Dieudonne Nahigombeye
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
ACASH1011
 
Link list
Link listLink list
Link list
Malainine Zaid
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
Knoldus Inc.
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
Meet scala
Meet scalaMeet scala
Meet scala
Wojciech Pituła
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
R short-refcard
R short-refcardR short-refcard
R short-refcardconline
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RRajib Layek
 
R Reference Card for Data Mining
R Reference Card for Data MiningR Reference Card for Data Mining
R Reference Card for Data Mining
Yanchang Zhao
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
Vladimir Kostyukov
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel
 
R reference card
R reference cardR reference card
R reference card
Hesher Shih
 

What's hot (19)

Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Pemrograman Terstruktur 4
Pemrograman Terstruktur 4Pemrograman Terstruktur 4
Pemrograman Terstruktur 4
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Stata cheatsheet transformation
Stata cheatsheet transformationStata cheatsheet transformation
Stata cheatsheet transformation
 
Stata cheat sheet: data transformation
Stata  cheat sheet: data transformationStata  cheat sheet: data transformation
Stata cheat sheet: data transformation
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Link list
Link listLink list
Link list
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
R short-refcard
R short-refcardR short-refcard
R short-refcard
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R Reference Card for Data Mining
R Reference Card for Data MiningR Reference Card for Data Mining
R Reference Card for Data Mining
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
R reference card
R reference cardR reference card
R reference card
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 

Similar to MA3696 Lecture 6

Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programming
Tim Essam
 
Array and functions
Array and functionsArray and functions
Array and functions
Sun Technlogies
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
Muhammad Sikandar Mustafa
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
Wojciech Pituła
 
AMIS - Can collections speed up your PL/SQL?
AMIS - Can collections speed up your PL/SQL?AMIS - Can collections speed up your PL/SQL?
AMIS - Can collections speed up your PL/SQL?
Getting value from IoT, Integration and Data Analytics
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Recursion Lecture in C++
Recursion Lecture in C++Recursion Lecture in C++
Recursion Lecture in C++
Raffi Khatchadourian
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
SatyajeetGaur3
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
MarlonMagtibay2
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
MahendraVusa
 
Arrays
ArraysArrays
Arrays
ViniVini48
 
‏‏Lecture 2.pdf
‏‏Lecture 2.pdf‏‏Lecture 2.pdf
‏‏Lecture 2.pdf
AhmedAbdalla903058
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
ambikavenkatesh2
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
Adam Mukharil Bachtiar
 
Pandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetPandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
 

Similar to MA3696 Lecture 6 (20)

MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programming
 
Array and functions
Array and functionsArray and functions
Array and functions
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
2ds
2ds2ds
2ds
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
AMIS - Can collections speed up your PL/SQL?
AMIS - Can collections speed up your PL/SQL?AMIS - Can collections speed up your PL/SQL?
AMIS - Can collections speed up your PL/SQL?
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Spark_Documentation_Template1
Spark_Documentation_Template1Spark_Documentation_Template1
Spark_Documentation_Template1
 
Recursion Lecture in C++
Recursion Lecture in C++Recursion Lecture in C++
Recursion Lecture in C++
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Arrays
ArraysArrays
Arrays
 
‏‏Lecture 2.pdf
‏‏Lecture 2.pdf‏‏Lecture 2.pdf
‏‏Lecture 2.pdf
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 
Pandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetPandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheet
 

More from Brunel University

Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
Brunel University
 

More from Brunel University (8)

MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
MA3696 Lecture 8
MA3696 Lecture 8MA3696 Lecture 8
MA3696 Lecture 8
 
MA3696 Lecture 7
MA3696 Lecture 7MA3696 Lecture 7
MA3696 Lecture 7
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Ma3696 Lecture 3
Ma3696 Lecture 3Ma3696 Lecture 3
Ma3696 Lecture 3
 
Ma3696 Lecture 2
Ma3696 Lecture 2Ma3696 Lecture 2
Ma3696 Lecture 2
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 

MA3696 Lecture 6

  • 2. SUMMARY Quick recap of 1D arrays 2D Arrays  Declaring and re-declaring  Assigning values  Outputting values Algorithms  Mapping out the tasks needed for a program
  • 3. REVIEW OF 1D ARRAYS Const NSTOCKS as Integer = 2 ReDim meanRet(NSTOCKS) as Double Const TIMEPERIODS as Integer = 4 Dim stockReturns(TIMEPERIODS) as Double **Assume we’re using Option Base 1**
  • 4. REVIEW OF 1D ARRAYS stockReturns(2) = ? _ **Assume we’re using Option Base 1**
  • 5. REVIEW OF 1D ARRAYS stockReturns(2) = 0.0024 **Assume we’re using Option Base 1**
  • 6. REVIEW OF 1D ARRAYS stockReturns(4) = ? _ **Assume we’re using Option Base 1**
  • 7. REVIEW OF 1D ARRAYS stockReturns(4) = 0.0052 **Assume we’re using Option Base 1**
  • 8. REVIEW OF 1D ARRAYS meanRet (1) = ? _ **Assume we’re using Option Base 1**
  • 9. REVIEW OF 1D ARRAYS meanRet (1) = 0.00371 **Assume we’re using Option Base 1**
  • 11. ARRAYS Hold a range (or set) of values Basically a set of variables Your arrays will mainly be 1 or 2 dimensions arrayName(5) arrayName(5) arrayName(5, 4) arrayName is the name of your array
  • 12. ARRAYS Hold a range (or set)about 2D Arrays of values Today is Basically a set of variables Your arrays will mainly be 1 or 2 dimensions arrayName(5) arrayName(5) arrayName(5, 4) arrayName is the name of your array
  • 13. DECLARING ARRAYS The same rules apply to 2D arrays as to 1D arrays  Disposable, local or global? – Declare in the right place  Must specify a data type Public Sub DescriptiveStats() Dim stockPrices(5, 2) As Double Dim stockRet(4, 2) As Double End Sub These are disposable
  • 14. DECLARING ARRAYS The same rules apply to 2D arrays as to 1D arrays  Disposable, local or global? – Declare in the right place  Must specify a data type Also, need to specify the size of the array  The “size” is the number of elements in the array Public Sub DescriptiveStats() Dim stockPrices(5, 2) As Double Dim stockRet(4, 2) As Double End Sub 5 rows 2 columns 4 rows 2 columns
  • 15. DECLARING ARRAYS returns 5 stock prices 4 stock 2 rules 2 variables The same stocks apply to arrays as to stocks  Disposable, local or global? – Declare in the right place  Must specify a data type Also, need to specify the size of the array  The “size” is the number of elements in the array Public Sub DescriptiveStats() Dim stockPrices(5, 2) As Double Dim stockRet(4, 2) As Double End Sub 5 prices 2 stocks 4 returns 2 stocks
  • 16. DECLARING ARRAYS (1D AND 2D) Many times we don’t know the number of elements that will be in the array. So, first declare them empty, like this: Dim stockPrices() As Double Dim stockRet() As Double For example, these are local Public Sub DescriptiveStats() End Sub General format for declaration arrayName() As DataType arrayName:= the name of the array DataType:= Integer, Double, String…
  • 17. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure Dim stockPrices() As Double, stockRet() As Double Dim nDays As Integer, nStocks As Integer Arrays will most likely need to be local or global These are local
  • 18. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure Dim stockPrices() As Double, stockRet() As Double Dim nDays As Integer, nStocks As Integer Public Sub DescriptiveStats() nDays = ComboBox1.Value nStocks = ComboBox2.Value End Sub User selects number of days from a ComboBox on the userform
  • 19. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure Dim stockPrices() As Double, stockRet() As Double Dim nDays As Integer, nStocks As Integer Public Sub DescriptiveStats() nDays = ComboBox1.Value nStocks = ComboBox2.Value End Sub User selects number of stocks from a ComboBox on the userform
  • 20. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure Dim stockPrices() As Double, stockRet() As Double Dim nDays As Integer, nStocks As Integer Public Sub DescriptiveStats() nDays = ComboBox1.Value nStocks = ComboBox2.Value Redim both arrays using the variables Redim stockPrices(nDays, nStocks) As Double Redim stockRet(nDays-1, nStocks) As Double End Sub
  • 21. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure Dim stockPrices() As Double, stockRet() As Double Dim nDays As Integer, nStocks As Integer Public Sub DescriptiveStats() nDays = ComboBox1.Value Num Rows Num Cols nStocks = ComboBox2.Value Redim stockPrices(nDays, nStocks) As Double Redim stockRet(nDays-1, nStocks) As Double End Sub
  • 22. DECLARING ARRAYS After the size of the array is know, re-declare it inside a procedure General format for re-declaration ReDim arrayName(nRows, nCols) As DataType nRows:= the number of rows in the array nCols:= the number of columns in the array
  • 23. SUMMARY OF DECLARING 2D ARRAYS If the size of the array is known and constant  Declare the constants for nRows and nCols as global  Declare the arrays (disposable, local or global) using the constants as the nRows and nCols Public Const TIMEPERIODS As Integer = 5 Public Const NSTOCKS As Integer = 2 Dim stockPrices(TIMEPERIODS, NSTOCKS) As Double Dim stockRet(TIMEPERIODS-1, NSTOCKS) As Double Remember to use Public instead of Dim if they are global.
  • 24. SUMMARY OF DECLARING 2D ARRAYS If the size of the array is unknown or variable  Declare the arrays empty (disposable, local or global)
  • 25. SUMMARY OF DECLARING 2D ARRAYS If the size of the array is unknown or variable  Declare the arrays empty (disposable, local or global)  Declare variables for nRows and nCols (disposable, local or global) and assign them values
  • 26. SUMMARY OF DECLARING 2D ARRAYS If the size of the array is unknown or variable  Declare the arrays empty (disposable, local or global)  Declare variables for nRows and nCols (disposable, local or global) and assign them values  Redim the arrays using the variables
  • 27. 2D ARRAYS VBA counts elements of 1D and 2D arrays from 0 For example,  This array is named stockPrices  It has 5 rows and 2 columns: stockPrices(5, 2) Count 0 1 2 3 4 0 1 stockPrices(0, 0)
  • 28. 2D ARRAYS VBA counts elements of 1D and 2D arrays from 0 For example,  This array is named stockPrices  It has 5 rows and 2 columns: stockPrices(5, 2) Count 0 1 2 3 4 0 1 stockPrices(4, 1)
  • 29. 2D ARRAYS To force VBA to count from 1  Use Option Base 1 at the top of each module Count 1 2 3 4 5 1 2 stockPrices(1, 1)
  • 30. 2D ARRAYS To force VBA to count from 1  Use Option Base 1 at the top of each module Count 1 2 3 4 5 1 2 stockPrices(5, 2)
  • 31. EXERCISE 1. DECLARING 2D ARRAYS Use Lecture 6 Student Example.xlsm, Module1 Declare an array to store the stock returns shown:  The array can be declared as disposable, local or global.  Assume you know in advance that:  nRows is the constant TIMEPERIODS  nCols is the constant NSTOCKS
  • 32. EXERCISE 2. DECLARING 2D ARRAYS Use Lecture 6 Student Example.xlsm, Module1 Declare an array to store the stock returns shown:  The array can be declared as disposable, local or global.  Assume that nRows and nCols are NOT known in advance:  nRows is the variable nTimePeriods. Assign a value in the procedure  nCols is the variable nStocks. Assign a value in the procedure
  • 34. FILLING 1D ARRAYS FROM A NAMED RANGE Range(“StockPrices”).Cells(1, 1) Range(“StockPrices”).Cells(2, 1) Range(“StockPrices”).Cells(3, 1) Range(“StockPrices”).Cells(10,1)
  • 35. FILLING 1D ARRAYS FROM A NAMED RANGE For i = 1 to TIMPERIODS stockPrices(i) = Range(“StockPrices”).Cells(i,1) Next i
  • 36. FILLING 2D ARRAYS FROM A NAMED RANGE Range(“StockPrices”).Cells(1, 1) Range(“StockPrices”).Cells(2, 1) Range(“StockPrices”).Cells(3, 1) Range(“StockPrices”).Cells(10,1)
  • 37. FILLING 2D ARRAYS FROM A NAMED RANGE Range(“StockPrices”).Cells(1, 2) Range(“StockPrices”).Cells(2, 2) Range(“StockPrices”).Cells(3, 2) Range(“StockPrices”).Cells(10,2)
  • 38. FILLING 2D ARRAYS FROM A NAMED RANGE For k = 1 to NSTOCKS For i = 1 to TIMPERIODS stockPrices(i,k) = Range(“StockPrices”).Cells(i,k) Next i Next k
  • 40. OUTPUTTING 1D ARRAY TO A NAMED RANGE Range(“StockReturns”).Cells(1, 1) Range(“StockReturns”).Cells(2, 1) Range(“StockReturns”).Cells(3, 1) Range(“StockReturns”).Cells(9,1)
  • 41. OUTPUTTING 1D ARRAY TO A NAMED RANGE For i = 1 to TIMPERIODS - 1 Range(“StockReturns”).Cells(i,1) = stockReturns(i) Next i
  • 42. OUTPUTTING 1D ARRAY TO A NAMED RANGE Range(“StockReturns”).Cells(1, 1) Range(“StockReturns”).Cells(2, 1) Range(“StockReturns”).Cells(3, 1) Range(“StockReturns”).Cells(9,1)
  • 43. OUTPUTTING 1D ARRAY TO A NAMED RANGE Range(“StockReturns”).Cells(1, 2) Range(“StockReturns”).Cells(2, 2) Range(“StockReturns”).Cells(3, 2) Range(“StockReturns”).Cells(9,2)
  • 44. OUTPUTTING 1D ARRAY TO A NAMED RANGE
  • 45. EXERCISE 3. FILL AND OUTPUT  Use Lecture 6 Student Example.xlsm, Module1  Read in the prices from the range “PricesPence” to an array with the same name Convert the pence to pounds and assign the values to an array called PricesPounds()  Output the values of PricesPounds() to the named range “PricesPounds” REMEMBER: Generalise your code! Ranges have been named.
  • 47. THINK ABOUT THE PROCESS  Taking a road trip  Just get in the car and drive, OR  Make a plan and print directions (or set the sat nav)? When you write a program  Go straight to VBA and try typing some code? NO!  Make a plan first! Why is this so difficult?  The plan is usually written for you (assignments, labs, etc…). You just have to follow the steps already given.  When you write a program/software, there are no steps given – you have to write them.
  • 48. TASK ANALYSIS (JOHNSON, 2010) (A few) questions you should answer first:  What will the user want to achieve with this software?  Portfolio optimisation, simulation, risk analysis, etc…?  What tasks are needed in order to get the end result?  Select stocks, assign weights, choose capital to invest, etc…?  What are the steps for each task?  Will they select stocks from a list, will they enter weights or investments, navigate with command buttons, etc…?  What tools are used to complete the steps for each task?  Includes userform controls as well as programing tools?  Where does the information for each task come from?  Are stock prices already in the spread sheet or downloaded from the internet, what information is entered by the user, etc…?
  • 49. TASK ANALYSIS (CONT’D) ( J O H N S O N , 2 0 1 0 )  What is the result/output of each task?  How will the result of each task be used?  Count how many stocks are chosen, output chosen stocks on a separate sheet, etc…?  What problems may users have in doing these tasks?  Will they know what order to do things & understand instructions?  What are common user mistakes?  Entering decimal numbers instead of integers, trying to move forward without completing all steps, etc…?  How will you deal with these errors when they occur?  What terminology will the user be familiar with?  Can they understand your lingo?
  • 50. BREAKING DOWN A COMPLICATED TASK You cannot write a complicated program without planning. Break everything down into simple steps You will never write a ‘perfect’ program without first making mistakes  Trial and error – that’s part of programming  Just because it doesn’t work the first time, don’t give up  If something isn’t ‘working’ it’s probably an error on your part, not VBA. The hardest part of creating a program is writing out the process. Usually, the actual code is fairly straight forward.
  • 52. EXAMPLE. BREAKING DOWN TASKS Task 1: Select a time period Task 2: Enter investments Task 3: Click ‘Estimate return’ button Task 4: Review investment on next userform. Task 5: Click ‘Change investment’ button Task 6: Click ‘Quit’ button
  • 53. EXAMPLE. TASK 1 – SELECT TIME  User selects time from combobox  Fill combobox BEFORE user sees  Userform initialise: add items  When the user selects a time:  Name the range of prices corresponding to that time  Use that range to calculate stats  I’ll need the means later, so assign those to an array first  Then use the array of means to assign values to the labels.  Display those stats on the userform  Format the display to be %  Enable the textboxes
  • 54. EXAMPLE. TASK 2 - INVESTMENTS  The user enters investments in the textboxes.
  • 55. EXAMPLE. TASK 3 – CLICK BUTTON  Check user entries:  Check for letters, blanks & negative numbers  I’ll need to use investments in a calculation so assign them to an array.  Calculate total investment  Add together all investments  Calculate the final value  Need means and investments  Calculate the return  (final value/total investment)–1  Show the next userform
  • 56. EXAMPLE. TASK 4 – REVIEW RESULTS  Output should display BEFORE the user sees the form (initialise sub)  Display total investment (£)  Assign the label the value of the total investment variable  Format label as £0.00  Display portfolio return  Assign the label the value of the portfolio return variable  Format label as 0.000%  Display final investment value (£)  Assign the label the value of the final investment variable  Format label as £0.00
  • 57. EXAMPLE. TASK 5 – CHANGE INVESTMENT  User wants to change their investments:  Unload the investment summary userform
  • 58. EXAMPLE. TASK 6 – QUIT  User wants to quit the program:  Ask user if they are sure they want to quit as this will close the program  If yes, then unload both userforms  If no, then do nothing
  • 59. LEARNING OUTCOMES You are ready to move on when…  LO26: You can declare a 2D array with the correct number of elements, in the correct location within your code and with the correct data type. In addition, you understand when to declare an array empty and how to use the ReDim statement.  LO27: You can assign values to a 2D array as well as assign the values of a 2D array to cells, a range or userform controls.  LO28: You can list the questions we should ask when developing a program. You can describe what is meant by a ‘task’ as part of a program. Lastly, you can identify the tasks of a program in order to construct an algorithm.