Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore www.desiamore.com/ifm DeSiaMorewww.desiamore.com/ifm1.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Introduction to arrays
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
CS-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.
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 
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Working with Arrays in MATLAB
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.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
UNIT 10 Multidimensional Arrays.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Data Structure and Algorithms
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Data Structure and Algorithms
Numeric Arrays Numeric Arrays Chapter 4.
Data Structures and Abstract Data Types
PZ05A - Abstract data types
Introduction To Programming Information Technology , 1’st Semester
Abstract data types Programming Language Design and Implementation
Arrays Week 2.
Abstract Data Types, Elementary Data Structures and Arrays
Abstract data types Programming Language Design and Implementation
A type is a collection of values
Presentation transcript:

Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1

Function for Deleting Array void array::del (int pos) { //skip to the desired position for (int i=pos; i<MAX; i++) arr[i-1] = arr[i]; arr[i-1]=0; } DeSiaMore 2

Displaying the array void array::display () { cout<<endl; //Traverse the entire array for (int i = 0; I < MAX; i++) cout<<“ “<<arr[i]; } DeSiaMore 3

A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order to do that, we need to identify: 1. The collection of data items 2. Basic operation that must be performed on them Abstract Data Type (ADT): a collection of data items together with the operations on the data DeSiaMore 4

Abstract Data Type (ADT) The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented. We think about what can be done with the data, not how it is done DeSiaMore 5

DeSiaMore 6

Types A type (data type) is a set of values that an object can have. An abstract data type (ADT) is a:  data type  Set of functions (operations) that operates on data of that type  Each function is defined by its signature.  Can also specify operations formally (see section 4.2.5) (Algebraic data types): f(g(A,S)) = A DeSiaMore 7

Example ADT ADT student: Operations: SetName: name x student  student GetName: student  name SetCourse: course x student  student GetCourse: student  course * GetCredits: student  integer In this example, Name and Course are undefined ADTs. They are defined by some other abstraction. DeSiaMore 8

Real world ADT example DeSiaMore 9

Implementation of ADT An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation DeSiaMore 10

Data Structures, Abstract Data Types, and Implementations in Real World Consider example of an airplane flight with 10 seats to be assigned Tasks  List available seats  Reserve a seat How to store, access data? DeSiaMore 11

Data Structures, Abstract Data Types, and Implementations Consider example of an airplane flight with 10 seats to be assigned Tasks  List available seats  Reserve a seat How to store, access data?  10 individual variables DeSiaMore 12

Use 10 individual variables Algorithm to List available seats 1. If seat1 == ‘ ’: display 1 2. If seat2 == ‘ ’: display If seat10 == ‘ ’: display 10 Algorithm to Reserve a seat 1. Set DONE to false 2. If seat1 ==‘ ’: print “do you want seat #1??” Get answer if answer==‘Y’: set seat1 to ‘X’ set Done to True 3. If seat2 ==‘ ’: print “do you want seat #2??” Get answer if answer==‘Y’: set seat2 to ‘X’ set Done to True. DeSiaMore 13

Data Structures, Abstract Data Types, and Implementations Consider example of an airplane flight with 10 seats to be assigned Tasks  List available seats  Reserve a seat How to store, access data?  10 individual variables  An array of variables DeSiaMore 14

Use Array Algorithm to List available seats For number ranging from 0 to max_seats-1, do: If seat[number] == ‘ ’: Display number Algorithm to Reserve a seat Readin number of seat to be reserved If seat[number] is equal to ‘ ’: set seat[number] to ‘X’ Else Display a message that the seat having this number is occupied DeSiaMore 15

ADTs In this simple example, it does illustrate the concept of an Abstract Data Type ADT consists of 1. The collection of data items 2. Basic operation that must be performed on them In the example, a collection of data is a list of seats The basic operations are (1) Scan the list to determine which seats are occupied (2) change seat’s status DeSiaMore 16

Data Structure and Abstract Data Type The term of Data Structure and Abstract Data Type are often used interchangeably However, we use ADT when data is studied at a logical level The term data structure refers to a construct in programming language that can be used to store data DeSiaMore 17

Array ADT Collection of data elements  A fixed-size sequence of elements, all of the same type Basic Operations  Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position DeSiaMore 18

Different types of Array One-dimensional array: only one index is used Multi-dimensional array: array involving more than one index Static array: the compiler determines how memory will be allocated for the array Dynamic array: memory allocation takes place during execution DeSiaMore 19

Array and ADT Collection of data elements  A fixed-size sequence of elements, all of the same type Basic Operations  Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position An Array as an ADT C++ Array Fixed size specify the capacity of the array Ordered indices are numbered 0,1,2,…,capacity-1 Same type of elements specify the element type Direct access subscript operator[] DeSiaMore 20

Multidimensional Arrays… 2D- Arrays A two dimensional array is a collection of elements placed in m rows and n columns. The syntax of 2-D array includes two subscripts, of which one represents number of rows and the other specifies the number of columns of an array These two subscripts are used to reference an element in an array. For example arr[3][4] is a 2-D array containing 3 rows and 4 columns. DeSiaMore 21

Multidimensional Arrays… 2D-Arrays Arr[0][2] is an element placed at 0 th row and 2 nd column in the array. The two dimensional array is also called a matrix. Representation of a 2-D array DeSiaMore 22

Multidimensional Arrays … 2D- Arrays Consider a table of test scores for several different students DeSiaMore 23

Row Major and Column Major Arrangement Rows and columns of matrix are imaginary. Matrix are get stored in memory linearly as computer memory can be viewed as consecutive units of memory. This lead to two major arrangements in memory:  Row Major arrangement  Column major arrangement Consider an example below DeSiaMore 24

Row Major and Column Major Arrangement int a[3][4] = { { 12,1,-9,23} { 14,7,11,121} { 6,78,15,34} } DeSiaMore 25

Row Major and Column Major Arrangement Since the array elements are stored in adjacent memory locations we can access any elements of the array one we know the base address (starting address) of the array and number of rows and columns present in the array. Example if the base address of the array above is 502 and we wish to refer the element 121, then the calculation involved would be as follows: DeSiaMore 26

Row Major and Column Major Arrangement Row Major arrangement: Element 121 is present at a[1][3]. Hence location of 121 would be = *4+3 = = 516 In general for an array a[m][n] the address of element a[i][j] would be: Base address + i*n+j DeSiaMore 27

Row Major and Column Major Arrangement Column Major arrangement: Element 121 is present at a[1][3]. Hence location of 121 would be: = *3 + 1 = = 522 Generally for an address of an array a[m][n] the address of element a[i][j] would be DeSiaMore 28

Row Major and Column Major Arrangement Base address + j*m+i. Note the C++ permits only a Row Major arrangement, whereas, Pascal uses a Column Major Arrangement. DeSiaMore 29