Week 8 Improving on building blocks Recursive procedures.

Slides:



Advertisements
Similar presentations
Exposure C++ Chapter XVI C++ Data Structures, the Record.
Advertisements

22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
Programmer’s Guide to F Structures and Derived Datatypes.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
COMPSCI 105 S Principles of Computer Science Recursion 1.
VBA Modules, Functions, Variables, and Constants
Recursion. Binary search example postponed to end of lecture.
Functions & Subroutines HTML version DFMain.chm. Types of subprograms Internal External Module Pure Elemental Recursive Optional arguments Generic Defined.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion.
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Chapter 10 Modules and programming with subroutines.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 9 Modules and Programming with Functions.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
1 Gentle Introduction to Programming Session 4: Arrays.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Intermediate PHP (2) File Input/Output & User Defined Functions.
Fortran: Specification Statements Session Six ICoCSIS.
Fortran: Program Units and Procedures Session Four ICoCSIS.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
A First Book of ANSI C Fourth Edition
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Week 3 Let's review! Fundamental data types List-directed input/output.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Structures or Derived Types. Real Life Objects Often structured –Students, Employees, Cars, Accounts, Cricket matches, flats etc contain heterogeneous.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Array processing and Matrix manipulation
Functions Declarations CSCI 230
VISUAL BASIC.
Introduction To Programming Information Technology , 1’st Semester
CS302 - Data Structures using C++
Array processing and Matrix manipulation
Introducing Modularity
Presentation transcript:

Week 8 Improving on building blocks Recursive procedures

What is recursion? n Recursion is an important mathematical concept. n Recursion in programming is a technique for defining a problem in terms of one or more smaller versions of the same problem. n The solution to the problem is built on the result(s) from the smaller version(s).

What is recursion? n Best known example: the factorial n! = n (n-1)! 0! = 1 n That’s all you need to calculate n!

Recursive procedures n recursive function function_name(…) result(result) n Let’s use it to calculate n! n Structure plan n = 0 factorial_n = 1 n > 0 factorial_n = n * factorial(n-1) n < 0 Error! Return factorial_n = 0

Example ! Factorial,function to calculate factorials recursively !* accepts : integer n => 0 !* returns : n ! function factorial (n) result (n) integer : : fact, i integer, intent (in) : : n fact = 1 do i = 2, n fact = fact * i end do end function factorial

Example ! Factorial, function to calculate factorials recursively !* accepts : integer n => 0 !* returns : n ! recursive function factorial (n) result (n) integer : : fact integer, intent (in) : : n if ( n == 0 ) then fact = 1 else fact = n * factorial (n-1) end if end function factorial

Solution 1 recursive function factorial(n) result(factorial_n) ! Dummy argument and result variable integer, intent(in) :: n real :: factorial_n ! Determine whether further recursion is required select case(n) case(0) ! Recursion has reached the end factorial_n = 1.0 case(1:) ! More recursive calculation(s) required factorial_n = n*factorial(n-1) case default ! n is negative - return zero as an error indicator factorial_n = 0.0 end function factorial Write a function for n! Using “select case” structure.

Solution 2 recursive subroutine factorial(n, factorial_n) ! Dummy arguments integer, intent(in) :: n real, intent(out) :: factorial_n ! Determine whether further recursion is required select case(n) case(0) ! Recursion has reached the end factorial_n = 1.0 case(1:) ! Recursive call(s) required ! to obtain (n-1)! call factorial(n-1, factorial_n) case default ! n is negative - return zero as an error indicator factorial_n = 0.0 end subroutine factorial Write a subroutine for n! Using “select case” structure.

Procedures as arguments Up to now, procedures had as dummy arguments real, integer, character, logical and arrays with these types n But, what about a procedure as a dummy argument?

How to declare a procedure? n We have to provide information concerning the interface of the procedure The interface block: interface interface_body end interface

How to declare a procedure? Example: interface function dummy_fun(a, b) result(r) real, intent(in) :: a, b real :: r end function dummy_fun end interface

How to declare a procedure? Example: interface subroutine one_arg(x) real, intent(inout) :: x end subroutine one_arg recursive subroutine two_args(x, y) real, intent(inout) :: x, y end subroutine two_args end interface

Couple of details... n Dummy procedure arguments in a function must be functions n All actual procedure arguments must be module procedures n It is not permissible for an intrinsic procedure to be an actual argument

Derived data types : Creating your own data n You can create your own data types! n They can be derived from: –Intrinsic data types: integer, real, character, logical –Previously defined data types F provides the programmers to create their own data type to supplement the above-given intrinsic types.

Derived data types and structures As mentioned before, arrays are used to store elements of the same type. Sometimes items needed to be processed, are not all of the same type. A data consisting a date for example, has two different types :  the month name of character type  the day and year of integer type Another example can be given a record (measurement) captured using a computer, might contain  an user’s name in character strings  a password in character strings  an identification number of integer type  captured record of real type

Derived data types and structures type, public :: new_type component_definition... end type new_type F provides a “derived data type” in order to declare a “structure” used to store items of different types. The position in the structure, in which these data items are stored, are called the “components of the structure”. It is possible to store a name component, a password component, etc. in a structure using the form of,

Example type, public :: person character(len=12) :: first_name character(len=1) :: middle_initial character(len=12) :: last_name integer :: age character(len=1) :: sex ! M or F character(len=5) :: tax_number end type person

Example Now you can declare variable of type person : type ( type­_name ) : : list_of_identifiers type(person) :: ali, mukaddes, veli

Be careful... n Declaratins need access to the type definition n Place definitions in a module

Putting data in a derived type variable ali = person("Ali","M","Aktepe",56,"M","45645") mukaddes = person("Mukaddes"," ","Has",18,"F","12345") veli = person("Veli","M","Karatepe",65,"M","34567") This is a structure constructor

Refering to components n variable%component structure_name % component_name Example: ali%last_name mukaddes%age veli%first_name

Using derived types to define new derived types n type, public :: employee type(person) :: employee character(len=20) department real :: salary end type employee n type(person) :: saadet n saadet%employee%age = 34

Arrays of derived types type(person), dimension(1:12) :: bil102class Then: bil102class(2)%sex = "M" You can do operations on components : bil102class(3)%age+bil102class(4)%age But no such operation is permissible: bil102class(3) - bil102class(4)

Example: Let's do some geometry! n Two derived types: –point –line n Two functions: –distinct_points(p1,p2) result(distinct) –line_from_points(p1,p2) result(join_line)

program example_8_1_1 !variable declaration real:: result integer :: n print *,"enter n value" read *, n !result = factorial(n) call factorials(n,result) print *,n,"!=", result end program example_8_1_1 recursive function factorial(n) result(factorial_n) !dummy argument and result variable integer, intent(in) :: n real :: factorial_n !determine whether further recursion is required select case (n) case(0) !recursion has reached the end factorial_n = 1.0 case (1:) !more recursive calculation(s) requires factorial_n = n*factorial(n-1) case default !n is negative - return zero as an error indicator factorial_n = 0.0 end select end function factorial

program example_8_1_2 !variable declaration real:: result integer :: n print *,"enter n value" read *, n call factorial(n,result) print *,n,"!=", result end program example_8_1_2 recursive subroutine factorial(n, factorial_n) !dummy argument and result variable integer, intent(in) :: n real, intent(out) :: factorial_n !determine whether further recursion is required select case (n) case(0) !recursion has reached the end factorial_n = 1.0 case (1:) !recursive call(s) required to obtain (n-1)! call factorial(n-1, factorial_n) factorial_n = n*factorial_n case default !n is negative - return zero as an error indicator factorial_n = 0.0 end select end subroutine factorial

Write a module that sorts given v vector in an ascending order