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

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Programming with Microsoft Visual Basic 2005, Third Edition
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
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.
C++ for Engineers and Scientists Third Edition
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.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Array Processing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6: Arrays: Lists and Tables
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.
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.
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.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
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.
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.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Arrays … The Sequel Applications and Extensions
Searching and Sorting Arrays
Searching and Sorting Arrays
Introduction to Computer Programming IT-104
Presentation transcript:

Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays

Chapter 7 - Visual Basic Schneider2 Outline and Objective Arrays in Visual Basic One- dimensional arrays Control arrays Two-dimensional arrays Searching Sorting

Chapter 7 - Visual Basic Schneider3 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.

Chapter 7 - Visual Basic Schneider4 Elements of an Array Array Name: A valid variable name for the structure. Subscript or Index : A value that refers to a particular array element. Element: An individual data item within an array.

Chapter 7 - Visual Basic Schneider5 Array Declaration Syntax Dim arrayname ( 1 To n) As VarType

Chapter 7 - Visual Basic Schneider6 Examples of arrays Dim month ( 1 To 12) As String Dim score (1 To 30) As Single Dim students (1 To 30 ) As String

Chapter 7 - Visual Basic Schneider7 The Dim Statement Used when you need to define an array. The Dim statement sets up storage for array. A Dim statement must occur before the first reference to the array elements.

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

Chapter 7 - Visual Basic Schneider9 Array teamName() teamName( 1 To 5) As String Red Sox Giants White Sox Cubs Cubs teamName(1) Array Name Index

Chapter 7 - Visual Basic Schneider10 Initializing an Array by Reading from a File Dim student ( 1 To 30) As String Open “STUDENTS.TXT” For Input As #1 For count = 1 To 30 Input #1, student ( count ) Next count

Chapter 7 - Visual Basic Schneider11 Adding Rows in an Array Dim score( 1 To 30) As Single, student (1 To 30) As String Open “STUDENT.TXT” For Input As #1 For count = 1 To 30 Input #1, student (count), score (count) Next count sum = 0 For count = 1 To 30 sum = sum + score(count) Next count average = sum/30

Chapter 7 - Visual Basic Schneider12 Parallel Arrays Two arrays are referred to as parallel if subscripted variables having the same subscript are related.

Chapter 7 - Visual Basic Schneider13 Example of Parallel Arrays Dim nom ( 1 To 8) As String, score ( 1 To 8 ) As Integer Open “SCORE.TXT” For Input As #1 For student = 1 To 8 Input #1, nom(student), score(student) Next student Close #1

Chapter 7 - Visual Basic Schneider14 Ordered Array An array is ordered if its values are in either ascending or descending order. For string arrays, the ANSI table is used to evaluate the “less than or equal to” condition.

Chapter 7 - Visual Basic Schneider15 Processing Arrays Searching successive elements of an array is called 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.

Chapter 7 - Visual Basic Schneider16 Example of Sequential Search (finding the quiz grades greater than 8) Dim quiz ( 1 To 15) As Single For position =1 TO 15 If quiz(position) > 8 THEN picOutput.Print quiz (position) count = count + 1 End If Next Position

Chapter 7 - Visual Basic Schneider17 Sequential Search Useful for short lists. Very inefficient for long lists ( example names in telephone book). Use binary search if the list is sorted.

Chapter 7 - Visual Basic Schneider18 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.

Chapter 7 - Visual Basic Schneider19 Binary Search Private Sub BinarySearch(corp As String, result As String foundFlag = 0 '1 indicates corp found first = 1 last = numFirms Do While (first corp last = middle - 1 Case Is < corp first = middle + 1 End Select Loop End Sub

Chapter 7 - Visual Basic Schneider20 Binary Search If foundFlag = 1 Then result = "found” Else result = "not found End If

Chapter 7 - Visual Basic Schneider21 Merging A common practice involving arrays is merging two sorted arrays.

Chapter 7 - Visual Basic Schneider22 The method for merging two sorted arrays : 1.Compare the two first elements of the first and second arrays. A. If one name alphabetically precedes the other, copy it onto the third list and cross it off the original array. B. If the names are the same, copy the name onto the third list and cross out the name from both arrays. 2. Repeat Step 1 with the new name in the array until you reach the end of either array. 3. Copy the names from the remaining array onto the third array.

Chapter 7 - Visual Basic Schneider23 Passing an Array An array can be passed to another procedure by reference.

Chapter 7 - Visual Basic Schneider24 Example of Passing an array: Private Sub cmddisplay_Click() ' Pass array to subprogram and function Dim score(1 To 5) As Integer Call FillArray(score( ) ) picAverage.Cls picAverage.Print ”Average is"; Sum(score( ) ) / 5 End Sub Passing array score

Chapter 7 - Visual Basic Schneider25 Array Score is passed to a Subprogram Private Sub FillArray(s( ) As Integer) ' Fill array with scores 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 score

Chapter 7 - Visual Basic Schneider26 Array Score is passed to a Function Private Function Sum(s ( ) As Integer) As Integer Dim total As Integer, index As Integer ' Add up scores total = 0 For index = 1 To 5 total = total + s(index) Next index Sum = total End Function

Chapter 7 - Visual Basic Schneider27 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.

Chapter 7 - Visual Basic Schneider28 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

Chapter 7 - Visual Basic Schneider29 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.

Chapter 7 - Visual Basic Schneider30 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 Index property for the elements of the control array must be passed. Example: Private Sub txtBox_GotFocus (Index As Integer)

Chapter 7 - Visual Basic Schneider31 Creating Control Array at Run Time: You can create elements of a control array via Load statement at run time.

Chapter 7 - Visual Basic Schneider32 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.

Chapter 7 - Visual Basic Schneider33 The location and size of a control

34 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

Chapter 7 - Visual Basic Schneider35 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.

Chapter 7 - Visual Basic Schneider36 Types of Sorting Bubble Sort Shell Sort

Chapter 7 - Visual Basic Schneider37 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.

Chapter 7 - Visual Basic Schneider38 Bubble Sort For passNum = 1 To 4 'Number of passes is 1 less than number of items For index = 1 To 5 - passNum If name(index) > name(index + 1) Then Call SwapData( name( ), index) End If Next index Next passNum

Chapter 7 - Visual Basic Schneider39 Swapping two variables Private Sub SwapData ( A ( ) As String, index As Integer) Dim temp As String temp = A ( index) A(index ) = A (index + 1) A ( index + 1) = temp End Sub

Chapter 7 - Visual Basic Schneider40 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.

Chapter 7 - Visual Basic Schneider41 Shell Sort Is similar to the bubble sort, but instead of comparing and swapping adjacent elements A(count) and A(count+1), it compares and swaps nonadjacent elements A(count) and A(count + Gap), where Gap starts considerably greater than 1.

Chapter 7 - Visual Basic Schneider42 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.

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

Chapter 7 - Visual Basic Schneider44 Efficiency of Bubble and Shell sort Array Elements Bubble Sort Shell Sort ,75022,517

Chapter 7 - Visual Basic Schneider45 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.

Chapter 7 - Visual Basic Schneider46 Declaration of Two-Dimensional Array Syntax: –Dim arrayname (m1 To n1, m2 To n2) As vartype Example: – Dim rm (1 To 4, 1 To 4) As Single column row

Chapter 7 - Visual Basic Schneider47 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)

Chapter 7 - Visual Basic Schneider48 Example of Input File 0, 2054, 802, , 0, 2786, , 2786, 0, , 2706, 100, 0

Chapter 7 - Visual Basic Schneider49 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

Chapter 7 - Visual Basic Schneider50 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