Chapter 7 - VB.Net by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Control Arrays Skip Structures (7.3), 7.4 and.

Slides:



Advertisements
Similar presentations
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Advertisements

Chapter 71 Array Properties Upper Bound: The value of arrayName.GetUpperBound(0) is the upper bound of arrayName(). Then What is the Lower Bound ? Ans:
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
Chapter 7 - Visual Basic Schneider
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
A DMINISTRATIVE Midterm 1. A DMINISTRATIVE Assignment #3 Console Application Writing to the console Console.WriteLine(“Writing to the console”) UserInput.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays 7.5 A Case Study:
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
Chapter 7 – Arrays 7.1 Creating and Accessing Arrays
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 7 - VB 2008 by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Some Additional Types of Arrays 7.4 Sorting.
1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Some Additional Types of Arrays 7.4 Sorting and Searching 7.5 Two-Dimensional.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Chapter 7 - VB.Net by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Some Additional Types of Arrays 7.4 Sorting.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 7 - VB 2005 by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.1 (a) Arrays and Loops 7.2 Using Arrays 7.3 Some Additional Types.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Visual Basic 2010 How to Program
Chapter 7 Arrays.
ARRAYS.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 9 – Additional Controls and Objects
The List Box Control Items can be placed into the list at design time or run time The Sorted property causes items in the list to be sorted automatically.
Chapter 6 - VB.Net by Schneider
Chapter 7 - VB.Net by Schneider
Introduction to Computer Programming IT-104
Presentation transcript:

Chapter 7 - VB.Net by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Control Arrays Skip Structures (7.3), 7.4 and 7.5

Chapter 7 - VB.Net by Schneider2 7.1 Creating and Accessing Arrays Array Creation ReDim Statement Using an Array as a Frequency Table

Chapter 7 - VB.Net by Schneider3 Simple variables A variable (or simple variable) is a name to which VB.NET can assign a single value. An array variable is a collection of simple variables of the same type to which VB.NET can efficiently assign a list of values.

Chapter 7 - VB.Net by Schneider4 Elements of an Array Array Name: A valid variable name for the structure. Subscript or Index : A value that refers to a particular array element. Element: An individual data item within an array.

Chapter 7 - VB.Net by Schneider5 Example Suppose that you want to evaluate the exam grades for 30 students and to display the names of the students whose scores are above average. Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim student1 As String, score1 As Double Dim student2 As String, score2 As Double Dim student3 As String, score3 As Double

Chapter 7 - VB.Net by Schneider6 Using Arrays Dim student(30) As String Dim score(30) As Double Array name Upper bound of subscripts in the array Data type

Chapter 7 - VB.Net by Schneider7 Putting Values into an Array student(1) = "Tom Brown" subscript Read: "student sub one equals Tom Brown" Which means that the string "Tom Brown" is being stored at the second location in the array called student… because all arrays begin counting at 0.

Chapter 7 - VB.Net by Schneider8 Array Terminology Dim arrayName(n) As DataType 0 is the "lower bound" of the array n is the "upper bound" of the array – the last available subscript in this array The number of elements is the size of the array

Chapter 7 - VB.Net by Schneider9 Variable Arrays Can have one or two dimensions A one-dimensional array is simply a row (or column) of variables Each element in an array is identified by a subscript You refer to an array element by the array’s name followed by the element’s subscript

Chapter 7 - VB.Net by Schneider10 One-dimensional Array NebraskaNew JerseyNew Mexico Tennessee Texas Nebraska New Jersey New Mexico Tennessee Texas Demo Provinces

Chapter 7 - VB.Net by Schneider11 Example 1 Private Sub btnWhoWon_Click(...) _ Handles btnWhoWon.Click Dim teamName(4) As String Dim n As Integer 'Place Super Bowl Winners into the array teamName(1) = "Packers" teamName(2) = "Packers" teamName(3) = "Jets" teamName(4) = "Chiefs" 'Access array n = CInt(txtNumber.Text) txtWinner.Text = teamName(n) End Sub

Chapter 7 - VB.Net by Schneider12 Output Example 1

Chapter 7 - VB.Net by Schneider13 Example 2 Dim teamName(3) As String Private Sub btnWhoWon_Click(...)Handles btnWhoWon.Click Dim n As Integer n = CInt(txtNumber.Text) txtWinner.Text = teamName(n) End Sub Private Sub Form1_Load(...) Handles MyBase.Load 'Place Super Bowl Winners into the array teamName(1) = "Packers" teamName(2) = "Packers" teamName(3) = "Jets" End Sub

Chapter 7 - VB.Net by Schneider14 Initializing Arrays Arrays may be initialized when they are created: Dim arrayName() As varType = {value0, _ value1, value2,..., valueN} declares an array having upper bound N and assigns value0 to arrayName(0), value1 to arrayName(1), value2 to arrayName(2),..., and valueN to arrayName(N). Dim teamName() As String = {"", "Packers", _ "Packers", "Jets", "Chiefs"}

Chapter 7 - VB.Net by Schneider15 Initializing an Array by Reading from a File Dim student (30) As String, count as integer Dim sr as IO.streamreader = IO.File.OpenText … Open “STUDENTS.TXT” For Input As #1 For count = 1 To 30 student ( count ) = sr.readline Next sr.close Demo Provinces2

Chapter 7 - VB.Net by Schneider16 Parallel Arrays Two arrays are referred to as parallel if subscripted variables having the same subscript (elements) are related. i.e. student(30) and grade(30)

Chapter 7 - VB.Net by Schneider17 The Average Program Revisited! For count = 1 To 30 student (count) = sr.readline grade (count) = Cdbl(sr.readline) Next sum = 0 For count = 1 To 30 sum += grade(count) Next average = sum/30

Chapter 7 - VB.Net by Schneider18 Unknown Array Size What do I do if I don’t know the size of a file – and thus the size of the array??

Chapter 7 - VB.Net by Schneider19 First Technique – Unknown Size Dim Array with no upper bound Use a loop and counter to count the number of items in the file ReDim the array based on the counter Use a second loop to then read the known file size

Chapter 7 - VB.Net by Schneider20 Second Technique – Unknown Size Dim Array with a large upper bound Use a loop and sr.peek to see if additional data exists in the file Use ReDim Preserve to increase the upper bound of the array by one Read one piece of data into the new array element just created

Chapter 7 - VB.Net by Schneider21 Third Technique – Unknown Size Dim an Array with a large upper bound (large enough to easily hold all data) Read from the file into the array using a loop with a counter Keep track of the number of pieces of data in the array with the counter So in effect, you only use part of a large array

Chapter 7 - VB.Net by Schneider22 ReDim Statement The size of an array may be changed after it is created: ReDim arrayName(m) where arrayName is the name of the already declared array and m is an Integer literal,variable, or expression.

Chapter 7 - VB.Net by Schneider23 Preserve To keep any data that has already been stored in the array when resizing it, use ReDim Preserve arrayName(m) Demo Provinces3

Chapter 7 - VB.Net by Schneider24 Using an Array as a Frequency Table Private Sub btnAnalyze_Click(...) Handles btnAnalyze.Click 'Count occurrences of the various letters in a sentence Dim index, letterNum As Integer Dim sentence, letter As String Dim charCount(26) As Integer 'Examine and tally each letter of the sentence sentence = (txtSentence.Text).ToUpper For letterNum = 1 To sentence.Length letter = sentence.Substring(letterNum - 1, 1) If (letter >= "A") And (letter <= "Z") Then index = Asc(letter) - 64 'The ANSI value of "A" is 65 charCount(index) += 1 End If Next

Chapter 7 - VB.Net by Schneider25 Example 4 continued 'List the tally for each letter of alphabet lstCount.Items.Clear() For index = 1 To 26 letter = Chr(index + 64) If charCount(index) > 0 Then lstCount.Items.Add(letter & " " & _ charCount(index)) End If Next End Sub

Chapter 7 - VB.Net by Schneider26 Example 4 Output

Chapter 7 - VB.Net by Schneider27 Out of Bounds Error The following sets up an array, and then references an element that doesn't exist. This will cause an error. Dim trees() As String = {"", "Sequoia", _ "Redwood", "Spruce"} txtBox.Text = trees(5)

Chapter 7 - VB.Net by Schneider28 Copying Arrays If arrayOne() and arrayTwo() have been declared with the same data type, then the statement arrayOne = arrayTwo makes arrayOne() an exact duplicate of arrayTwo(). It will have the same size and contain the same information.

Chapter 7 - VB.Net by Schneider29 Exercises p. 312 In-class P. 319, #36

Chapter 7 - VB.Net by Schneider Using Arrays Ordered Arrays Using Part of an Array Merging Two Ordered Arrays Passing Arrays to Procedures

Chapter 7 - VB.Net by Schneider31 Ordered Arrays An array is ordered if the elements are in ascending or descending order. Why would we want to order an array??

Chapter 7 - VB.Net by Schneider32 Ordered Arrays - Example 1 Dim nom() As String = {"", "AL", "BOB", "CARL", "DON","ERIC", _ "FRED", "GREG", "HERB", "IRA", "JACK"} Private Sub btnSearch_Click(...) Handles btnSearch.Click Dim name, name2Find As String Dim n As Integer 'Subscript of the array name2Find = txtName.Text.ToUpper Do n += 1 'Add 1 to n Loop Until (nom(n) >= name2Find) Or (n = 10) If nom(n) = name2Find Then txtResult.Text = "Found." Else txtResult.Text = "Not found." End If End Sub

Chapter 7 - VB.Net by Schneider33 Ordered Arrays Answer – It makes searching them much more efficient On average, you only have to search half an array that is ordered whereas you have to search the whole array if unordered

Chapter 7 - VB.Net by Schneider34 Merging Two Ordered Arrays To consolidate two lists into a single ordered list: 1. Compare the two names (numbers) at the top of the first and second lists. a)If one name (number) precedes the other, copy it onto the third list and cross it off its original list. b)If the names (numbers) are the same, copy the name (number) onto the third list and cross out the name (number) from the first and second lists. 2.Repeat Step 1 with the current top names (numbers) until you reach the end of either list. 3.Copy the names from the remaining list onto the third list. Demo modified

Chapter 7 - VB.Net by Schneider35 Passing Arrays to Procedures An array declared in a procedure is local to that procedure An entire array can be passed to a Sub or Function procedure

Chapter 7 - VB.Net by Schneider36 Example 4 Private Sub btnCompute_Click(...) Handles btnCompute.Click Dim score() As Integer = {0, 85, 92, 75, 68, 84, 86, _ 94, 74, 79, 88} txtAverage.Text = CStr(Sum(score) / 10) End Sub Function Sum(ByVal s() As Integer) As Integer Dim total, index As Integer total = 0 For index = 1 To s.GetUpperBound(0) 'The upper bound is 10 total += s(index) Next Return total End Function

Chapter 7 - VB.Net by Schneider37 Comments Searching successive elements of an ordered list beginning with the first element is called a sequential search.

Chapter 7 - VB.Net by Schneider38 Passing an Array Element A single element of an array can be passed to a procedure just like any ordinary numeric or string variable. Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim num(20) As Integer num(5) = 10 lstOutput.Items.Add(Triple(num(5))) End Sub Private Function Triple(ByVal x As Integer) As Integer Return 3 * x End Function

Chapter 7 - VB.Net by Schneider Some Additional Types of Arrays Control Arrays Array of Structures (OMIT)

Chapter 7 - VB.Net by Schneider40 Control Arrays Control arrays are arrays of controls, such as labels, text boxes, etc. They are created in much the same way as any other array: Dim arrayName(n) As ControlType or Dim arrayName() As ControlType

Chapter 7 - VB.Net by Schneider41 Control Arrays continued The following statements declare control arrays. Dim lblTitle(10) As Label Dim txtNumber(8) As TextBox Dim btnAmount() As Button Demo 7-3-1modified

Chapter 7 - VB.Net by Schneider42 Example 1 Dim lblDept(5) As Label Dim txtDept(5) As TextBox Private Sub Form1_Load(...) Handles MyBase.Load Dim depNum As Integer lblDept(1) = Label1 lblDept(2) = Label2 lblDept(3) = Label3 lblDept(4) = Label4 lblDept(5) = Label5 txtDept(1) = TextBox1 txtDept(2) = TextBox2 txtDept(3) = TextBox3 txtDept(4) = TextBox4 txtDept(5) = TextBox5 Array of controls Placing controls Into arrays

Chapter 7 - VB.Net by Schneider43 Example 1 continued For depNum = 1 To 5 lblDept(depNum).Text = "Department " & depNum txtDept(depNum).Clear() Next End Sub Private Sub btnCompute_Click(...) Handles btnCompute.Click Dim totalSales As Double = 0 Dim depNum As Integer For depNum = 1 To 5 totalSales += CDbl(txtDept(depNum).Text) Next txtTotal.Text = FormatCurrency(totalSales) End Sub

Chapter 7 - VB.Net by Schneider44 Example 1 Output

Chapter 7 - VB.Net by Schneider45 Exercises p. 345

Chapter 7 - VB.Net by Schneider46 ListBoxes Adding to a listbox Listboxes as arrays Events associated with listboxes

Chapter 7 - VB.Net by Schneider47 Useful Properties of the List Box Items can be placed into a listbox at design time or runtime lstBox.items.add(‘item1”) lstBox.items.add(“item2”) This forms an array with 2 elements For the index number of the currently highlighted item: lstBox.SelectedIndex

Chapter 7 - VB.Net by Schneider48 More List Box Properties lstBox.Items() is the list of items in the list box. The value of the item with an index of "n" is: lstBox.Items(n) The data type of the elements in the lstBox.Items() array is Object. To put the first element of lstBox.Items in a text box: txtBox.Text = CStr(lstBox.Items(0))

Chapter 7 - VB.Net by Schneider49 Currently Highlighted Item in a List Boxes The currently highlighted item can be obtained by: lstBox.Items(lstBox.SelectedIndex) Or lstBox.Text Or lstBox.SelectedItem

Chapter 7 - VB.Net by Schneider50 List Box Events Three main types of events with list boxes: Click – if the user clicks on an item in the list box SelectedIndexChanged – (default event) if the user clicks on an item or uses the arrow keys to select it DoubleClick - if the user double-clicks on an item Demo - Movies

Chapter 7 - VB.Net by Schneider51 The Random Class A random number generator declared with the statement: Dim randomNum As New Random() If m and n are whole numbers and m < n then the following generates a whole number between m and n (including m, but excluding n) randomNum.Next(m, n) Demo – Random