introduction to computer programming loops 1/2waltchen/files/chapman/chap4-loops-1-en.pdf ·...

38
Introduction to Computer Programming Loops 1/2 Chap 4 Loops and Character Manipulation Fortran 95/2003 for scientists and Engineers by Stephen J. Chapman Prepared by Walter Chen, Dept. of Civil Engineering, NTUT For classroom teaching purpose

Upload: lenga

Post on 29-Apr-2018

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Introduction to Computer Programming Loops 1/2

Chap 4 Loops and Character Manipulation Fortran 95/2003 for scientists and Engineers

by Stephen J. Chapman

Prepared by Walter Chen, Dept. of Civil Engineering, NTUT

For classroom teaching purpose

Page 2: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Control constructs: Loops Loops are Fortran constructs that permit us to execute a sequence

of statements more than once While loops

The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied

Iterative loops (counting loops) The code in an interactive loop is repeated a specified

number of times, and the number of repetitions is known before the loop starts

Page 3: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

While Loops

Page 4: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

The While Loop

Fortran code

Flow chart

While is not a keyword

Page 5: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

EXIT statement A while loop may contain one or more EXIT statements to

terminate its execution Each EXIT statement is usually a part of an IF statement or

block IF construct If the logical_expr in the IF is true when the statement is

executed, control transfers immediately to the first statement after the END DO

Page 6: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Good programming practice

In a well-structured program, every while loop should have a single entry point and a single exit point

The entry point for a while loop is the DO statement, and the exit point is the EXIT statement

Page 7: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Example 4-1 statistical analysis

Average or arithmetic mean

Standard deviation

Page 8: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Requirements

Implement an algorithm that reads in a set of measurements and calculates the mean and the standard deviation of the input data set

We do not know in advance how many measurements are included in the data set

The program must be able to read in an arbitrary number of measurements

Page 9: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Analysis We can use a while loop to accumulate the input

measurements before performing the calculations We must have some way of telling the program that there

are no more data values to enter We will assume that all the input measurements are

either positive or zero, and we will use a negative input value as a flag to indicate that there are no more data values to read

Page 10: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Procedures State the problem Define the inputs and outputs

Input: unknown number of positive or zero real numbers Output: original data, mean, standard deviation

Design the algorithm See flow chart

Turn the algorithm into Fortran statements Test the program

Page 11: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Flow Chart

Page 12: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Codes

Declare all variables

Beginning

End

Common Technique

Page 13: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Test values and Results

Page 14: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Fatal flaw in the preceding program

If we enter either no numbers or only one number, then we are dividing by zero in the equations

The division-by-zero error causes the program to abort

We need to modify the program to detect this problem and stop gracefully

Page 15: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Fixed program

Check at least 2 numbers to compute the standard deviation

Page 16: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Do While Loop

Page 17: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

The DO WHILE loop

If the logical expression is true, statements 1 through n are executed

The process is repeated until the logical expression becomes false

Page 18: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

The iterative or counting loop

Each of the three DO loop parameters istart, iend, and incr may be a constant, a variable, or an expression

If index*incr ≦iend*incr, the program body executes

Afterwards, the control variable is recalculated as index = index + incr

Page 19: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Flow chart

Page 20: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Execute 10 times Execute 5 times

Execute 0 times Execute 4 times

Examples:

Page 21: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Example 4-3 calculating the day of year

The day of year is the number of days (including the current day) that have elapsed since the beginning of a given year

Write a Fortran program that accepts a day, month, and year and calculates the day of year corresponding to that date

Page 22: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Solution To determine the day of year, this program needs to sum the

number of days in each month preceding the current month, plus the number of elapsed days in the current month

A DO loop is used to perform this sum A SELECT CASE is used to determine the proper number of days

to add for each month During a leap year, an extra day must be added to the day of

year for any month after February

Page 23: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Gregorian calendar The most widely used calendar in the world today Was decreed by Pope Gregory XIII, after whom it was named A reform of the Julian calendar, in which all years exactly

divisible by 4 are leap years The Julian calendar is a reform of the Roman calendar, and

was introduced by Julius Caesar in 46 BC Dropped 10 days to bring the calendar back into

synchronization with the seasons

Source: wikipedia

Page 24: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Adoption in UK

Britain and the British Empire (including the eastern part of what is now the United States) adopted the Gregorian calendar in 1752 by which time it was necessary to correct by 11 days

Wednesday, 2 September 1752 was followed by Thursday, 14 September 1752 to account for 29 February 1700 (Julian)

Source: wikipedia

Page 25: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Adoption in Russia

In Russia the Gregorian calendar was accepted after the October Revolution (so named because it took place in October 1917 in the Julian calendar)

On 24 January 1918 the Council of People's Commissars issued a Decree that Wednesday, 31 January 1918 was to be followed by Thursday, 14 February 1918

Source: wikipedia

Page 26: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Adoption in Eastern Asia

Japan is the first eastern Asia country to adopt the the Gregorian calendar on 1 January 1873

The Republic of China (ROC Taiwan) formally adopted the Gregorian calendar at its founding on 1 January 1912

Source: wikipedia

Page 27: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Adoption timeline

Source: wikipedia

Page 28: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Leap year In the Gregorian calendar

Years evenly divisible by 400 are leap years Years evenly divisible by 100 but not by 400 are not leap

years All years divisible by 4 but not by 100 are leap years All other years are not leap years

We will use the MOD function to determine whether or not a year is evenly divisible by a given number

Page 29: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Simply put

1 year = 365.242199 days We need to add 1 day every 4 years in order to keep

the seasons right But this will create an extra day after 100 years So we subtract 1 day every 100 years But this again cause problems One extra day will be created after 400 years So we subtract 1 day every 400 years

Page 30: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Program codes

Do you understand the if statement here?

Accumulate days using a loop

If it is a leap year, leap_day = 1

The name of the program is doy not day. It means "Day Of Year"

Page 31: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Test cases

Page 32: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Example 4-4 statistical analysis

Implement an algorithm that reads in a set of measurements and calculates the mean and the standard deviation of the input data set, when any value in the data set can be positive, negative, or zero

Ask the user for the number of input values

Page 33: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Flow chart

Page 34: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Codes

Page 35: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Notes concerning the DO loop

The index variable and control parameter of a DO loop should always be of type integer

If a DO loop completes normally, the value of the index variable is undefined when the loop is completed

Page 36: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Program findmaxDeclare variables

max = 0

Beginning of the loopAsk user to enter a number, xIf x is less than 0, leave the loopIf x is greater than max, set x to max

End of the loop

Write out the max. valueend program findmax

Write a Fortran program to find the maximum value

Page 37: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Test Values

3862111857

Page 38: Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf · Fortran 95/2003 for scientists and Engineers ... Solution To determine the ... Write a

Encryption and decryption

Pemod n=?

Test P = 115 e = 137 n = 3737

Hint:You don't really have to compute P^e1* 115 mod 3737 = 115115*115 mod 3737 = 20142014*115 mod 3737 = 36533653*115 mod 3737 = 15511551*115 mod 3737 = 2726...