Transcript array
CS105 Lab 10 – Arrays
Announcements
MP 2: Please make sure you have changed the
“Author” field according to your NetIDs
Midterm 2 is next Tuesday, Nov. 3rd, at 7pm
Please READ THE INSTRUCTIONS
Request your conflict NOW
MP 1 grades are now final
CS 105 – Fall 2009
1
Objectives
Learn how to use VBA arrays
CS 105 – Fall 2009
2
Our plan : a Guessing Game
Allow 4 players to make guess each about the
number of moves, and determine the best guess
We need to store the numbers somewhere
We could use worksheet cells… but not every
programming language is connected to a worksheet.
We could use a separate variable for each number… but
what if the list is very large?
We will use an Array.
CS 105 – Fall 2009
3
What is an Array?
What is an array?
an organized list of information.
individual items in an array are called
elements.
Each element in an array is designated by an
index or subscript indicating the elements
position in the array.
CS 105 – Fall 2009
4
Using Arrays
Arrays are defined using the following syntax:
Dim <arrayname>(<start-index> To <end-index>) _
As <type>
For Example: Dim strWords(1 to 6) As String
Array elements are accessed using the
following syntax:
<arrayname>(<index>)
For Example: strWords(3) = "cat"
(conceptually, strWords(3) is a variable)
CS 105 – Fall 2009
5
Entering Numbers
Define a module-level array of integers with size 4.
Dim mintGuess(1 To 4)As Integer
After user clicks the Start button and before the guy
is moving: Create a For-Loop that prompts the user
to enter 4 numbers. Store the numbers in array
elements 1 through 4.
mintGuesses(intCounter) = InputBox("Enter.. ", "")
Call ShowGuesses to show the guesses on the
worksheet.
CS 105 – Fall 2009
6
FindTheBestGuess sub
Fill in the code for FindTheBestGuess sub
The final number of moves is stored in
intMoves
Create a For-Loop that identifies the closest
element of mintGuess array.
Guess A is closer than Guess B if
Abs(A – intMoves) < Abs(B – intMoves)
Built-in function Abs() computes absolute value.
CS 105 – Fall 2009
7
How the algorithm works
intCounter=?
intBestGuesses=?
Before loop
Array
1:
Loop 1st time
2:
Loop 2nd time
3:
Loop 3rd time
4:
Loop 4th time
After loop
CS 105 – Fall 2009
8
Extend the Game : more than 4 players
The code we have created only works for 4
players.
Suppose instead that we want to allow
variable number of users to play each time.
But how?
Make array a variable size array
Ask the number of players before each play
Then, Resize the array
CS 105 – Fall 2009
9
Variable Size Array
Change the array definition to not specify size
information.
Dim mintGuesses() As Integer
When the array size is known, use ReDim to
resize the array appropriately:
ReDim <arrayname>(<start-index> To <end-index>)
For Example: ReDim mintGuesses(1 to 6)
CS 105 – Fall 2009
10
Changing an Array’s Size (continued)
Define a module-level variable of type Integer
to keep track of the array size.
Dim mintSize As Integer
Change all the For-Loops to use mintSize.
Including ShowGuesses sub
Before asking for the guesses, ask for the
number of players
mintSize = InputBox("How many players?")
Resize the array
ReDim mintGuesses(1 To mintSize)
CS 105 – Fall 2009
11
Extend the game to 2 team of players
We want to have 2 teams, each has 4
guesses. A team wins if its best guess is
closer to the answer.
How do we store the guesses?
Add another array?
OK, but what if we want to extend to 3,4,5… teams
We can use a 2 dimensional array
CS 105 – Fall 2009
12
Extend the game to 2 players
Re-define the array.
Dim mintGuesses(1 To 2, 1 To 4) As Integer
mintGuess number 1, by team 1?
What is guess number 2, by team 1?
What is guess number 1, by team 2?
What do these mean?
mintGuesses(1,i) , mintGuesses(2,j)
Can you extend to M teams instead of 2?
CS 105 – Fall 2009
13
What you should know?
Declare and Use one dimensional arrays.
For loop to assign elements of the array
Algorithm and Code to find the closest guess.
Resize variable-length arrays
Declare and Use two dimensional arrays.
Finish (perhaps) your first home-made
computer game
CS 105 – Fall 2009
14
Exercises
Do-Loop-While
Do-Loop-Until
Do-Until-Loop
Sub Test1 (intA As Integer)
Do
intA = intA + 1
Loop While intA < 10
End Sub
Sub Test2 (intA As Integer)
Do
intA = intA + 1
Loop Until intA < 10
End Sub
Sub Test3 (intA As Integer)
Do Until intA >= 10
intA = intA + 1
Loop
End Sub
Exercise
# Times
value of intA?
Exercise
# Times
value of intA?
Exercise
Test1(1)
Test2(10)
Test3(100)
Test2(1)
Test3(10)
Test1(0)
Test3(1)
Test1(100)
Test1(-10)
Test1(10)
Test2(100)
Test2(9)
15
# Times
value of intA?