1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2010: Reloaded Fourth Edition
VBA Array-1 CS 105 Spring 2010 VBA Arrays What is an array? How can you identify an array by looking at the code? What is an element of an array and where.
Programming with Microsoft Visual Basic 2005, Third Edition
An Introduction to Programming with C++ Fifth Edition
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.
Arrays.
Introduction to Programming with C++ Fourth Edition
Chapter 9 Introduction to Arrays
Chapter 7: Working with Arrays
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Chapter 8 Arrays and More.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Chapter 9: Advanced Array Concepts
Using Arrays and File Handling
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Mouse Events. Mouse Driven Events Unlike control_click() which is passed no arguments (unless it's an index for an array), MouseDown, MouseUp and MouseMove.
CSCI Chapter 8 Arrays Instructor: Bindra Shrestha University of Houston – Clear Lake.
Week Arrays, Timers, and More 9. 2 Introduction Arrays are like groups of variables that allow you to store sets of similar data A single dimension array.
Chapter 10: Structures and Sequential Access Files
Array Processing.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CIS162AD - C# Arrays – part 1 12_arrays_loading.ppt.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
Tutorial 6 The Repetition Structure
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8 Arrays and More.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
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.
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.
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.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Arrays and More CHAPTER 8. INTRODUCTION Arrays are like groups of variables that allow you to store sets of similar data A single dimension array is useful.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional 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.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
13 Arrays CE : Fundamental Programming Techniques June 161.
Chapter 8 Arrays and More. Chapter 8 Arrays and More.
Chapter 6: Using Arrays.
An Introduction to Programming with C++ Sixth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Average Monthly Temperature and Rainfall
Visual Basic .NET BASICS
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Tutorial 11 Arrays Tutorial 11: Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Arrays Part 2.
Presentation transcript:

1 Microsoft Visual Basic 2010 Arrays

2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare and initialize a one-dimensional array  Assign data to a one-dimensional array  Display the contents of a one-dimensional array  Access an element in a one-dimensional array  Search a one-dimensional array  Compute the average of a one-dimensional array’s contents  Find the highest entry in a one-dimensional array  Update the contents of a one-dimensional array  Sort a one-dimensional array

3 Arrays  A simple variable, also called a scalar variable, is one that is unrelated to any other variable in memory  An array is a group of variables that have the same name and data type and are related in some way  Although arrays in Visual Basic can have as many as 60 dimensions, the most commonly used arrays are one- dimensional and two-dimensional  Programmers use arrays to store related data in the internal memory of the computer

4 One-Dimensional Arrays  A one-dimensional array is simply a row (or column) of variables  A two-dimensional array resembles a table in that it has rows and columns  Each element in an array is identified by a subscript, which Visual Basic assigns to the variable when the array is created  You refer to an array element by the array’s name followed by the element’s subscript

5 One-Dimensional Array AlaskaMontanaSouth CarolinaTennesseeTexas Alaska Montana South Carolina Tennessee Texas

6 Declaring an Array  Version 1 accessibility arrayname(highestSubscript) As datatype  Version 2 accessibility arrayname() As datatype = {initialValues}  These statements create and initialize the array variables in memory  accessibility is Dim, Public, or Private

7 Declaring an array  Dim strCitys(3) As String  Private intNumbers(5) As Integer  Private udtItems(4) As ItemStruc  Private strStates() As String = {“Hawaii”, “Alaska”, “Maine”}  Dim intScores() As Integer = {75, 9, 23, 6}

8 Storing Data in a One-Dimensional Array  You can use a variety of ways to enter data into an array strMonthArray(0) = “Jan” strMonthArray(1) = “Feb” strMonthArray(2) = “Mar” strMonthArray(3) = “Apr” strMonthArray(4) = “May” strMonthArray(5) = “June”

9 Assigning Values to Array Elements For intNum = 1 to 6 intSquareArray(intNum - 1) = intNum * intNum Next intNum For intNum = 0 to 10 sngNum(intNum) = Val(InputBox(“Enter number”)) Next intNum udtItems(0).sngPrice = udtItems(0).sngPrice * 1.1

10 One-Dimensional Array Dim intX As Integer Do While (intX <= 5 AndAlso Not EOF(1)) FileGet(1, udtItems(intX), intX + 1) intX += 1 Loop

11 Display the Contents of a One-Dimensional Array Dim strMonths() As String = {“JAN”, “FEB”, “MAR”, “APR”, “MAY”, “JUN”, “JUL”, “AUG”, “SEP”, “OCT”, “NOV”, “DEC”} Dim intX As Integer For intX = 0 To strMonths.Length - 1 Me.MonthListBox.Items.Add(strMonths(intX)) Next intX

12 Searching a One-Dimensional Array Dim intSales() As Integer = {45000, 35000, 25000, 60000, 23000} Dim intX As Integer ‘keeps track of subscripts Dim intCount As Integer ‘counter variable Dim intSearchFor As Integer ‘number to search for intSearchFor = Val(InputBox(“Enter sales to search for:”, _ “Sales”)) For intX = 0 To 4 If intSales(intX) > intSearchFor Then intCount += 1 End If Next intX MessageBox.Show(“Count: ” & intCount, “Sales”, _ MessageBoxButtons.OK, MessageBoxIcon.Information)

13 Calculating the Average Amount Stored in a One-Dimensional Numeric Array Declare variables Repeat for each score add array score to intTotal variable End repeat for intNum Calculate the average by dividing intTotal by number of scores Display the average

14 Calculating the Average Amount Stored in a One-Dimensional Numeric Array Dim intScores() As Integer = {98, 100, 56, 74, 35} Dim intX As Integer ‘keeps track of subscripts Dim intTotal As Integer ‘accumulator variable Dim sngAvg As Single ‘average score For intX = 0 To intScores.Length - 1 intTotal += intScores(intX) Next intX sngAvg = intTotal / intScores.Length MessageBox.Show(“Average: ” & sngAvg, “Average”, _ MessageBoxButtons.OK, MessageBoxIcon.Information)

15 Determining the Highest Value Stored in a One-Dimensional Array Declare variables Assign first array value (zero) to intHigh variable Initialize the intX variable to 1 (second subscript) Repeat while intX is less than the number of elements in the array If current array value > intHigh value then assign current array value to intHigh End If End repeat for intNum Display the highest value(stored in intHigh)

16 Updating the Values Stored in a One-Dimensional Array Declare variables Prompt user for the value to add Repeat for each price in the array add value to current array value display the contents of the current array element End repeat

17 Sorting the Data Stored in a One-Dimensional Array declare an Integer array named intNumbers declare an Integer variable named intX open the random access file named nums.data repeat while intX is less than or equal to 5 and it is not the end of the nums.data file read a number from the file and store it in the current array element display the contents of the current array element in a message box add 1 to the intX variable end repeat close the nums.data file sort the intNumbers array in ascending order using the Array.Sort method repeat for each element in the intNumbers array display the contents of the current array element in a message box end repeat

18 Summary of Array Methods ClearSets a range of elements in the Array to zero or to a null reference (Nothing in Visual Basic). CopyCopies a section of one Array to another Array IndexOfReturns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array LastIndexOfReturns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array ReverseReverses the order of the elements in a one- dimensional Array or in a portion of the Array SortSorts the elements in one-dimensional Array objects

19 Parallel Arrays  Arrays that are related by an element’s position (subscript)  Searching one array gives you the subscript for the other array Dim strId() As String = {"BX35”, “CR20", “FE15”, “KW10”, “MM67”} Dim intPrice() As Integer = {13, 10, 12, 24, 4} Dim intX As Integer, strSearchFor As String strSearchFor = UCase(IdTextBox.Text) ‘search the array Do While intX strId(intX) intX += 1 Loop Product IDPrice BX3513 CR2010 KW1024 MM674

20 An Array of Structures Declare an ItemStruc array named udtPriceList Declare variables intX and strSearchFor Assign IDs and prices to the udtPriceList array Assign the product ID entered in the IdTextBox control, converted to uppercase, to the strSearchFor variable Repeat while intX is less than the list length and the value of strSearchFor variable is not equal to the value stored in the current array element’s strId field Increment intX End repeat If the intX variable contains a number that is less than the lists length display the appropriate price from the intPrice field in the array Else display the message “Product ID is not valid” End if

21 An Array of Structs Structure ItemStruc Public strId As String Public intPrice As Integer End Structure Dim udtItem(4) As ItemStruc ‘populate the array udtItem(0).strId = “BX35”: udtItem(0).intPrice = 13 … ‘search the array Do While intX udtItem(intX).strId intX = intX + 1 Loop

22 Two-Dimensional Arrays  A two-dimensional array resembles a table in that the variables are in rows and columns AC24ShirtRed BD12CoatBlue CP14BlouseWhite strProducts(0, 0)strProducts(0, 2) strProducts(2, 1) strProducts(1, 2)

23 Two-Dimensional Arrays Version 1 accessibility arrayname(highestRowSubscript, highestColumnSubscript) As datatype Version 2 accessibility arrayname(,) As datatype = {{initialValues}, {initialValues}, …{initialValues}} Dim strCitys(5, 3) As String Dim intScores(,) As Integer = {{75, 90}, {9, 25}, {23, 56}, {6, 12}}

24 Storing data in a Two-dimensional Array Example 1 strCitys(0, 0) = “Madrid” strCitys(0, 1) = “Paris” strCitys(0, 2) = “Rome” strCitys(0, 3) = “London” Example 2 For intRow = 0 To 3 For intColumn = 0 To 1 intScores(intRow, intColumn) = 0 Next intColumn Next intRow

25 Calculating the Total in a Two-Dimensional Array Dim intSales(,) As Integer = {{12000, 10000}, _ {45000, 56000}, {32000, 42000}, _ {67000, 23000}, {24000, 12000}, _ {55000, 34000}} Dim intRow, intCol As Integer ‘keeps track of subscripts Dim intTotal As Integer ‘accumulator variable For intRow = 0 To 5 For intCol = 0 To 1 intTotal = intTotal + intSales(intRow, intCol) Next intCol Next intRow

26 Multidimensional Array Properties  Length will tell you the total number of elements  intSales.Length will return 12  Use GetLength(dimension) to get the size of a particular dimension  intSales.GetLength(0) will return 6  intSales.GetLength(2) will return 2 For intRow = 0 To intSales.GetLength(0) - 1 For intCol = 0 To intSales.GetLength(1) - 1 intTotal = intTotal + intSales(intRow, intCol) Next intCol Next intRow