11 - 2/4/2000AME 150 L Program Control Subroutines and Functions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Sequences and Mathematical Induction
A sequence is a set of numbers arranged in a definite order
Recursion. Binary search example postponed to end of lecture.
Discrete Structures Chapter 2 Part A Sequences Nurul Amelina Nasharuddin Multimedia Department.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS150 Introduction to Computer Science 1
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Chapter 4 Mathematical Induction Used to verify a property of a sequence 2,4,6,8,… for i >= 1 a i = 2i –infinite sequence with infinite distinct values.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
Kavita Hatwal Fall Sequences and Induction.
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
CMSC 250 Discrete Structures Summation: Sequences and Mathematical Induction.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
1 5.The Gamma Function (Factorial Function ) 5.1 Definition, Simple Properties At least three different, convenient definitions of the gamma function are.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
Special Sum The First N Integers. 9/9/2013 Sum of 1st N Integers 2 Sum of the First n Natural Numbers Consider Summation Notation ∑ k=1 n k =
Exponents and Polynomials
Chapter 11 Special functions
April 30 th copyright2009merrydavidson Happy Birthday to: 4/25 Lauren Cooper.
1 © 2010 Pearson Education, Inc. All rights reserved 10.1 DEFINITION OF A SEQUENCE An infinite sequence is a function whose domain is the set of positive.
Sequences & Summations CS 1050 Rosen 3.2. Sequence A sequence is a discrete structure used to represent an ordered list. A sequence is a function from.
Sequences Informally, a sequence is a set of elements written in a row. – This concept is represented in CS using one- dimensional arrays The goal of mathematics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Fundamentals of C and C++ Programming Control Structures and Functions.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 11 Further Topics in Algebra.
Copyright © Cengage Learning. All rights reserved.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 10 Further Topics in Algebra.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Copyright © 2007 Pearson Education, Inc. Slide 8-1.
Copyright © 2011 Pearson Education, Inc. Slide
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Algorithms and their Applications CS2004 ( ) Dr Stephen Swift 3.1 Mathematical Foundation.
Grading Exams 60 % Lab 20 % Participation 5% Quizes 15%
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Section Finding sums of arithmetic series -Using Sigma notation Taylor Morgan.
1 1 OBJECTIVE At the end of this topic you should be able to Define sequences and series Understand finite and infinite sequence,
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
6.2 For…Next Loops General Form of a For…Next Loop
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
JavaScript, Fourth Edition
Section 11.1 Sequences. Sequence – list of values following a pattern Arithmetic – from term to term there is a common difference we’ll call d Geometric.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Infinite Geometric Series Recursion & Special Sequences Definitions & Equations Writing & Solving Geometric Series Practice Problems.
9.1 Sequences and Series. Definition of Sequence  An ordered list of numbers  An infinite sequence is a function whose domain is the set of positive.
Fortran: Control Structures Session Three ICoCSIS.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Arithmetic Sequences and Series Section Objectives Use sequence notation to find terms of any sequence Use summation notation to write sums Use.
Functions.
Chapter 6: Loops.
Chapter 4 C Program Control Part I
Programming For Nuclear Engineers Lecture 6 Arrays
Infinite Geometric Series
Midterm Review Programming in Fortran
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
EXCEL Study Guide #2.
Sequences and Summation Notation
REPETITION Why Repetition?
Presentation transcript:

11 - 2/4/2000AME 150 L Program Control Subroutines and Functions

11 - 2/4/2000AME 150 L Program Control - Loops Powerful numerical tool - SERIES –Polynomials or Power Series –Infinite Series of Generalized Functions

11 - 2/4/2000AME 150 L Arrays of Variables The use of indexed or subscripted variables can be treated in one of two ways a) Create different variables c 0  C0, c 1  C1, …, c N  CN b) Use a formal array structure REAL, dimension(0:12) :: C then c 0  C(0), c 1  C(1), …, c N  C(N) Size of an array is either a fixed number, a parameter, (or an argument or dynamic)

11 - 2/4/2000AME 150 L Declaring Arrays Two equivalent methods a) REAL, dimension(-2:5) :: a,b,c the three arrays a,b,s all have the same dimension (-2:5) b) REAL :: a(-2:5), B(15), c(0:5) makes it possible to use one declaration for arrays of different sizes

11 - 2/4/2000AME 150 L Recursion re·cur·sion (r-kûrzhn) n. Mathematics –An expression, such as a polynomial, each term of which is determined by application of a formula to preceding terms. –A formula that generates the successive terms of a recursion. [Late Latin recursi, recursin- a running back, from Latin recursus, past participle of recurrere, to run back; see recur.] other definition 1, definition 2definition 1definition 2

11 - 2/4/2000AME 150 L Recursion (Examples) Calculate x n x = {set value of x} x_to_n = 1 … x_to_n = x * x_to_n 1st time x_to_n=x, 2nd time x_to_n=x*x, etc. Calculate (-1) n m1n = 1 … m1n = - m1n every time statement is executed, m1n changes sign +1, -1, +1

11 - 2/4/2000AME 150 L Loops To execute the same set of instructions repeatedly Most common structure DO … END DO –Loop can be counted (original DO loop) –Loop can be conditional ( DO WHILE ) –Loop can be Infinite (but broken by a condition or a test) DO may have a label

11 - 2/4/2000AME 150 L Counted DO Loop Syntax: [label:]DO ctr = init, fin [, incr] …Fortran statements END DO [label] If label is used, it is followed by one colon : ctr is a variable, and must be declared (It is preferred that ctr be an integer) If incr is omitted, it is assumed to be +1

11 - 2/4/2000AME 150 L Counted DO (continued) The DO loop is executed exactly largest of [(fin-init)/incr, 0 * ] times DO loops can count backwards, but increment must be negative DO loops may have ctr as a REAL variable, but practice is discouraged DO index (ctr) can be used as an index of an array, or in normal expressions

11 - 2/4/2000AME 150 L Trivial example of a Loop Sum of Integers, Program fragment INTEGER :: n, i, sum n = {read in some value for n} sum = 0!Initialize variable loop:DO I = 1, n sum = sum + I END DO loop WRITE(*,*)n,sum

11 - 2/4/2000AME 150 L Parts of a Loop Initialization –Both Loop Index and calculations Incrementing –At completion of loop, increment (or 1) is added to Loop Index Testing –Whenever Loop Index <= Final value, repeat loop with new value of Loop Index

11 - 2/4/2000AME 150 L Parts of a Loop (continued) Body of Loop –The statements that are repeatedly executed for different values of the Loop Index Special cases –CYCLE - go to increment & test immediately –EXIT - exit loop immediately

11 - 2/4/2000AME 150 L Power Series INTEGER :: i, N, sum=0 REAL :: x, c(0:20) …read in x, N, and all c's (NOTE N<=20) DO i = 0, N sum = sum + c(i) * x**i END DO

11 - 2/4/2000AME 150 L Special Power Series - e x This series is a special case of the preceding example, where c i = 1/i! n! is notation for n factorial (or the factorial function) n! = 1*2*3*…*(n-1)*n

11 - 2/4/2000AME 150 L The Factorial Function ! Normal definition -- repeated product Program Fragment INTEGER :: I, n, nfact n= {get some value for n} nfact=1!initialize factorial DO I=1,n nfact = nfact * I END DO

11 - 2/4/2000AME 150 L More Factorial Function ! Recursive definition 1! = 1 n! = n*(n-1)! [=n*(n-1)*(n-2)*…*2*1] Hence 2!=2*1!=2*1=2; 3!=3*2!=3*2=6; 4!=4*3!=4*6=24; 5!=5*4!=5*24=120 6!= 6*5!=6*120=720 7!=7*6!=7*720=5040 … factorial grows fast!

11 - 2/4/2000AME 150 L Factorial (Continued) Since n!=n*(n-1)!, and 1!=1 1! = 1* 0!  0! = 1 and 0! = 0 (-1)! =1  (-1)! is infinite (1/0) and for every negative integer (-n)! = (-n)*(-n+1)!  ( -n)! =  and believe it or not,

11 - 2/4/2000AME 150 L The Gamma Function  Factorial is related to an integral called the Gamma Function and hence, n! is defined for non-integer values of n [a real exclamation point]