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.

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

Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
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
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Searching and Sorting Arrays
 2007 Pearson Education, Inc. All rights reserved C 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.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
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
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 8 Arrays and Strings
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
Arrays Why we need data structure? Simple data types use a single memory cell to store a variable. Sometimes (for example scores of a class) it is more.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9: Sorting and Searching Arrays
Chapter 7 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Starting Out with Programming Logic & Design
Introduction to Computer Programming IT-104
Presentation transcript:

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 Learn about sorting

Array verses Simple Variable Simple variable is used to store a single value. Ex:Dim X as Integer 0 X 0000 X(1) X(2) X(3) X(4) X Array variable is used to represent many values of the same type with one variable name. Ex: Dim X(1 to 4) as Integer

4 Elements of an Array Array Name: A valid variable name for the structure. (X) Subscript or Index : A value that refers to a particular array element. X(1) Element or Subscripted variable: An individual data item within an array. Ex: X(1), X(2) 0000 X(1) X(2) X(3) X(4) X

5 Array Grade() Dim Grade( 1 To 4) As Integer Grade(1 ) Array Name Index

6 Array Declaration Syntax Dim arrayName(m To n) As varType where m and n are integers Declaring arrays - specify: name type of array number of elements

7 Array Declaration Examples Dim month(1 To 5) As String Dim stdID(1 To 3) As Integer Month(1) Month(2)Month(3)Month(4)Month(5) 000 stdID(1) stdID(2)stdID(3) This statement creates array of 3 integer elements

8 The Dim Statement Used to declare an array A Dim statement must occur before the first reference to the array elements.

9 Initializing an Array Private Sub cmdExample_Click() Dim Grade(1 To 5) As Integer Grade (1) = 5 Grade (2) = 30 Grade (3) = 25 Grade (4) = 15 Print Grade(1) + Grade(3) Print Grade(4) Print Grade(1+2) End Sub Grade(1) Grade(2) Grade(3) Grade(4) Grade(5)

The statements Dim arrayName(0 to n) as VarType can be placed by the statement Dim arrayName(n) as VarType Ex. Dim X(0 To 3) As Integer Ex. Dim X(3) As Integer 0000 x(0) x(1)x(2)x(3) 0000 x(0) x(1)x(2)x(3)

Example Dim Y(-1 to 2) As Integer 0000 Y(-1) Y(0)Y(1)Y(2) Dim Z(-2 to 0) As Single 000 Z(-2) Z(-1)Z(0)

Example Dim Y(-1 to -1) As Integer 0 Y(-1) Dim Z(2 to 2) As Single 0 Z(2)

Example Dim Y(2) As Integer Dim Z(2 to 2) As Single 0 Z(2) 000 Y(0)Y(1)Y(2)

Example Dim Y(10 to 1) As Integer Compile ERROR Dim Z(2 to -2) As Single Compile ERROR

Example i = 5 Dim arr(i) As Integer Invalid statements

Example

m, n must be a constant

Chapter 7 - Visual Basic Schneider 19 Initializing an Array by Reading from a File Dim student (1 To 30) As String Dim count As Integer Open “STUDENTS.txt” For Input As #1 For count = 1 To 30 Input #1, student(count) Next count Ali Ahmed.. Huda STUDENTS.txt AliAhmed Huda Student 12 30

Chapter 7 - Visual Basic Schneider20 Adding Up Elements in an Array Dim score(1 To 30) As Single, student(1 To 30) As String Dim count As Integer, average as Integer, sum As Integer 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 AliAhmed Huda Student Score 1230 Ali 22 Ahmed 19.. Huda 25 Students.txt

21 Dim XY (1 To 5) As Integer Dim count As Integer count = 1 Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, XY(count) count= count + 1 Loop Example

22 Parallel Arrays Two arrays are said to be parallel if subscripted variables having the same subscript are related. AliAhmed Huda Student Score 1230 Ali 22 Ahmed 19.. Huda 25 Students.txt

23 Example of Parallel Arrays Dim nom(1 To 3) As String, score(1 To 3) As Integer Dim student As Integer Open “SCORE.TXT” For Input As #1 For student = 1 To 3 Input #1, nom(student), score(student) Next student Close #1

Form_Load() Use it to Initialize Form Level variables Use it to initialize Form Level Arrays Use it to Dim Dynamic Arrays Chapter 7 - Visual Basic Schneider24

25 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 x(0) x(1)x(2)x(3)

26 Passing an Array An array can be passed to another procedure (Only) by reference. the name of the array, followed by an empty set of parenthesis, must appear as an argument in calling statement, and an array variable name of the same type must appear as corresponding parameters in the procedure definition of the procedure that is to receiver the array Parenthesis are optional with the arguments

27 Example

30 Sorting A common practice involving arrays is to sort the elements of the array in either ascending or descending order. A sort is an algorithm for ordering an array Sorting techniques Bubble Sort Shell Sort Ascending : lowest to highest descending: highest to lowest

31 Bubble Sort The bubble sort involves comparing adjacent elements and swapping the values of those elements when they are out of order.

32 Shell Sort Similar to the bubble sort Instead of comparing and swapping adjacent elements A(count) and A(count+1), Shell sort compares and swaps non-adjacent elements A(count) and A(count + Gap), where Gap starts at roughly half the size of the array

33 Efficiency of Bubble and Shell sort (average number of comparisons) Array Elements Bubble Sort Shell Sort ,9502, ,75022,517

34 Searching Arrays The process of finding the position of a value in an array is called searching For example Note: search(x) means search about x Search(-10) = 3 Search (3) = 1 Search (5) = Not Found stdID(1) stdID(2)stdID(3)

35 Searching techniques A sequential search examines each element, beginning with the first, until the specified value is found or the end of the array is reached For example Search about number 4  Search(4) = stdID(1) stdID(2)stdID(3) 1014 stdID(4) stdID(5)stdID(6) Is 4 = 3: False Is 4 = 7: False Is 4 = -10: False Is 4 = 1: False Is 4 = 4: True The number of comparisons to find the value 4 = 5

36 Sequential Search Useful for small arrays. Very inefficient for large arrays (for example, names in a telephone book). For any size array, if the array is ordered, the more efficient binary search can be used.

37 Binary Search In a binary search, an ordered array is repeatedly divided in half. The half not containing the target value is ignored.

38 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.

39 Declaration of Two-Dimensional Array Syntax: Dim arrayName(m1 To m2, n1 To n2) As varType Example: Dim rm(1 To 4, 1 To 4) As Single column row

40 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

Examples: How many elements? Dim rm(0 To 3, 97 To 99) As Integer  12 elements

What is the output of: Dim (1 to 3, 1 to 3) as integer For i = 1 To 3 For j = 1 To 3 a(i, j) = i * j Print a(i, j); Next j Print Next i The output is

Examples: How many elements? Dim rm(1 To 4) As Single rm(1) rm(2)rm(3) 0 rm(4)  4 elements

Examples: How many elements? Dim arr(4, 1 To 4) As Single  20 elements

Examples: How many elements? Dim arr(-5 To -3, 5 To 5) As String  3 elements

Examples: How many elements? Dim xy(-3 To -5) As Integer 46  ERROR

Examples Dim arr(0 To 3, 97 To 99) As Integer arr(1,98)= 20 arr(3,99)=15 arr(0,2)  Error: index out of range arr(-1,99)  Error: index out of range

Examples Dim arr(0 To 3, 97 To 99) As Integer For i = 97 To 99 arr(1,i) = 10 Next i