Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.

Slides:



Advertisements
Similar presentations
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Searching and Sorting Arrays
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
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
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Chapter 8 Arrays and Strings
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting 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.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
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.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Two-Dimensional Arrays. Two-dimensional arrays variables store the contents of tables or matrices. Example: Dim arrTable(1 to 5, 1 to 5) As Integer first.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Copyright © Don Kussee 1120-Ch9#491 CNS 1120 Chapter 9 arrays 1120-Ch9.PPT.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Searching and Sorting Arrays
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 7 Arrays.
Searching and Sorting Arrays
Data Structures (CS212D) Week # 2: Arrays.
Searching and Sorting Arrays
Introduction to Computer Programming IT-104
Presentation transcript:

Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays

2 Outline and Objective Arrays in Visual Basic One- dimensional arrays Control arrays Two-dimensional arrays Searching Sorting

3 Array verses Simple Variable Simple variable is used to store a single value. Array variable is used to represent many values of the same type with one variable name.

4 Parts of an Array Array Name: A valid variable name for the structure. Subscript or Index : A value that refers to a particular array element. Which must be defined as an integer Element: An individual data item within an array.

5 Array Declaration Syntax Dim arrayname ( 1 To n) As VarType

6 Examples of arrays Dim strMonth ( 1 To 12) As String Dim intScore (1 To 30) As Integer Dim strStudents (1 To 30 ) As String Dim strStudents (30) as String Will the last two arrays hold the same number of elements?

7 The Dim Statement Used when you need to define an array. The Dim statement sets up storage area in memory for the array. ******A Dim statement must occur before the first reference to the array elements.*******

8 Initializing an Array Private Sub cmdWhoWon_Click() Dim strTeamName( 1To 5) As String Dim n As Integer ' Fill array with World Series Winners strTeamName(1) = "Red Sox" strTeamName(2) = "Giants" strTeamName(3) = "White Sox" strTeamName(4) = "Cubs" strTeamName(5) = "Cubs" n = Val(txtNumber.Text) picWinner.Print "The "; strTeamName(n); " won World Series number"; n End Sub

9 Array strTeamName() strTeamName( 1 To 5) As String Red Sox Giants White Sox Cubs Cubs strTeamName(1) Array Name Index

10 Initializing an Array by Reading from a File Dim strCereal ( 1 To 30) As String Dim strMfr (1 to 30) as string Dim intCount as Integer Open “Cereal.txt” For Input As #1 For intCount = 1 To 70 Input #1, strCereal ( intCount ), strMfr(intcount) Next intCount

11 Parallel Arrays, Use the same index Dim intScore( 1 To 30) As Single, strStudent (1 To 30) As String Dim sngAverage as Single, intSum as Integer Open “StudentFile.txt” For Input As #1 intSum = 0 For intCount = 1 To 30 Input #1, strStudent (intCount), intScore (intCount) intSum = intSum + intScore(intCount) Next intCount sngAverage = sum/30

12 Parallel Arrays Arrays are parallel if subscripted variables have the same subscript and are related.

13 Example of Parallel Arrays Dim strName ( 1 To 8) As String Dim intScore ( 1 To 8 ) As Integer Dim intStudent as Integer Open “Score.txt” For Input As #1 For intStudent = 1 To 8 Input #1, strName(intStudent), intScore(intStudent) Next intStudent Close #1 intStudent is the ?

14 Summing the Elements of an Array in a For-Next Loop RateOfReturn For K = 1 To 5 Sum = Sum + RateOfReturn(K) Next K When K = 1,the statement is Sum = Sum + RateOfReturn(1)

15 Ordered Array An array is ordered if its values are in either ascending or descending order. For string arrays, the ASCI table is used to evaluate the “less than or equal to” condition. Is “Ace” > “Nine”

16 Processing Arrays Searching successive elements of an array is called a Sequential Search. Also called linear search or serial search. A Sequential Search examines each element, from the first to the last, until the specified value is found or the end of the array is reached.

17 Example of Sequential Search (finding the sngQuiz grades greater than 8) Dim sngQuiz ( 1 To 15) As Single Dim intPosition as Integer For intPosition =1 TO 15 If sngQuiz(intPosition) > 8 THEN picOutput.Print sngQuiz (intPosition) intCount = intCount + 1 End If Next intPosition picOutput.Print “Number of Grades found =“ intCount

18 Sequential Search Useful for short lists. 1 to 500 elements Very inefficient for long lists ( example names in telephone book). Use binary search if the list is sorted.

19 Binary Search In binary search, an ordered array is repeatedly divided in half. The half not containing the target value is ignored. To use binary search, the data in the array must be arranged in ascending or descending order. (Sorted)

Binary search with a target value of 76 SampleArray Lower bound Upper bound Begin search at midpoint of array (element 6) mid = (lowerbound + upperbound) \ 2 If value in element 6 is = 76, then we are done. If value in element 6 is < 76, then set lower bound to mid + 1. Otherwise, set upper bound to mid - 1.

21 Now recompute the midpoint mid = (lowerbound + upperbound) \ 2 If value in element 9 is = 76, then we are done. If value in element 9 is < 76, then set lower bound to mid + 1. Otherwise, set upper bound to mid - 1. SampleArray Lower bound Upper bound Binary search with a target value of

Now recompute the midpoint mid = (lowerbound + upperbound) \ 2 Recall that the arithmetic operator (\) means integer division (15 \ 2 = 7) SampleArray Lower bound Upper bound Binary search with a target value of 76 We have now found the value we were searching for. If we had not found the value, we would have adjusted the lower or upper bound as appropriate and then computed a new midpoint.

23 Dim strFirm(1 to 100) as String Private Sub BinarySearch(strCorp As String, strResult As String) Dim first as Integer, middle as Integer, last as Integer Dim Found as Integer, intFirms as Integer, strFirm as String Found = 0 '1 indicates strCorp has been found first = 1; last = intFirms Do While (first strCorp last = middle - 1 Case Is < strCorp first = middle + 1 End Select Loop End Sub

24 Passing an Array An array can be passed to another procedure by Reference.

25 Example of Passing an array: Private Sub cmddisplay_Click() ' Pass array to Sub Procedure and function Dim intScore(1 To 5) As Integer Call FillArray(intScore( ) ) picAverage.Cls picAverage.Print ”Average is"; Sum(intScore( ) ) / 5 End Sub Passing array intScore User Function

26 Array intScore is passed to a Subprogram Private Sub FillArray( s( ) As Integer) ' Fill array with intScores s(1) = 85 s(2) = 92 s(3) = 75 s(4) = 68 s(5) = 84 End Sub This array is pointing to the same location as array intScore

27 Array intScore is passed to a Function Private Function Sum ( s ( ) As Integer ) As Integer Dim intTotal As Integer, intIndex As Integer ' Add up intScores intTotal = 0 For intIndex = 1 To 5 intTotal = intTotal + s(intIndex) Next intIndex Sum = intTotal End Function

28 Control Array A means of constructing arrays of text boxes, labels, and command buttons. At least one element of a control array must be created when the form is designed. The remaining elements can be created either during form design, or with the Load statement.

29 Creating Control Array during Form Design Add one instance of the desired control to the form Set the Index property to a number Set any other properties that will be common to all other elements Click on the control and then press Ctrl+C Press Ctrl + V, to create the next element

30 Control Array All the properties of the first element are passed to other elements of the control array including the Top and Left properties. The only property that differs from first element is the Visible property. The Load statement sets the Visible property to False.

31 Control Array Event Procedures: Even though we may have many elements in the txtBox( ) control array, we will have just one txtBox_GotFocus event procedure to deal with. The value of intIndex property for the elements of the control array must be passed. Example: Private Sub txtBox_GotFocus (intIndex As Integer)

32 Creating Control Array at Run Time: You can create elements of a control array by using the Load statement at run time.

33 Creating Control Array at Run Time: The standard unit of measurement is called Twip. To place a new element of a control array, adjust the Top and Left properties during run time. Set the Visible property to True.

34 The location and size of a control Set the index property to zero!

35 Example ( Creating control array during run time) Private Sub Form_Load() Dim i As Integer, monthNames As String monthNames = "FebMarAprMayJunJulAugSepOctNovDec" For i = 1 To 11 Load lblMonth(i) Load txtInfo(i) lblMonth(i).Top = lblMonth(i - 1).Top + txtInfo(0).Height txtInfo(i).Top = txtInfo(i - 1).Top + txtInfo(0).Height lblMonth(i).Caption = Mid(monthNames, 3 * i - 2, 3) lblMonth(i).Visible = True txtInfo(i).Visible = True Next i End Sub

36 Sorting A common practice involving arrays is sorting the elements of the array in either ascending or descending order. You can sort an array in alphabetic order or numeric order. There are various methods to sort data items.

37 Types of Sorting Bubble Sort Shell Sort

38 Bubble Sort The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order. One complete time through the array is called a pass.

39 Bubble Sort For passNum = 1 To 4 'Number of passes is 1 less than number of items For intIndex = 1 To 5 - passNum If name(intIndex) > name(intIndex + 1) Then Call SwapData( name( ), intIndex) End If Next intIndex Next passNum

40 Swapping two variables Private Sub SwapData ( A ( ) As String, intIndex As Integer) Dim temp As String temp = A ( intIndex) A(intIndex ) = A (intIndex + 1) A ( intIndex + 1) = temp End Sub

41 Bubble Sort The maximum number of passes necessary to sort the elements in an array is equal to the number of elements in the array less 1. The minimum number of passes to sort an array may be one. It works well for a small number of data items. It is too slow for really long lists.

42 Shell Sort Is similar to the bubble sort, but instead of comparing and swapping adjacent elements A(intCount) and A(intCount+1), it compares and swaps nonadjacent elements A(intCount) and A(intCount + Gap), where Gap starts considerably greater than 1.

43 Shell Sort Gap is set to one-half the length of the array. After each pass if flag is set to 1, then Gap is halved again for the next pass. At the end Gap becomes one, and adjacent elements are compared and swapped.

44 Shell Sort gap = Int(numParts / 2) Do While gap >= 1 Do doneFlag = 1 For intIndex = 1 To numParts - gap If part(intIndex) > part(intIndex + gap) Then Call Swap( part(intIndex), part(intIndex + gap) doneFlag = 0 End If Next intIndex Loop Until doneFlag = 1 gap = Int(gap / 2) 'Halve the length of the gap Loop

45 Efficiency of Bubble and Shell sort Array Elements Bubble Sort Shell Sort ,75022,517

46 Two-Dimensional Arrays Store values as a table, grouped into rows and columns. The first subscript of a two-dimensional array refers to the row and the second subscript to the column.

47 Declaration of Two-Dimensional Array Syntax: Dim arrayname (r1 To n1, c2 To n2) As vartype Example: Dim rm (1 To 4, 1 To 4) As Single column row

48 Example of a two-dimensional array: rm(1 To 4, 1 To 4) Chicago Los Angeles New York Philadelphia ChicagoLos AngelesNew YorkPhiladelphia rm (2,2) rm (3,4)

49 Example of Input File 0, 2054, 802, , 0, 2786, , 2786, 0, , 2706, 100, 0

50 Manipulating a Two-Dimensional Array Use nested For ….. Next loops to assign or access elements of a two-dimensional array. Example: For row = 1 To 4 For col = 1 To 4 Input #1, rm(row, col) Next col Next row

51 Example of two-dimensional array Dim a(1 To 4, 1 To 5 ) As Integer Private Sub cmdDisplay_Click() For j = 1 To 4 For k = 1 To 5 a (j, k) = (j - k) * j picOutput.Print a(j,k); Next k picOutput.Print Next j End Sub