MODULE 12
ONE DIMENSIONAL ARRAYS
Welcome to Module 12!
• In this unit, we will introduce the concept of arrays.
Throughout this course, it was understood that variables can
store one (and only one) value.
• In this unit, we discuss that there is one structure that CAN
hold more than one value: an array.
• Arrays are great tools for storing a group of similar (or
dissimilar, in some cases) items of data.
• We will discuss how arrays are created, how items are stored
in arrays, and how you can search and retrieve items from
them.
WHAT IS AN
ARRAY?
• Allows
programmers to
represent many
values with one
variable name
• Elements in the
array are
distinguished by
their subscript
THE DIM
STATEMENT
FOR ARRAYS
Eggs as elements
in an Array
• Arrays are items that can store
a collection of related, or
similar, values that can be
referenced individually by their
indices. One way to picture an
array is to think of a container
of similar objects that only has
a certain number of slots. The
egg carton itself is the
container, and each egg is a
variable. You can reference
each egg by its index: egg 1,
egg 2, and so on. This allows
you to store similar elements
together.
Image source: http://flylib.com/books/en/4.226.1.41/1/
Eggs as elements
in an Array
• Arrays in Visual Basic are
zero-based, meaning that the
first item in an array is stored
at index 0. Thus, the last
item's index array is always
one minus the number of
items in the array. All
variables in an array must be
of the same data type—you
can't mix and match
• So we need to change our
image a little
Image source: http://flylib.com/books/en/4.226.1.41/1/
Eggs as elements
in an Array
• When you set up an array with
the Dim word, you put the name
of your array variable, and tell
Visual Basic how many items
you want to store in the array.
• But you need to use
parentheses around your
number. You then assign your
data to a position in the array. In
our Egg example:
• Dim myEggCarton(11) As Integer
Image source: http://flylib.com/books/en/4.226.1.41/1/
ARRAYS
• Take a look at the picture below:
• In order to create an array, we use the following
syntax:
• Dim strNames(4) As String
ARRAYS
• We could then populate our array with names as
follows:
• Dim strNames(5) As String
• We could then populate our array with names as
follows:
ARRAYS
• An easier way would be to use For…Next loops to
populate the array.
• You set up the loop with a counter.
• The loop goes from 0 to 5 (6 total iterations) and each
time an input box is generated, allowing the user to
input a name.
• This can be accomplished in the following manner:
SEARCH ARRAYS
• Always use a For…Next Loop
• Example:
For intCounter = 0 to intNumbers.Length-1
If intNumber(intCounter)= 5 Then
MessageBox.Show(“The number has been found”)
Next
• What does this do – first it looks at intNumber(0) and sees if it is 5, then it
looks at intNumber(1), etc .
• Whenever it “sees” a 5 it pops a MessageBox
DYNAMIC ARRAYS
• Dynamic Array- Can differ in size at run time
• Dim intNum(-1) - (-1) means that the array is dynamic.
• ReDim –Used to change the size of an array
• Preserve – preserves elements so they are not cleared.
EXAMPLE
• Dim intTestScores(-1) As Integer
• Dim intNumberOfScores As Integer
• intNumberOfScores=inputbox(“How many test scores would
you like to enter?”)
• Redim intTestScores(intNumberOfScores) As Integer
For intCounter 0 to intTestScores.Length-1
IntTestScores (intCounter)=InputBox(“Enter Score Here”)
Next
-1 indicates a dynamic array
Redim changes the size of the array
ERROR ALERT
WHY IS MY DYNAMIC
ARRAY ALL ZEROS?
A Couple of Errors in the Course
• In slide 5 of the tutorial you add this code:
• In slide 6 of the tutorial you are SUPPOSED to modify ( or
replace) that code with this code:
• If you didn’t preserve, then you are displaying the cleared
array – and you will get those zeros.
A Couple of Errors in the Course
• Dynamic Tutorial - The video is fine with this one, but in
the steps - it tells you to double click on btnSearch and it
should be btnDynamic
• Slide 7 on Tutorial 4 has the wrong image – should be:
• The above image is in the instructions so you can
reference it

Mod 12

  • 1.
  • 2.
    Welcome to Module12! • In this unit, we will introduce the concept of arrays. Throughout this course, it was understood that variables can store one (and only one) value. • In this unit, we discuss that there is one structure that CAN hold more than one value: an array. • Arrays are great tools for storing a group of similar (or dissimilar, in some cases) items of data. • We will discuss how arrays are created, how items are stored in arrays, and how you can search and retrieve items from them.
  • 3.
    WHAT IS AN ARRAY? •Allows programmers to represent many values with one variable name • Elements in the array are distinguished by their subscript
  • 4.
  • 5.
    Eggs as elements inan Array • Arrays are items that can store a collection of related, or similar, values that can be referenced individually by their indices. One way to picture an array is to think of a container of similar objects that only has a certain number of slots. The egg carton itself is the container, and each egg is a variable. You can reference each egg by its index: egg 1, egg 2, and so on. This allows you to store similar elements together. Image source: http://flylib.com/books/en/4.226.1.41/1/
  • 6.
    Eggs as elements inan Array • Arrays in Visual Basic are zero-based, meaning that the first item in an array is stored at index 0. Thus, the last item's index array is always one minus the number of items in the array. All variables in an array must be of the same data type—you can't mix and match • So we need to change our image a little Image source: http://flylib.com/books/en/4.226.1.41/1/
  • 7.
    Eggs as elements inan Array • When you set up an array with the Dim word, you put the name of your array variable, and tell Visual Basic how many items you want to store in the array. • But you need to use parentheses around your number. You then assign your data to a position in the array. In our Egg example: • Dim myEggCarton(11) As Integer Image source: http://flylib.com/books/en/4.226.1.41/1/
  • 8.
    ARRAYS • Take alook at the picture below: • In order to create an array, we use the following syntax: • Dim strNames(4) As String
  • 9.
    ARRAYS • We couldthen populate our array with names as follows: • Dim strNames(5) As String • We could then populate our array with names as follows:
  • 10.
    ARRAYS • An easierway would be to use For…Next loops to populate the array. • You set up the loop with a counter. • The loop goes from 0 to 5 (6 total iterations) and each time an input box is generated, allowing the user to input a name. • This can be accomplished in the following manner:
  • 11.
    SEARCH ARRAYS • Alwaysuse a For…Next Loop • Example: For intCounter = 0 to intNumbers.Length-1 If intNumber(intCounter)= 5 Then MessageBox.Show(“The number has been found”) Next • What does this do – first it looks at intNumber(0) and sees if it is 5, then it looks at intNumber(1), etc . • Whenever it “sees” a 5 it pops a MessageBox
  • 12.
    DYNAMIC ARRAYS • DynamicArray- Can differ in size at run time • Dim intNum(-1) - (-1) means that the array is dynamic. • ReDim –Used to change the size of an array • Preserve – preserves elements so they are not cleared.
  • 13.
    EXAMPLE • Dim intTestScores(-1)As Integer • Dim intNumberOfScores As Integer • intNumberOfScores=inputbox(“How many test scores would you like to enter?”) • Redim intTestScores(intNumberOfScores) As Integer For intCounter 0 to intTestScores.Length-1 IntTestScores (intCounter)=InputBox(“Enter Score Here”) Next -1 indicates a dynamic array Redim changes the size of the array
  • 14.
    ERROR ALERT WHY ISMY DYNAMIC ARRAY ALL ZEROS?
  • 15.
    A Couple ofErrors in the Course • In slide 5 of the tutorial you add this code: • In slide 6 of the tutorial you are SUPPOSED to modify ( or replace) that code with this code: • If you didn’t preserve, then you are displaying the cleared array – and you will get those zeros.
  • 16.
    A Couple ofErrors in the Course • Dynamic Tutorial - The video is fine with this one, but in the steps - it tells you to double click on btnSearch and it should be btnDynamic • Slide 7 on Tutorial 4 has the wrong image – should be: • The above image is in the instructions so you can reference it