CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.

Slides:



Advertisements
Similar presentations
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Advertisements

Introduction to arrays
Chapter 9: Advanced Array Manipulation
An Introduction to Programming with C++ Fifth Edition
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.
Selection Sort, Insertion Sort, Bubble, & Shellsort
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.
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.
Insertion Sort & Shellsort By: Andy Le CS146 – Dr. Sin Min Lee Spring 2004.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
IE 212: Computational Methods for Industrial Engineering
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
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 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
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 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Chapter 6: Arrays: Lists and Tables
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
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.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
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.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
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 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
 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.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Computer Programming BCT 1113
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Visit for more Learning Resources
Data Structures (CS212D) Week # 2: Arrays.
Introduction to Computer Programming IT-104
Insertion and Shell Sorts
Presentation transcript:

CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1

CONTENTS Single Dimension Arrays Declaring Arrays Arrays Manipulation Sorting Arrays: Bubble Sort, Shell Sort Searching Arrays: Linear Search © Prepared By: Razif Razali 2

INTRODUCTION By definition, an array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item. For example, if we need to enter one hundred names, we might have difficulty in declaring 100 different names, this is a waste of time and efforts. So, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item, for example name(1), name(2),name(3) etc., which will make declaring variables streamline and much systematic. © Prepared By: Razif Razali 3

DIMENSION OF ARRAY An array can be one dimensional or multidimensional. One dimensional array is like a list of items or a table that consists of one row of items or one column of items. A two dimensional array will be a table of items that make up of rows and columns. While the format for a one dimensional array is ArrayName(x), the format for a two dimensional array is ArrayName(x,y) while a three dimensional array is ArrayName(x,y,z). Normally it is sufficient to use one dimensional and two dimensional array,you only need to use higher dimensional arrays if you need with engineering problems or even some accounting problems. © Prepared By: Razif Razali 4

Example One dimensional Array Two Dimensional Array © Prepared By: Razif Razali 5 Student Name Name (1)Name (2)Name (3)Name (4)Name (5) Name (1,1)Name (1,2)Name (1,3)Name (1,4) Name (2,1)Name (2,2)Name (2,3)Name (2,4) Name (3,1)Name (3,2)Name (3,3)Name (3,4)

Declaring Arrays We could use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure. The general format to declare a one dimensional array is as follow: Dim arrayName(subs) as dataType where subs indicates the last subscript in the array. © Prepared By: Razif Razali 6

Example: Declaring an array Dim CusName(10) as String Will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CusName(1) to CusName(10). Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10) © Prepared By: Razif Razali 7 CusN ame (1) CusN ame (2) CusN ame (3) CusN ame (4) CusN ame (5) CusN ame (6) CusN ame (7) CusN ame (8) CusN ame (9) CusN ame (10)

Example Dim Count(100 to 500) as Integer Declares an array that consists of the first element starting from Count(100) and ends at Count(500) Eg. 4 student results stored in an array Dim result(1 TO 4)as Single Array result has 4 elements, each of single type © Prepared By: Razif Razali 8

Example Dim studentName(10) As String Dim num As Integer Private Sub cmdStart_Click() For num = 1 To 10 studentName(num) = InputBox("Enter the student name", "Enter Name", "", 1500, 4500) If studentName(num) <> "" Then Form1.Print studentName(num) Else End End If Next End Sub © Prepared By: Razif Razali 9

Output The above program accepts data entry through an input box and displays the entries in the form itself. As you can see, this program will only allows a user to enter 10 names each time he click on the start button. © Prepared By: Razif Razali 10

Example © Prepared By: Razif Razali 11

© Prepared By: Razif Razali 12

Multidimensional Arrays Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two indexes. The first (by convention) identifies the element's row and the second (by convention) identifies the element's column. Tables or arrays that require two indexes to identify a particular element are called two dimensional arrays. Note that multidimensional arrays can have more than two dimensions. Visual Basic supports at least 60 array dimensions, but most people will need to use more than two or three dimensional-arrays. © Prepared By: Razif Razali 13

Multidimensional Arrays The following statement declares a two-dimensional array 50 by 50 array within a procedure. Dim AvgMarks ( 50, 50) As Integer It is also possible to define the lower limits for one or both the dimensions as for fixed size arrays. An example for this is given here. Dim Marks ( 101 To 200, 1 To 100) As Integer An example for three dimensional-array with defined lower limits is given below. Dim Details( 101 To 200, 1 To 100, 1 To 100) As Integer © Prepared By: Razif Razali 14

Two Dimensional Array Some arrays have two dimensions, such as the number of offices on each floor of each building on a campus. The specification of an element requires both the building number and the floor, and each element holds the count for that combination of building and floor. Therefore, such an array uses two indexes. The following example declares a variable to hold a two- dimensional array of office counts, for buildings 0 through 40 and floors 0 through 5. Dim officeCounts(40, 5) As Byte A two-dimensional array is also called a rectangular array. © Prepared By: Razif Razali 15

SORTING Sorting is any process of arranging items in some sequence and/or in different sets, and accordingly, it has two common, yet distinct meanings: ◦ Ordering: arranging items of the same kind, class, nature, etc. in some ordered sequence, ◦ Categorizing: grouping and labeling items with similar properties together (by sorts). Sorting is an operation that you often perform on arrays. As you probably know, there are dozens of different sort algorithms, each one with its strengths and weaknesses. © Prepared By: Razif Razali 16

Why we do sorting? Commonly encountered programming task in computing. Examples of sorting: ◦ List containing exam scores sorted from Lowest to Highest or from Highest to Lowest ◦ List containing words that were misspelled and be listed in alphabetical order. ◦ List of student records and sorted by student number or alphabetically by first or last name.

BUBBLE SORT Bubble sort is a straightforward and simplistic method of sorting data that is used in computer science education. The algorithm starts at the beginning of the data set. It compares the first two elements, and if the first is greater than the second, then it swaps them. It continues doing this for each pair of adjacent elements to the end of the data set. It then starts again with the first two elements, repeating until no swaps have occurred on the last pass. This algorithm is highly inefficient, and is rarely used, except as a simplistic example. © Prepared By: Razif Razali 18

Example of Bubble Sort Have a look at Bubble SortBubble Sort URL : ples/sort/Sort2-E.html © Prepared By: Razif Razali 19

Shell sort Founded by Donald Shell and named the sorting algorithm after himself in st algorithm to break the quadratic time barrier but few years later, a sub quadratic time bound was proven Shell sort works by comparing elements that are distant rather than adjacent elements in an array or list where adjacent elements are compared.

Shell sort Shell sort uses a sequence h 1, h 2, …, h t called the increment sequence. Any increment sequence is fine as long as h 1 = 1 and some other choices are better than others.

Shell sort Shell sort makes multiple passes through a list and sorts a number of equally sized sets using the insertion sort. Shell sort improves on the efficiency of insertion sort by quickly shifting values to their destination.

Searching – Linear Search In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists in checking every one of its elements, one at a time and in sequence, until the desired one is found. Linear search is the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for. Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) may be much more efficient. © Prepared By: Razif Razali 23

Linear Search Example Linear Search animation Linear Search © Prepared By: Razif Razali 24