Visual Basic 2010 How to Program

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8: Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
Chapter 7 – Arrays 7.1 Creating and Accessing Arrays
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Two-dimensional Arrays two-dimensional arrays are often used to represent tables of values consisting of data arranged in rows and columns (Fig. 7.16).
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays 1.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Chapter 6 JavaScript: Introduction to Scripting
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
IS 350 Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
Visual Basic 2010 How to Program
Chapter 7 Arrays.
JavaScript: Functions.
ARRAYS.
Arrays, For loop While loop Do while loop
Java How to Program, Late Objects Version, 10/e
CSCI 3327 Visual Basic Chapter 7: Arrays
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Introduction to LINQ Chapter 11.
Introduction To Programming Information Technology , 1’st Semester
Can store many of the same kind of data together
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Arrays.
Arrays Chapter 7.
Single-Dimensional Arrays chapter6
CIS16 Application Development and Programming using Visual Basic.net
Programming Logic and Design Fifth Edition, Comprehensive
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Computer Programming IT-104
Presentation transcript:

Visual Basic 2010 How to Program Chapter 7: Arrays Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Arrays An array is a group of variables (called elements) containing values that all have the same type. To refer to a particular element in an array, we specify the name of the array and the position number of the element to which we refer. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Dim arrayName(n) As DataType Arrays 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, n + 1, is the size of the array. You can determine the size of the array using the system method (length) arrayName.Length © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

© 1992-2011 by Pearson Education, Inc. All Rights Reserved © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 1: Arrays Terminology Dim C(11) As Integer 0 is the lower bound of the array C(0) value equals to -45 11 is the upper bound of the array C(11) value equals to 78 The number of elements, 12 , is the size of the array C.Length = 12 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Array Methods & properties arrayName.Count number of elements arrayName.Length arrayName.Max highest value arrayName.Min lowest value arrayName.First first element arrayName.Last last element arrayName.GetUpperBound(0) The upper bound value arrayName.GetLowerBound(0) The lower bound value numArrayName.Average average value of elements numArrayName.Sum sum of values of elements © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Maysoon Al-Duwais

Example 2: Array Methods & properties Dim array1() As Integer = {6,2,8} array1.Count 3 array1.Length array1.Max 8 array1.Min 2 array1.First 6 array1.Last array1.GetUpperBound(0) array1.GetLowerBound(0) array1.Average 5.3 array1.Sum 16 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Maysoon Al-Duwais

Example 3: Accessing Array Elements The position number in parentheses is called an index it can be: Nonnegative integer . Example: C(3) Or integer expression. Example: if value1 =5, value2 = 6 c(value1 + value2) += 2 c(5 + 6) += 2 c(11) += 2 C(11) = C(11) + 2 C(11) = 78 +2 C(11) =80 + 2 C(11) 78 C(11) 80 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 4: Accessing Array Elements Values stored in arrays can be used in calculations. For example, sum = c(0) + c(1) + c(2) sum = -45 + 6 + 0 sum = -39 result = c(6) \ 2 result = 0 \ 2 result = 0 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

How to declare Array? Different ways to declare array: Dim c(0 To 3) As Integer Dim c(3) As Integer Dim c() As Integer = {9, 2, 6, 1} Dim c() = {1, 2, 3, 6} The lower bound of all the three arrays above is 0 and the upper bound is 3. The size of all the three arrays above equals to 4. In the last two array declarations, we declared & initialize the array without specifying the upper bound value. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Declaring and intialize Arrays Dim c() As Integer = {9, 2, 6, 1} Dim c() = {1, 2, 3, 6} When the initializer list is used, you cannot specify the upper bound value. So, if you write the above declaration as follows: Dim c(3) As Integer = {9, 2, 6, 1} You will get a Syntax Error Initializer List X © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Default Initialization for Arrays When you do not provide an initializer list, the elements in the array are initialized to the default value for the array’s type as follows: 0 for numeric primitive data-type variables False for Boolean variables Nothing for String and other class types. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example5: Initializing the Values in an Array © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

© 1992-2011 by Pearson Education, Inc. All Rights Reserved © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example5: Initializing the Values in an Array Figure 7.2 creates two five-element integer arrays and sets their element values, using an initializer list and a For…Next statement that calculates the element values, respectively. Line 13 declares and allocates array2, whose size is determined by the expression array1.GetUpperBound(0) = 4 array1.GetLowerBound(0) = 0 Array1.Length = 5 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example5: Initializing the Values in an Array Dim array2(array1.GetUpperBound(0)) As Integer Dim array2(4) As Integer This means that array2 will have the same size of array 1: array2.GetUpperBound(0) = 4 array2.GetLowerBound(0) = 0 Array2.Length = 5 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Using Loops with Arrays In Example 6 the greatest value in a numeric array ages is determined. The value of the variable max is set to the first element of the array. Then a For…Next loop successively examines each element of the array and resets the value of max when appropriate. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 6: Output: Greatest age: 69 Dim ages() As Integer = {55, 56, 61, 52, 69, 64, 46, 54, 47} 'last 9 presidents Dim max As Integer = ages(0) For i As Integer = 1 To ages.Count - 1 If ages(i) > max Then max = ages(i) End If Next txtOutput.Text = "Greatest age: " & max Output: Greatest age: 69 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 6: Trace 55 ages(1) = 56 56 ages(2) = 61 61 ages(3) = 52 max ages(i) i 55 ages(1) = 56 1 56 ages(2) = 61 2 61 ages(3) = 52 3 ages(4) = 69 4 69 ages(5) = 64 5 ages(6) = 46 6 ages(7) = 54 7 ages(8) = 47 8 _ 9 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Flag Variables Have type Boolean Used when looping through an array Provide information to be used after loop terminates. Or, allows for the early termination of the loop. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 7: Using the Flag Variable Dim Names() As String = {"hend", "manal", "asma", "sarah“, “nouf”, “Lamya”} Dim nameFound As Boolean = False ‘ The Flag Variable Dim Name_Start_with_A As String = Nothing Dim upperName As String = Nothing Dim i As Integer = 0 Do While ( Not nameFound ) upperName = Names(i).ToUpper If upperName.StartsWith("A") Then 'Search a name that starts with ‘A’ nameFound = True Name_Start_with_A = Names(i) End If i += 1 Loop Label1.Text = "A Name that starts with A = " & Name_Start_with_A © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 7: Output © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 7: Trace Name_Start_with_A Not namesFound nameFound upperName Names(i) i Nothing True False HEND hend MANAL manal 1 asma ASMA 2 _ 3 Loop will stop here (when i =3) because the Do While ....Loop condition is not met (When the flag variable nameFound = True Not nameFound = False ) © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

For Each Loops can be replaced with For i As Integer = 1 To ages.Count - 1 If ages(i) > max Then max = ages(i) End If Next can be replaced with For Each age As Integer In ages If age > max Then max = age © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

For Each Loops (continued) In the For…Next loop, the counter variable i can have any name. In the For Each loop, the looping variable age can have any name. The primary difference between the two types of loops is that in a For Each loop no changes can be made in the values of elements of the array. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Searching for an Element in an Array A statement of the form numVar = Array.IndexOf(arrayName, value) assigns to numVar the index of the first occurrence of value in arrayName. Or assigns -1 if the value is not found. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 8: Searching for an Element in an Array Dim numbers() As Integer = {8, 2, 6, 6, 6} Label1.Text = "Array.IndexOf(numbers, 6)=" & Array.IndexOf(numbers, 6) & vbCrLf Label1.Text &= "Array.LastIndexOf(numbers, 6)=" & Array.LastIndexOf(numbers, 6) © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Copying an Array arrayOne = arrayTwo If arrayOne and arrayTwo have been declared with the same data type, then the statement arrayOne = arrayTwo makes arrayOne an exact duplicate of arrayTwo. Actually, they share the same location in memory. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 9: Copying Array Dim Names() As String = {"hend", "asma", "manal", "sarah"} Dim Names2(1) As String Names2 = Names For Each element In Names2 Label1.Text &= “element = " & element & vbCrLf Next © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

ArrayName = StringName.Split(“SplitCharacter”) Split Method Split can convert a string containing comma-separated data into a string array. ArrayName = StringName.Split(“SplitCharacter”) Split Character also called delimiter could be: Comma “,” Dot “.” Start “*” Semicolon “;” Or any other character If no character is specified, the space character “ “ will be used as the delimiter. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 10: Splitting Array Dim employee() As String Dim line As String = "Bob;23,50;45" employee = line.Split(“;") For i = 0 To employee.GetUpperBound(0) Label1.Text &= "employee(" & i & ") = " & employee(i) & vbCrLf Next sets the size of employees to 3 sets employees(0) = “Bob” sets employees(1) = “23,50” sets employees(2) = “45” © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Join Function The reverse of the Split method is the Join function. Join concatenates the elements of a string array into a string containing the elements separated by a specified delimiter. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Example 11: Join Function Dim greatLakes() As String = {"Huron","Ontario", "Michigan","Erie","Superior"} Dim lakes As String lakes = Join(greatLakes, ",") txtOutput.Text = lakes Output: Huron,Ontario,Michigan,Erie,Superior © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Out of Range Error The following code references an array element that doesn't exist. This will cause an error. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Two-dimensional Arrays two-dimensional arrays are often used to represent tables of values consisting of data arranged in rows and columns (Fig. 7.16). © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example1: Declaring & initializing two-dimensional Arrays (1 of 4) A two-dimensional array letters with two rows and two columns can be declared and initialized with ' numbers in a 2 by 2 array Dim letters(1, 1) As Char letters(0, 0) = “a” letters(0, 1) = “b” letters(0, 2) = “c” letters(1, 0) = “d” letters(1, 1) = “e” letters(1, 2) = “f” column2 column1 column0 Row0 Row1 c b a f e d © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example1: Declaring & initializing two-dimensional Arrays (2 of 4) Alternatively, the initialization can be written on one line, as shown in the two examples bellow: Dim letters = {{“a”,“b”,”c”}, {“d”,“e”,”f”}} Dim letters(,) As Char = {{“a”,“b”,”c”}, {“d”,“e”,”f”}} © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example1: Declaring & initializing two-dimensional Arrays (3 of 4) Important Notes: letters.Initialize() - initializes all the elements of the array by its default value. For example: if we declare array of Integer this function will initialize all elements by zero if we declare array of String this function will initialize all elements by the keyword Nothing letters.GetUpperBound(0) = number of rows in letters -1 = 2 -1 = 1 letters.GetUpperBound(1) = number of columns in letters -1 = 3 -1 = 2 © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example1: Declaring & initializing two-dimensional Arrays (4 of 4) Important Notes (Continued): letters.Length = number of elements in all dimensions (rows x columns) in values letters.Length = 2 rows x 3 columns = 6 elements You cannot use some functions in two dimensional arrays such as: letters.count() X letters.SetValues(value Of element , Index) X © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example2: Manipulating Two-dimensional Array (1 of 4) The program in the next slide initializes 2 by 3 array ( array with 2 rows and 3 columns) called values. Then uses nested For…Next loops to traverse the array (that is, to manipulate every array element). The contents of the array are displayed in outputTextBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example2: Manipulating Two-dimensional Array (2 of 4) (The Code) © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

outputTextBox.AppendText Example2: Manipulating Two-dimensional Array (3 of 4) (Trace) values.GetUpperBound(0) = number of rows – 1 = 1 values.GetUpperBound(1) = number of columns – 1 = 2 outputTextBox.AppendText values(row , column) column <= 2 row < = 1 1 values(0,0) = 1 1 2 values(0,1) = 2 1 2 3 values(0,2) = 3 2 - 3 4 values(1,0) = 4 4 5 values(1,1) = 5 4 5 6 values(1,2) = 6 © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Example2: Manipulating Two-dimensional Array (4 of 4) (The Output) © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Road-distance Table (kilometers km) Riyadh Jeddah Dammam Hail 846 390 600 1236 715 950 © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Road-Mileage Array Dim rm(,) As Double = {{0, 846, 390, 600}, {846, 0, 1236, 715}, {390, 1236, 0, 950}, {600, 715, 950, 0}} declares and initializes an array of road-mileages. Some elements of the array are rm(0,0)=0, rm(0,1)=2054, rm(1,2)=2786 © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais

Road-distance Table (kilometers km) Column Row 1 2 3 846 390 600 1236 715 950 © 1992-2011 by Pearson Education, Inc. All Rights Reserved-Edited By: Maysoon Al-Duwais