Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf ·...
Transcript of Introduction to Computer Programming Loops 1/2waltchen/files/Chapman/Chap4-loops-1-EN.pdf ·...
![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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051602/5ae4d6eb7f8b9a90138f82e3/html5/thumbnails/38.jpg)
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...