Lecture Roger Sutton CO331 Visual Programming 12: One-dimensional Arrays 1.

Slides:



Advertisements
Similar presentations
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Advertisements

AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
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 CS 201 Passing Function as Parameter & Array Debzani Deb.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
 Pearson Education, Inc. All rights reserved Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Apply Sub Procedures/Methods and User Defined Functions
CS0004: Introduction to Programming Variables – Numbers.
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
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
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.
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.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Tutorial 6 The Repetition Structure
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
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.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays 1.
Programming Right from the Start with Visual Basic .NET 1/e
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
2. Understanding VB Variables
ARRAYS.
Visual Basic .NET BASICS
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.
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Arrays.
Presentation transcript:

Lecture Roger Sutton CO331 Visual Programming 12: One-dimensional Arrays 1

CO331 Visual Programming 2 Declaration An array is a consecutive group of memory locations that have the same name and type. Their declaration will include an explicit indication of the size of the array or the means to infer its size. E.g.Dim marks(5) As integer This indicates to the compiler that 6 memory locations should be reserved for an integer array called marks. The first element of an array has a position number 0. On declaration, the upper bound (i.e. the highest index number) of the array is specified (not the actual size). Several arrays (or variables) may be declared in the same line: E.g.Dim x(6) As Integer, s(24) As String

CO331 Visual Programming 3 Array data types An array can hold any data type Primitive data type, e.g.  Integer  Double  Boolean ADTs, e.g.  Controls- Button,TextBox  VB library- Random  User-defined- Circle,Square,Account The only constraint is that all the elements in an array must be of the same type

CO331 Visual Programming 4 Initialisation Arrays may be initialised on declaration, E.g. Dim age( ) As Integer = { 23, 54, 96, 13, 7, 32} Here the size of the array is not specified but is inferred by the number of initial values. By default numeric array elements are initialised to zero String to “” Objects to Nothing. Alternatively a programmer may explicitly initialise the array using assignment statements. Dim age(5) As Integer age(0) = 23 age(1) = 54 age(2) = 96 age(3) = 13 age(4) = 7 age(5) = 32

CO331 Visual Programming 5 Referencing To reference a particular element of the array, its name and position number are specified. E.g. the fourth element of marks array is referred to by: marks(3) The position number contained within brackets is more formally called an index. Sometimes it is useful to use a variable value as an index. Such variables are usually declared to be Integer. The Functions LBound and Ubound take an array as their argument and return the lowest and highest numbered index value respectively. These are useful to determine the length of an array and ensure an index remains within its declared range.

CO331 Visual Programming 6 E.g. Loops involving arrays The following accumulates a set of marks: This is equivalent to the following which uses a while loop: Dim std As integer Dim total As integer = 0 For std = LBound(marks) to UBound(marks) total += marks(std) Next Dim total As integer = 0 Dim std As Integer = 0 Do while std < UBound(marks) total += marks(std) std += 1 Loop

CO331 Visual Programming 7 Example: program

CO331 Visual Programming 8 Methods and properties All arrays have access to the methods and properties of System.Array class. E.g.  Length – returns the total number of elements in the array  getUpperBound( 0 ) - returns the upper bound of the one-dimensional array  Sort - sorts the array in an ascending order

CO331 Visual Programming 9 Methods and properties – cont’d E.g. Dim size, highestIndexNo As Integer Dim myArray() As Integer = {2, 3, 4, 1} size = myArray.Length highestIndexNo = myArray.GetUpperBound(0) Array.Sort(myArray) 2341 myArray size highestIndexNo myArray

CO331 Visual Programming 10 Example: using arrays Listing days of the week: cmdShow method Private Sub cmdShow_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdShow.Click Dim weekDays() As String = {"Monday", "Tuesday", "Wednesday", _ "Thursday", "Friday"} Dim i As Integer lstBox.Items.Add("Index Day") For i = 0 To weekDays.getUpperBound(0) lstBox.Items.Add( CStr(i) & Tab & weekDays(i) ) Next End Sub

CO331 Visual Programming 11 Change array size Once an array is created its length/size is fixed its index ranges from 0 to its upper bound When a program is executed, Visual Studio automatically performs bounds checking to ensure the program does not attempt to access data outside the bounds of an array. Referencing array elements outside the array bounds causes a runtime error. ReDim – is used to change the length/size of an array at run- time, i.e. as a program executes.

CO331 Visual Programming 12 Changing array size - cont’d E.g. ReDim Preserve weekDays(6) weekDays(5) = "Saturday" weekDays(6) = "Sunday " Dim weekDays() As String = { "Monday", "Tuesday", "Wednesday", _ "Thursday", "Friday" } weekDays.Length is 5 weekDays.GetUpperBound(0) is 4 weekDays.Length is 7 weekDays.GetUpperBound(0) is 6 The keyword Preserve ensures that data stored in the array are retained.

CO331 Visual Programming 13 Passing Arrays An array is passed to a method or function by including the array name in the argument list. E.g.Dim marks(5) As Integer CalculateStats(marks) A single array element may also be passed by simply specifying the array element in the procedure’s argument list. E.g.TopMark(marks(3)) For a method to receive an array through a call, the parameter list must indicate that an array will be received. E.g.Private Sub useArray( ByVal x( ) As Integer ) Note the size of the array is not specified between the brackets.

CO331 Visual Programming 14 Passing Arrays - cont’d E.g. Calculate the average course mark: Dim courseMarks() As Integer = {85, 70, 90, 65} Dim mean As Double mean = Average(courseMarks) MessageBox.Show(" Mean: " & CStr(mean) ) Private Function Average(ByVal arr() As Integer) As Double Dim i As Integer Dim totalValue As Integer = 0 Dim totalNo As Integer = arr.Length For i = 0 To totalNo - 1 totalValue += arr(i) Next Return Math.Round( totalValue / totalNo, 1) End Function The size of the array is not specified.

CO331 Visual Programming 15 Summary An array is a collection of data with a single name All the elements in an array are of the same type The index of an array starts at 0 All arrays have access to methods and properties of System.Array class, e.g.  Length  getUpperBound( 0 )  Sort Referencing array elements outside the array bounds causes a runtime error The size of an can be changed at run-time using ReDim