1 / 18

Computer Memory

integer. 21. oneGrade. Computer Memory. We can declare, assign and manipulate individual variables with ease…. integer. integer. 12. integer. integer. 23. 21. 21. twoGrade. oneGrade. ThreeGrade. fourGrade. Computer Memory.

afountain
Télécharger la présentation

Computer Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. integer 21 oneGrade Computer Memory We can declare, assign and manipulate individual variables with ease…

  2. integer integer 12 integer integer 23 21 21 twoGrade oneGrade ThreeGrade fourGrade Computer Memory If we have a “list” of variables, we don’t currently have an elegant way to handle this…

  3. 21 12 23 21 Grades integer Computer Memory What we need is a single variable that can handle multiple values (of the same type) This is an Array

  4. Arrays An array is declared using the following syntax: DimArrayName(UpperBound)AsDatatype An array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits. Individual values within an array are selected by using an index. The lowest index of an array is 0, while the upper bound of the array is the highest index in the array. An index must be a Short, an Integer, or a Long data type. The data type is any valid variable like a Short, an Integer, or a String.

  5. Dim shtGrades(3) AsShort Computer Memory

  6. shtGrades(0) = 21 We can assign an individual variable by using its name, and index. Index and subscript are synonymous Computer Memory

  7. shtGrades(0) = 21 shtGrades(1) = 12 shtGrades(2) = 23 shtGrades(3) = 21 Computer Memory

  8. shtGrades(4) = 21 It is our responsibility to make sure that we are always addressing a legal location in the array. shtGrades(-1) = 1 would also crash our program. Computer Memory

  9. Dim strSimpsonNames(4) As String strSimpsonNames(0) = “Homer" strSimpsonNames(1) = “Marge" strSimpsonNames(2) = “Bart" strSimpsonNames(3) = “Lisa" strSimpsonNames(4) = “Maggie" Arrays can contain any variable type

  10. Arrays can contain any variable type Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False

  11. We can use array variables just like regular variables bntRedDemo.Text = strSimpsonNames(0) & “ Simpson” If(strSimpsonNames(3) = “Lisa”) Then bntRedDemo.Text = “Oldest of the Simpson girls” End If But the real power of arrays comes from using them with loops

  12. Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To 4 If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex

  13. Dim blnSimpsonSexIsMale()As Boolean = {True, False, True, False, False} Note that there is no upper bound used. This value is inferred by counting the number of items in the list It is often convenient to initialize an array in the same line of code as you declare the array. You can initialize an array by setting it equal to the values you wish to initialize the array to enclosed within curly braces and separated by commas.

  14. Dim blnSimpsonSexIsMale()As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To Ubound(blnSimpsonSexIsMale) If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex It is convenient to use the UBound function to determine the upper bound of an array. By passing an array to UBound, the upper bound of the array is returned.

  15. Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) bntRedDemo.Text = strSimpsonNames(intLoopCount) & " Simpson" Next intLoopCount Note: Without a call to the Sleep function, this happens so fast all we see is “Maggie Simpson” ….

  16. Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) If blnSimpsonSexIsMale(intLoopCount) Then bntRedDemo.Text = "Male : " & strSimpsonNames(intLoopCount) & " Simpson" Else bntRedDemo.Text = "Female : " & strSimpsonNames(intLoopCount) & " Simpson" End If Next intLoopCount ….

  17. Useful Exercises Can you extend the code above to count the number of males in the Simpson family? Can you add a new Array variable to hold the Simpson's ages, then find the average age of a Simpson family member? Can you produce this output… Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….

  18. Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….

More Related