Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.

Slides:



Advertisements
Similar presentations
CISC Data Structures Ben Perry University of Delaware Summer 2011.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Compiler Construction
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ITEC200 – Week03 Inheritance and Class Hierarchies.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Programming with Recursion
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Templates Containers Iterators (TIC++V1:C16) Yingcai Xiao 06/26/2008.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Names Variables Type Checking Strong Typing Type Compatibility 1.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Basic Semantics Associating meaning with language entities.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
CS212: Object Oriented Analysis and Design
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Names, Scope, and Bindings Programming Languages and Paradigms.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Learners Support Publications Constructors and Destructors.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Constructors and Destructors
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Data Types In Text: Chapter 6.
Functions.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Chapter 5 Function Basics
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.
Types of Programming Languages
CS212: Object Oriented Analysis and Design
Extending Classes.
Constructors and Destructors
Compiler Construction
Java Programming Language
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Types(2)

2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined in terms of problems that are closer to the simple case  By applying this redefinition process every time the recursive function is called, eventually the problem is reduced entirely to simple csaes, which are relatively easy to solve. if this is a simple case solve it else redefine the problem using recursion

3 Example int multiply (int m, int n) { int ans; if (n==1) ans = m; else ans = m + multiply ( m, n-1 ) return (ans); }

4 Tracing multiply Function m is 6 n is 3 3==1 false multiply (6,2) ans is 6 + Return (ans) m is 6 n is 2 2==1 false multiply (6,1) ans is 6 + Return (ans) m is 6 n is 1 1==1 is true ans is 6 Return (ans) multiply (6,3)

5 Recursive Data Type (1)  Recursive data type uses itself in its declaration  Recursive data types are important to recursive algorithms  Used to represent data whose size and structure is not known in advance  Examples: lists and trees

6 Recursive Data Type (2) Struct CharList { char data; Sturct CharList next; /* illegal C*/ };  Needs a unlimited storage space (illegal C)  In the analogy with recursive functions Int factorial (int n) { return n * factorial (n-1); }/* illegal C*/  No base case (test to stop the recursion)

7 Recursive Data Type (3)  The solution: using pointers to allow manual dynamic allocation Struct CharListNode { char data; Sturct CharListNode *next; /* legal C*/ };  Each individual element in a CharListNode has a fixed size  Elements can be linked together to form a list of arbitrary size

8 Type Equivalence  The assignment statement serves to replace the current value of a variable with a new value specified as an expression. The variable (or the function) and the expression must be of identical type.  Name Equivalence Two types are equivalent if they have the same name  Structure Equivalence Two types are equivalent if they have the same structure

9 Example:  Name equivalent c and d have the same type a and c haven’t the same type  Structure equivalent a, b, c, d have the same type d and e haven’t the same type  Field names are different struct complex { float re, im; }; struct polar { float x, y; }; struct y { float re, im; }; struct complex c, d; Struct y a, b; struct polar e; int f[5], g[10];

10 Subtypes  A subtype is a type that has certain constraints placed on its values or operations.

11 Ada Example subtype one_to_ten is Integer range ; type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Weekend is Day range Saturday.. Sunday; type Salary is delta 0.01 digits 9 range _999_999.99; subtype Author_Salary is Salary digits 5 range ;

12 Java example  An object s of class S can be assigned to object t of class T  t = s Either S and T are the same class Or S is a subclass of T Widening Conversion

13 Polymorphism and Generics (1)  Polymorphism Comes from Greek Means: having many forms  A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result  Operators overloading is a kind of polymorphism  An advantage of polymorphism is that it enables code reuse.

14 Polymorphism and Generics (2)  To enables code reuse Ada introduced the concept of generics or template  A generic function is a template that can be instantiated at a compile time with concrete types and operators Binding of arguments to a type is deferred from programming time to compile time.

15 Polymorphism and Generics (3)  C++ provides generics or templates  The implementation mechanism Actual parameters are textually substituted for generic parameters Resulting text is then compiled