Module 13 One_dimensional Arrays PPT Transcript

advertisement
Module 13.1.1 One Dimensional Arrays PowerPoint Transcript
Slide 1:
In Module 13, you will focus on single-dimensional arrays. The key points to focus on are how to create,
declare and populate an array. Take notes as you go along to aid you when working through the various
lessons and practice assignments
Slide 2:
Our Essential Standard for this Module is Objective 7.00 Apply Advanced Logic, Indicator 7.02 Apply
One-Dimensional Arrays.
Slide 3:
What is an Array? An array is a container that hold more than one value. Each value is called an
element. Each element of the array must be the same data type. A reminder…data types can be
integers or strings.
Slide 4:
Picture a carton of eggs. The carton is the container for the eggs. Each egg is an element in the
container.
Slide 5:
An array like all variables must be declared before it can be used. Use “Dim arrayName(intLastIndex) AS
DataType” as the code. For example: “Dim intArray(5) as Integer. {0, 1, 2, 3, 4, 5} The first element is
always located at the index position of zero. The index position points to the location of a value in an
array. In Visual Basic, this array will have six elements.
Slide 6:
Using our egg/egg carton analogy, each position would be numbered, starting at 0.
Slide 7:
View the arrays video….It is below this transcript on the lesson page.
Slide 8:
In the previous slide we declared an array but did not give the elements any values. Since the data type
was integer all elements have a value of zero. Unless given values, the array will be initialized as follow:
Numeric Values initialized as 0; String Value initialized as nothing.
Slide 9:
However, we can modify our statement:
Dim intArray() As Integer = {1, 2, 3, 4, 5}
intArray with values is 1, 2, 3, 4, 5
Index positions are 0, 1, 2, 3, 4.
This declaration has an initializer list that will set all of the values of the array. Notice there is no integer
within the ( )’s.
Slide 10:
We can also add values individually like this:
intArray(0) = 1
intArray(1) = 2
We can also use a loop if we are putting successive values in the array. Here is the code:
For i as Integer = 0 to 5
intArray(i)=InputBox(“Enter a Value”, “Fill Array”)
Next i.
Slide 11:
You can also populate an Array using a loop. Code:
Dim intArray(4) As Integer
Dim I As Integer = 2
Dim j As Integer = 0.
Then code:
Do While i <= 10; intArray(j) = I
i += 2 ‘increases I
j +=1 ‘increases index j
Loop
This will give the five elements of the array an value. Starting with 2 and ending with 10 counting by 2s.
Slide 12:
Now your array has data in it! How do you display it to the user? Use a loop of course! Length is a
propeprty that will give the length (number of elements) of any declared array. Note the Length will be
1 more than the index number of the last element in the array. Here is the code:
intArray(4) has a Length of 5.
Slide 13:
Consider the following example of code to pull data from an array:
Do While x < intArray.Length
MessageBox.Show(intArray(x))
x+=1
Loop
You can also use a For Loop.
Example:
for I As Integer = 0 To ArrayName.Length-1.
Slide 14:
Let’s consider potential problems with Arrays. You will get a runtime error if you try to assign a value or
pull a value from an array using an incorrect runtime error. Example:
Dim strName (3) As String
strName (4)=“Jane”
There is an error because there is not a 4th index position.
Slide 15:
But Wait….There is an Easier Way. There is a special type of loop just for arrays! Use For Each. This
special loop’s main use is pulling data from arrays. This is the proper syntax for For Each:
For Each var AS Data Type In ArrayName
‘Statements
Next var
The following is a VB coded example:
For Each intNum As Integer In IntArray
MessageBox.Show(intNum) ’shows the value of intArray(intNum)
Next i.
Slide 16:
Now let’s consider how to sort data in an array. The syntax is:
Array.Sort(NameofArray)
example:
Array.Sort(intArray)
This will sort the array from smallest to largest. Hint: A string array would be sorted A-Z.
Slide 17:
To sort a string array code:
Dim strArray() As String={“apple”, “orange”, “banana”, “pineapple”, “pear”}.
Then code:
Array.Sort(strArray); For Each x In strArray
1stFruit.Items.Add(x)
Next x.
Slide 18:
We can flip the array (last element becomes the first) using the Reverse method. Code:
Array.Reverse(strArray).
Slide 19:
You can search an array by using a loop with an IF statement. In many cases, it is more efficient to
search an array if it is sorted first. Syntax for searching an array:
For Each variable As DataType In ArrayName
If variable = searchForVar Then
Statements(Whatever should happen)
End If
Next Variable
Slide 20:
You can use multiple arrays where their elements are related by their position in the array.
Slide 21:
If you find you need a larger array, you will need to re-declare the array as arrays are not dynamic. In
other words, once you set the size of an array, you cannot change it. Use the ReDim statement to redeclare the array. Syntax: ReDim arrayName(newSize). Re-declaring an array this way will delete all
existing values in the array.
Slide 22:
To re-declare the array AND hold the values, add the Preserve keyword. Syntax: ReDim Preserve
arrayName(newSize).
Slide 23:
View the program on the next slide that accepts three numbers and shows them to the user, smallest to
largest. It uses textboxes for input and output.
Slide 24:
Look over this sample VB code. You may want to use it later.
Slide 25:
View the program on the next slide that takes in grades from a user. The user will tell the program how
many grades to enter by using a textbox. The grades will display in a listbox sorted smallest to largest.
The average of the grades will display in a label.
Slide 26:
Slide 27:
To code smallest to largest use Array.Sort(intArray).
To pull data from an array use: For Each grade In intArray; 1st Grades.Items.Add(grade); Next grade.
To average use: decAverage=intGradeTotal/intGradeNum.
To display with 1 decimal place use: lblAverage.Text=decAverage.ToString(“##.0”).
Slide 28:
You can pass an array to a sub either ByVal or ByRef.
Syntax for the Sub header: Private Sub addNums (ByVal NumArr() As Integer)…Note: No numbers
between the parentheses ().
Syntax for Call statement: addNums(NumArr)…Note No empty ()’s.
Slide 29:
You can also pass one element from an array.
Syntax for the Sub header: Private Sub addNums(ByVal intNum as Integer)…Note: It is only passed a
single data value.
Syntax for Call Statement: addNums(NumArr(i))…Note: passes the single value.
Slide 30:
Be sure you know these vocabulary words: Array, Element, Index Position. Length, ReDim; Preserve,
Dynamic Array
Slide 31:
Print out a copy of these codes and keep handy while working on your projects.
Download