CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays

Slides:



Advertisements
Similar presentations
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Advertisements

Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
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.
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
The University of Texas – Pan American
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
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 Group of variables that have a similar type
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
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 More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
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.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Arrays 1.
CE En 270 Brigham Young University Norm Jones
IndexedListWithIteratorsViaLinear1Ind
Searching Arrays Linear search Binary search small arrays
Chapter 11 - JavaScript: Arrays
Review Array Array Elements Accessing array elements
Arrays Chapter 7.
Arrays.
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 7: Working with Arrays
Visual Basic 2010 How to Program
Single Dimensional Arrays
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Chapter 7 Arrays.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
CSCI 3327 Visual Basic Chapter 7: Arrays
Introduction to Programming
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
CSCI 3327 Visual Basic Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3327 Visual Basic Review: Exam I
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CIS16 Application Development and Programming using Visual Basic.net
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Random Access Files Dr. John Abraham.
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous lecture slides. – Xiang Lian

Objectives In this chapter, you will: Learn sorting data in arrays Sort method of arrays Linear search Binary search Learn the declaration of rectangular arrays Know how to resize an array in Visual Basic

In the Last Class: Example of an Array Array: C Dim C = New Integer(11) C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33

Sort an Array C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 C(0) -1345 C(1) -45 C(2) C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939

Class Array Array is a class in namespace System Method for sorting: Array.Sort(arrayName)

Example 7.12: SortArray.vb URL: ListBox Button http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html ListBox OriginalValuesListBox.Items.Clear() Button sortButton.Enabled = True Array.Sort(integerArray)

Sorting and Searching Sorting implementation You need to implement it Don’t use the method Sort comes with array class You may use bubble sort or selection sort Assume we have an array: CustomerName

Bubble Sort Dim sorted As Boolean = False Dim j As Integer = CustomerName.GetUpperBound(0) While Not (sorted) sorted = True For i = 0 To j - 1 If CustomerName(i) > CustomerName(i + 1) Then Swap(i) sorted = False End If Next i j = j - 1 End While

Swap Private Sub Swap(ByVal i As Integer) Dim tmp1 As String tmp1 = CustomerName(i) CustomerName(i) = CustomerName(i + 1) CustomerName(i + 1) = tmp1 tmp2 = CustomerTele(i) CustomerTele(i) = CustomerTele(i + 1) CustomerTele(i + 1) = tmp2 End Sub

Searching Searching: to determine whether a value (called search key) is in the array Linear search Compare each element of the array with the search key For either sorted or unsorted array Efficient for small array Binary search For sorted array

Linear Search search key C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33

Example 7.14: LinearSearchTest.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html Code: If searchData(i) = searchKey Then index = i Exit For 'terminate the loop, as the key is found End If

Binary Search first search key: 10 middle last C(0) -1345 C(1) -45 C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

Binary Search first search key: 10 middle last C(0) -1345 C(1) -45 C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

Binary Search first search key: 10 middle last C(0) -1345 C(1) -45 C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

Binary Search first search key: 10 middle last C(0) -1345 C(1) -45 C(3) 6 C(4) 10 C(5) 33 C(6) 34 C(7) 39 C(8) 40 C(9) 72 C(10) 98 C(11) 939 first search key: 10 middle last

Code of Binary Search Dim Lookfor As String Dim first, last, middle As Integer Dim nfound As Boolean LblName.Caption = "": LblTele.Caption = "" Lookfor = TxtSearch.Text nfound = False first = 1: last = Index While (Not (nfound) And first <= last) middle = (first + last) \ 2 If RTrim(Lookfor) = RTrim(CustomerName(middle)) Then nfound = True LblName.Caption = CustomerName(middle) LblTele.Caption = CustomerTele(middle) ElseIf Lookfor < CustomerName(middle) Then last = middle - 1 Else: first = middle + 1 End If End While If Not (nfound) Then LblName.Caption = "Name not in list"

Using an Array for a Stack Keep track of the top Push: Increase it before adding Pop: Decrease it after taking Push or Pop only to or from the top Examine the stack to see if it is empty or full For example, if stack.top < 0, then it is empty You may choose not to use the 0th location for data, then you can say it is empty when stack.top is zero

Rectangular Array So far, we have studied one-dimensional array Dim C = New Integer (11) Rectangular array is a 2-dimensional array Column 0 Column 1 Column 2 Column 3 a(0, 0) a(0, 1) a(0, 2) a(0, 3) a(1, 0) a(1, 1) a(1, 2) a(1, 3) a(2, 0) a(2, 1) a(2, 2) a(2, 3) Row 0 Row 1 Row 2

Declaration of Rectangular Array Create a 2-by-2 array Dim numbers (1, 1) As Integer Initialize values numbers (0, 0) = 1 numbers (0, 1) = 2 numbers (1, 0) = 3 numbers (1, 1) = 4 Declare and initialize a rectangular array Dim numbers = {{1, 2}, {3, 4}} Dim numbers(,) As Integer = {{1, 2}, {3, 4}}

Case Study: Grade Report Using a Rectangular Array Example 7.20: GradeReport.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

Resize an Array Keyword: ReDim An array's size cannot be changed But a new array can be created and assigned to an array variable Example: Dim values() As Integer = {1, 2, 3, 4, 5} ReDim values(6) ' {0, 0, 0, 0, 0, 0, 0} ReDim Preserve values(6) ' {1, 2, 3, 4, 5, 0, 0}