Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
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.
The new features of Fortran 2003 David Muxworthy BSI Fortran Convenor.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Fortran: Array Features Session Five ICoCSIS. Outline 1.Zero-sized Array 2.Assumed-shaped Array 3.Automatic Objects 4.Allocation of Data 5.Elemental Operations.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Functions & Subroutines HTML version DFMain.chm. Types of subprograms Internal External Module Pure Elemental Recursive Optional arguments Generic Defined.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
Chapter 10 Modules and programming with subroutines.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 9 Modules and Programming with Functions.
The Programming Discipline Professor Stephen K. Kwan Things you need to know (learn) for developing large computer programs.
1 Gentle Introduction to Programming Session 4: Arrays.
analysis, plug ‘n’ chug, & induction
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)
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.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Fortran: Specification Statements Session Six ICoCSIS.
Fortran: Program Units and Procedures Session Four ICoCSIS.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Programming Pillars Introduction to Object- Oriented Programming.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Week 8 Improving on building blocks Recursive procedures.
 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.
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.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
Extensions to I/O Tom Clune SIVO Fortran 2003 Series March 11, 2008.
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.
1 MT258 Computer Programming and Problem Solving Unit 4.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Compilation Technology SCINET compiler workshop | February 17-18, 2009 © 2009 IBM Corporation Software Group Fortran 2003 Jim Xia IBM Toronto Lab
11/10/2015.  Copy these definitions into your notes: 1. Rational number: Any number that can be put into the form of a fraction. 2. Irrational number:
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
Structures or Derived Types. Real Life Objects Often structured –Students, Employees, Cars, Accounts, Cricket matches, flats etc contain heterogeneous.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Functions What is a function –“sub” program, with an isolated set of statements –Accepts parameters, returns a result (perhaps) –Think of it as its own.
Programming Fundamentals Enumerations and Functions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Deitel- C:How to Program (5ed)
Array processing and Matrix manipulation
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
CS302 - Data Structures using C++
Array processing and Matrix manipulation
CPS125.
Introducing Modularity
Presentation transcript:

Week 8

Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3

What is recursion? 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

Solution 1 n 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

Solution 2 n 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

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 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

Derived data types type, public :: new_type component_definition... end type new_type

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(person) :: ali, mukaddes, veli

Derived types vs. arrays A

Be careful... n Declarations 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 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 n 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)

Assignment due on April 22 n Problem 8.3 from Ellis & Philips n Make sure to fully document your programs!