Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.

Slides:



Advertisements
Similar presentations
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
© Dr. Alaaeldin Amin 1 Type Compatibility & conversion VHDL is a Strongly-Typed Language. Compiler Flags an Error Whenever Different Types are Mixed. Subtypes.
Pascal Programming Pascal Units, Abstract Data, Ordinals, Arrays.
FOR DOWNTO Suppose you want to write a FOR-DO loop where the control variable is decreased with each repetition of the loop. Pascal provides the reserved.
Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CPS120: Introduction to Computer Science
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Pascal CourseWinter 2010/111. Introduction Imperative and procedural programming language Designed: 1968/9 Published: 1970 Static and strong typing.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
1 CS161 Introduction to Computer Science Topic #9.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CPS120: Introduction to Computer Science Variables and Constants.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
A Simple Program CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Chapter 2 – part b Brent M. Dingle Texas A&M University
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 7: User-Defined Functions II
11 Chapter Structured Data
Computing and Statistical Data Analysis Lecture 2
Haskell Chapter 2.
The CONST definition CONST Pi = , City = ‘New York’;
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
Pascal Winter 2010/ Course.
(c)opyright Brent M. Dingle 2001
One-Dimensional Array Introduction Lesson xx
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Chapter 11: Structured Data.
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Data Structures and Algorithms Introduction to Pointers
Primitive Types and Expressions
Standard Version of Starting Out with C++, 4th Edition
(c)opyright Brent M. Dingle 2001
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Variables in C Topics Naming Variables Declaring Variables
Complex Array Structures
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Variables and Constants
Presentation transcript:

Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

Fall 2001(c)opyright Brent M. Dingle 2001 Known Data Types So far we have seen several predefined data types in Pascal: char integer real boolean others But what if we want to represent something that is none of those?

Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types Notice that while we understand how to use types char, int, real and boolean we don’t know how things “really” work. A data type consists of a set of values together with a set of operations defined on these values. A data type is called an abstract data type if the programmer who is using the type does not have access to the details of how the operations are implemented (or does not (re)write them for the program in which they are used).

Fall 2001(c)opyright Brent M. Dingle 2001 TYPE Declaration and format In Pascal the TYPE declaration allows you to declare new data types. Type declarations are of the format: TYPE [type_name 1] = [type definition 1]; [type_name 2] = [type definition 2]; : : : : [type_name n] = [type definition n];

Fall 2001(c)opyright Brent M. Dingle 2001 Order of Declarations So in a program the order of declarations becomes: 1. Constants (CONST) 2. Types (TYPE) 3. Variables (VAR) 4. Procedures and Functions

Fall 2001(c)opyright Brent M. Dingle 2001 Program Layout And the program layout becomes Program [Name]; CONST { constants declared here } TYPE { types declared here } VAR { variables declared here } { Procedures and Functions declared here } BEGIN [main body] END.

Fall 2001(c)opyright Brent M. Dingle 2001 Subrange Types The simplest ‘new’ type is a subrange. Subranges are intervals of already defined types in which the type members can be counted. An example of a valid subrange is: the numbers from 4 to 10 are a subrange of integers. i.e. TYPE SHORT_RANGE = Note: you canNOT have a subrange of things like type real – reals are not a countable set.

Fall 2001(c)opyright Brent M. Dingle 2001 Subrange Type (cont) Say you knew, for some reason, that you only wanted to use the numbers from –5 to 15 and you needed a variable named score to handle this. You would declare things as follows: PROGRAM la; TYPE ODD_RANGE = ; VAR odd_score : ODD_RANGE; score : integer; :

Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – host type In the above example type integer would be the host type of the subrange ODD_RANGE.

Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – compatibility A variable of the host type may be assigned a value of the subrange type. e.g. score := odd_score; is a valid statement A subrange type should NOT be assigned a value outside its range. e.g. odd_score := 342; is NOT a valid statement Caution: odd_score := score; will compile but if score’s value is not in [-5, 15] an error may unexpectedly occur.

Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – Another Example You can also do subranges on char- the number of chars is countable. For example all capital letters: PROGRAM la; TYPE CAP_LETTERS = ‘A’..’Z’; VAR letter: CAP_LETTERS; any_char: char;

Fall 2001(c)opyright Brent M. Dingle 2001 Task (not graded) Read the case study on page 296 – 305 Understand how they broke the task into subtasks and how they created their algorithm and how they implemented their algorithm  using ADT’s,  procedures,  functions and  units. Try entering the program and running it.

Fall 2001(c)opyright Brent M. Dingle 2001 Ordinal Types Types whose values can be specified by a list are ordinal types. These have also been called scalar types and defined as a data type containing a finite number of elements. Examples: integer char boolean PRIME_COLORS = (red, green, blue)

Fall 2001(c)opyright Brent M. Dingle 2001 Enumerated Types An enumerated type is just a list of identifiers. Enumerated types are user defined ordinal types. The values of an enumerated type have no properties other than their order and their names Their proper use can make programs easier to read.

Fall 2001(c)opyright Brent M. Dingle 2001 Enumerated Type (example) PROGRAM Rainbow; TYPE COLORS = (Red, Orange, Yellow, Green, Blue, Indigo, Violet); VAR color : COLORS; BEGIN color := Yellow; color := Violet; IF (color = Violet) THEN BEGIN Writeln(‘Color is violet.’); END; END.

Fall 2001(c)opyright Brent M. Dingle 2001 Type Casting Notice since there are 7 colors you can type cast the integers 0 to 6 to a color. For example if you said: color := COLORS(2);  color would be set = yellow. color := COLORS(0);  sets color = red. color := COLORS(42);  produces an error as 42 is out of the range of COLORS

Fall 2001(c)opyright Brent M. Dingle 2001 Pred and Succ Functions Remember the functions Pred and Succ? Since Enumerated types have an order, these functions automatically work on them. Some things to watch out for: if you try to do succ(violet) you should get a compile time error – out of range. If you ‘accidentally’ try to take the succ(color) when color’s value is violet you may get an unexpected result at runtime.

Fall 2001(c)opyright Brent M. Dingle 2001 Ord and Chr Ord will work and can be useful on enumerated types. Chr really doesn’t do much for enumerated types, though when converting from type char to type integer and back again it comes in useful.

Fall 2001(c)opyright Brent M. Dingle 2001 Problems (not graded) page , 12, 13 pages 320 – 323, programming 16, 20, 21, 22 Also suggested would be to look at the sample programs: Rainbow.pas Rainbow2.pas

Fall 2001(c)opyright Brent M. Dingle 2001 End ADTs Chapter 8.2, 8.3 now complete. And so ends Chapter 8