Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2010: Reloaded Fourth Edition
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
VBA Modules, Functions, Variables, and Constants
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
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.
Arrays.
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.
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.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 7: Working with Arrays
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
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.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with 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.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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:
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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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:
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
Chapter 7 – Arrays 7.1 Creating and Accessing Arrays
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.
Arrays. Variable Arrays a group of variables that have the same name and data type each element in a variable array is identified by a subscript refer.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
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:
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 Chapter Arrays, Timers, and More 8.
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.
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 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
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.
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.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
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.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays.
Arrays 1.
Chapter 7 Arrays.
ARRAYS.
Visual Basic 2010 How to Program
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Chapter 7 - VB.Net by Schneider
Presentation transcript:

Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: Arrays

Objectives  Creating and Accessing Arrays  Using Arrays  Some Additional Types of Arrays  Sorting and Searching  Two-Dimensional Arrays

Simple and Array Variables  A variable (or simple variable) is a name to which Visual Basic can assign a single value.  An array variable is a collection of simple variables of the same type to which Visual Basic can efficiently assign a list of values. 3

Simple and Array Variables  Consider the following situation:  Suppose that you want to evaluate the exam grades for 30 students.  Not only do you want to compute the average score, but you also want to display the names of the students whose scores are above average. 4

Declaring an Array Variable  Visual Basic provides a data structure called an array that let us do what we tried to accomplish in the loop.  The variables names, similar to those in the previous example, will be: Student(0), student(1),.., student(29) AND Score(0),score(1),….,score(29)

Declaring an Array Variable  We refer to these collection of variables as the array variables student() and score ().  The numbers inside the parentheses of the individual variables are called subscripts or indexes and each individual variable is called a subscripted variable or element.

Declaring an Array Variable  Array variables have the same kinds of names as simple variables.  If arrayName is the name of an array variable and n is an integer literal, variable the the declaration statement Dim arrayName(n) As varType reserves space in memory to hold the values of the subscripted varialbes arrayName(0),….,arrayName(n).

Simple and Array Variables  Many variables  An array variable Value1Value4Value7…… Values Location

Simple and Array Variables  Many variables  VS  An array variable  Which is better? Benefits? 9

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. COULD DO Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click Dim student0 As String, score0 As Double Dim student1 As String, score1 As Double Dim student2 As String, score2 As Double Handles btnDisplay.Click Dim student0 As String, score0 As Double Dim student1 As String, score1 As Double Dim student2 As String, score2 As Double 10

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. BETTER Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click Dim student(29) As String, score(29) As Double Handles btnDisplay.Click Dim student(29) As String, score(29) As Double 11

Declaring an Array Variable  The subscripted variables will all have the same data type, namely, the type specified by varType.  The could be all String variables or all integer Variables.

Using Arrays Dim student(29) As String Dim score(29) As Double Why 29 if we need 30 students?? 13 Array name Upper bound of subscripts in the array Data type

Putting Values into an Array student(0) = "Tom Brown" 14 subscript Read: "student sub zero equals Tom Brown" Which means that the string "Tom Brown" is being stored at the first location in the array called student… because all arrays begin counting at 0.

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, n + 1, is the size of the array 15

Example 1: Form 16 txtNumber txtWinner

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

Example 1: Output 18

Load Event Procedure  The array teamName was assigned values within the btnWhoWon_Click event procedure.  Every time the button is clicked, the values are reassigned to the array.  The manners of assigning values to an array can be very inefficient, especially in program with large arrays where the task of the program may be repeated numerous times for different user input. 19

Load Event Procedure Occurs as the Form loads in memory Private Sub frmName_Load(...) _ Private Sub frmName_Load(...) _ Handles MyBase.Load Handles MyBase.Load The keyword MyBase refers to the form being loaded. This event procedure is a good place to assign values to an array. - This is the FIRST thing that loads - Loads BEFORE you see the form 20

Example 2 Dim teamName(3) As String Private Sub btnWhoWon_Click(...) Handles btnWhoWon.Click Dim n As Integer Dim n As Integer n = CInt(txtNumber.Text) n = CInt(txtNumber.Text) txtWinner.Text = teamName(n - 1) txtWinner.Text = teamName(n - 1) End Sub Private Sub frmBowl_Load(...) Handles MyBase.Load 'Place Super Bowl Winners into the array 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(0) = "Packers" teamName(1) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(2) = "Jets" teamName(3) = "Chiefs" teamName(3) = "Chiefs" End Sub 21

Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = {value0, _ value1, value2,..., valueN} value1, value2,..., valueN}  declares an array having upper bound N and assigns value0 to arrayName(0), value1 to arrayName(1),..., and valueN to arrayName(N). 22

Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = {value0, _ value1, value2,..., valueN} value1, value2,..., valueN}  For Example: Dim Students() As String = {"Jack", "John", "Julie", "Jimmy", "Janet"} 23

Initializing Arrays  Arrays may be initialized when they are created: Dim arrayName() As varType = IO.File.ReadAllLines(filespec)  Opens filespec, reads all lines from it, and stores it in arrayName  Each line in filespec is stored in one location of arrayName 24

GetUpperBound Method The value of arrayName.GetUpperBound(0) is the upper bound of arrayName(). 25

Example Private Sub btnWhoWon_Click(...) _ Handles btnWhoWon.Click Handles btnWhoWon.Click Dim teamName(3) As String Dim teamName(3) As String Dim n As Integer Dim n As Integer 'Place Super Bowl Winners into the array 'Place Super Bowl Winners into the array teamName(0) = "Packers" teamName(0) = "Packers" teamName(1) = "Packers" teamName(1) = "Packers" teamName(2) = "Jets" teamName(2) = "Jets" teamName(3) = "Chiefs" teamName(3) = "Chiefs" 'Access array 'Access array n = CInt(txtNumber.Text) n = CInt(txtNumber.Text) txtWinner.Text = teamName(n - 1) txtWinner.Text = teamName(n - 1) End Sub 26 teamName.GetUpperBound(0)?

Example Dim teamName() As String = {"Packers", _ "Packers", "Jets", "Chiefs"} "Packers", "Jets", "Chiefs"} txtBox.Text = CStr(teamName.GetUpperBound(0)) Output: 3 Why 3? There are 4 elements! 27

Example Dim Grades() As String = {70, 75, 80, 85, 90} Grades.Average  80 Grades.Count  5 Grades.Min  70 Grades.Max  90 Grades.Sum 

ReDim Statement The size of an array may be changed after it has been created. ReDim arrayName(m) ReDim arrayName(m) changes the upper bound of the array to m. 29

ReDim Statement ReDim arrayName(m) ReDim arrayName(m)  Note: Since the type cannot be changed, there is no need for an “As datatype” clause at the end of the ReDim statement. 30

Preserve Keyword  ReDim arrayName(m) resets all values to their default.  String values to Nothing and resets all numeric values to 0.  This can be prevented with the keyword Preserve. ReDim Preserve arrayName(m)  Resizes the array and retains as many values as possible. 31

Out of Bounds Error  The following code references an array element that doesn't exist. This will cause an error. 32

Out of Bounds Error  Using a subscript greater than the upper bound of an array is not allowed. 33

Assignment Statement for Arrays  If arrayOne() and arrayTwo() have been declared with the same data type, then the statement arrayOne = arrayTwo arrayOne = arrayTwo  makes arrayOne() an exact duplicate of arrayTwo().  Actually, they share the same location in memory - What does this mean?? - What does this mean?? 34

Using Part of An Array  In some programs we must dimension an array before knowing how many pieces of data are to be placed into it.  We dimension the array large enough to handle all reasonable contingencies.

Using Part of An Array  If an array is to hold exam grades, and class size are at most 100 student.  We use the statement such as Dim grades (99) As Integer.  We must to employ a counter variable to keep track of the number of values actually stored in the array.

Two-Dimensional Arrays  Each array discussed so far held a single list of items. Such array variables are called one- dimensional Array.  An array can also hold the contents of table with several rows and columns. Such array variables are called two-dimensional arrays. 37

Two-Dimensional Arrays  They have the same types of names as other array variables.  The only difference is that they have two subscripts, each with its own upper bound.  The firs subscript is determined by the number of rows in the table, and the second subscript is determined by the number of colummns.  Dim arrayName (m,n) As DataType 38

Two-Dimensional Arrays 1234 BusinessHarvardStanfordU of PAMIT EngineeringMITStanfordUC BerkGA Teach LawYaleHarvardStandardNYU 39 Univ(0,0) = “Harvard” Univ (1,2) = “UC Berk” Unive (2,3) = “NYU” Univ(1,1) = ? Univ(1,3) = ? Univ (3,3) = ? Univ (2,1) = ?

Two-Dimensional Arrays  An unsized two-dimensional array can be declared with a statement of the for  Dim arrayName(,) As VarType  Two dimension array can be declared and initialized at the same time wit a statement of the form  Dim arrayName(,) As varType = {{ROW0}, {ROW1}, … {ROWn}} 40

Erasing an Array  An array can consume a large block of memory.  After the array is no longer needed, we can release all memory allocated to the array by executing the following statement: Erase arrayName 41