












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Chapter 2 ! This is the modified version of the program given in Figure 2.1 (pp. 18-19). ! The program is incomplete since it does contain any code for ! procedure calculate_circle PROGRAM circle IMPLICIT NONE ! This program calculates the equation of a circle passing ! through three points ! Variable declarations REAL :: x1, y1, x2, y2, x3, y3, a, b, r ! Step 1 WRITE (,) "Please type the coordinates of three points" WRITE (,) "in the order x1,y1,x2,y2,x3,y3" READ (,) x1,y1,x2,y2,x3,y3! Read the three points ! Step 2 CALL calculate_circle(x1,y1,x2,y2,x3,y3,a,b,r) ! Step 3 WRITE (,) "The center of the circle through these points is & &(",a,",",b,")" WRITE (,) "Its radius is ", r STOP END PROGRAM circle Chapter 3 ! Modified Example 3.1 (page 55) PROGRAM centigrade_to_fahrenheit IMPLICIT NONE ! A program to convert a Centigrade temperature to Fahrenheit ! Variable declarations REAL :: temp_c, temp_f ! Ask for Centigrade temperature WRITE (,) "What is the Centigrade temperature? " READ (,) temp_c
! Convert it to Fahrenheit temp_f = 9.0 * temp_c / 5.0 + 32. ! Print both temperatures WRITE (,) temp_c, "C = ", temp_f, "F" STOP END PROGRAM centigrade_to_fahrenheit ! Modified Example 3.2, page 64 PROGRAM welcome IMPLICIT NONE ! This program manipulates character strings to produce a ! properly formatted welcome message ! Variable declarations CHARACTER (LEN=20) :: title, first_name, last_name CHARACTER (LEN=40) :: full_name ! Ask for name, etc WRITE (,) "Please give your full name in the form requested" WRITE (,) "Title (Mr./Mrs./Ms./Professor/etc: " READ (,) title WRITE (,) "First name: " READ (,) first_name WRITE (,) "Last name: " READ (,) last_name ! Create full name full_name = TRIM(title)//" "//TRIM(first_name)//" "//last_name ! Print messages WRITE (,) "Welcome ", full_name WRITE (,) "May I call you ", TRIM(first_name), "?" STOP END PROGRAM welcome ! Modified example 3.3, page 71 PROGRAM geometry IMPLICIT NONE
TYPE :: complex_number REAL :: real_part, imaginary_part END TYPE complex_number ! Variable definitions TYPE(complex_number) :: c1, c2, sum, diff, prod ! Read data WRITE (,) "Please supply two complex numbers" WRITE (,) "Each complex number should be typed as two numbers, " WRITE (,) "representing the real and imaginary parts & &of the number" READ (,) c1, c ! Calculate sum, difference and product sum%real_part = c1%real_part + c2%real_part sum%imaginary_part = c1%imaginary_part + c2%imaginary_part diff%real_part = c1%real_part - c2%real_part diff%imaginary_part = c1%imaginary_part - c2%imaginary_part prod%real_part = c1%real_part * c2%real_part - & c1%imaginary_part * c2%imaginary_part prod%imaginary_part = c1%real_part * c2%imaginary_part + & c1%imaginary_part * c2%real_part ! Print results WRITE (,) "The sum of the two numbers is ", sum WRITE (,) "The difference between the two numbers is ", diff WRITE (,) "The product of the two numbers is ", prod STOP END PROGRAM complex_arithmetic Chapter 4 ! Example 4.1, page 86, code modified for elf90 compiler PROGRAM wheat_sowing IMPLICIT NONE
! A program to calculate the quantity of wheat required to ! sow a triangular field ! Variable declarations REAL :: a, b, c, s, area, density, quantity INTEGER :: num_bags ! Read the lengths of the sides of the field WRITE (,) "Type the lengths of the three sides of the field & &in meters: " READ (,) a, b, c ! Calculate the area of the field s = 0.5 * (a + b + c) area = SQRT (s * (s - a) * (s - b) * (s - c)) ! Read sowing density WRITE (, ) "What is the sowing density (gm/sq.m.)? " READ (,) density ! Calculate quantity of wheat and the number of ! full 10 kg bags quantity = density * area num_bags = 0.0001 * quantity + 0.9! Round up more than 1kg ! Print results WRITE (,) "The area of the field is ", area, "sq. meters" WRITE (,) "and ", num_bags, " 10 kilo bags will be required" STOP END PROGRAM wheat_sowing ! Example 4.2 from text, page 103 ! A function that takes two chracter arguments as input arguments ! containing two names (a 'first name' and a 'family name', respectively), ! and returns a string containing the two names exactly one space separating ! them. FUNCTION full_name(first_name, last_name) IMPLICIT NONE CHARACTER (LEN=) :: full_name CHARACTER (LEN=), INTENT(IN) :: first_name, last_name
! Calculate and print sum, difference, product and quotient WRITE (,) "The sum of two numbers is ", c_add(z1, z2) WRITE (,) "The difference between the two numbers is ", & c_sub (z1, z2) WRITE (,) "The product of the two numbers is ", c_mult(z1, z2) WRITE (,) "The result of dividing the first number by the & &second is ", c_div(z1,z2) STOP contains FUNCTION c_add (z1, z2) USE complex_data IMPLICIT NONE ! Declare function type and dummy arguments TYPE (complex_number) :: c_add TYPE (complex_number), INTENT(IN) :: z1, z ! Calculate function result c_add%real_part = z1%real_part + z2%real_part c_add%imag_part = z1%imag_part + z1%imag_part RETURN END FUNCTION c_add FUNCTION c_sub(z1,z2) USE complex_data IMPLICIT NONE ! Declare function type and dummy arguments TYPE (complex_number) :: c_sub TYPE (complex_number), INTENT(IN) :: z1, z ! Calculate function result c_sub%real_part = z1%real_part - z2%real_part c_sub%imag_part = z1%imag_part - z2%imag_part RETURN END FUNCTION c_sub FUNCTION c_mult(z1, z2) USE complex_data IMPLICIT NONE ! Declare function type and dummy arguments TYPE (complex_number) :: c_mult
TYPE (complex_number), INTENT(IN) :: z1, z ! Calculate function result c_mult%real_part = z1%real_part * z2%real_part - & z1%imag_part * z2%imag_part c_mult%imag_part = z1%real_part * z2%imag_part + & z1%imag_part * z2%real_part RETURN END FUNCTION c_mult FUNCTION c_div(z1, z2) USE complex_data IMPLICIT NONE ! Declare function type and dummy arguments TYPE (complex_number) :: c_div TYPE (complex_number), INTENT(IN) :: z1, z ! Local variable to save calculating denominator twice REAL :: denom ! Calculate function result denom = z2%real_part2 + z2%imag_part c_div%real_part = (z1%real_part * z2%real_part + & z1%imag_part * z2%imag_part)/denom c_div%imag_part = (z2%real_part * z1%imag_part - & z1%real_part * z2%imag_part)/denom RETURN END FUNCTION c_div END PROGRAM complex_example ! Example 4.5, page 118-119, modified for elf MODULE geometric_data IMPLICIT NONE ! Type definitions TYPE :: point REAL :: x, y! Cartesian coordinates of the point END TYPE point TYPE :: line REAL :: a, b, c! coefficients of defining equations END TYPE line
WRITE (,) " b = ", p1_to_p2%b WRITE (,) " c = ", p1_to_p2%c STOP END PROGRAM Geometry Chapter 5 ! Example 5.1, page 139, code modified for elf90 compiler PROGRAM wheat_sowing IMPLICIT NONE ! A program to calculate the quantity of wheat required to ! sow a triangular field ! Variable declarations REAL :: a, b, c, s, area, density, quantity INTEGER :: num_bags ! Read the lengths of the sides of the field WRITE (,) "Type the lengths of the three sides of the field & &in meters: " READ (,) a, b, c ! Calculate the area of the field s = 0.5 * (a + b + c) area = SQRT (s * (s - a) * (s - b) * (s - c)) ! Read sowing density WRITE (, ) "What is the sowing density (gm/sq.m.)? " READ (,) density ! Calculate quantity of wheat and the number of ! full 10 kg bags quantity = density * area num_bags = 0.0001 * quantity! Any part-full bag is excluded ! Check to see if another bag is required IF (quantity > 10000 * num_bags) THEN num_bags = num_bags + 1 END IF
! Print results WRITE (,) "The area of the field is ", area, "sq. meters" WRITE (,) "and ", num_bags, " 10 kilo bags will be required" STOP END PROGRAM wheat_sowing ! Example 5.2: Page 143, modified for elf ! An external function that returns the cube root of its argument FUNCTION cube_root(x) IMPLICIT NONE REAL :: cube_root ! Function to calculate the cube root of a ! real number ! Dummy argument declaration REAL, INTENT (IN) :: x ! Local constant REAL, PARAMETER :: epsilon = 1E- ! Eliminate (nearly) zero case IF (ABS(x) < epsilon) THEN cube_root = 0. ! Calculate cube root by using loops ELSE IF (x < 0) THEN ! First deal with negative argument cube_root = -EXP(LOG(-x)/3.0) ELSE ! Positive argument cube_root = EXP(LOG(x)/3.0) END IF RETURN END FUNCTION cube_root ! Example 5.3: pages 144-145, modified for elf ! It has two warning errors MODULE geometric_data IMPLICIT NONE ! Type definitions
! Dummy argument CHARACTER (LEN=1), INTENT(IN) :: char ! Local constant INTEGER, PARAMETER :: upper_to_lower = IACHAR("a") - IACHAR("A") ! Check if argument is lower case alphabetic, upper case ! alphabetic, or non-alphabetic IF ("A" <= char .AND. char <= "Z") THEN ! Upper case - convert to lower case change_case = ACHAR(IACHAR(char)+upper_to_lower) ELSE IF ("a" <= char .AND. char <= "z") THEN ! Lower case - convert to upper case change_case = ACHAR(IACHAR(char) - upper_to_lower) ELSE ! Not alphabetic change_case = char END IF RETURN END FUNCTION change_case ! Example 5.5, page 155, modified for elf PROGRAM seasons IMPLICIT NONE ! A program to calculate in which season a specified data lies ! Variable declarations CHARACTER(LEN=10) :: date CHARACTER(LEN=2) :: month ! Read date WRITE(,) "Please type a date in the form yyyy-mm-dd" READ (,) date ! Extract month number month = date(6:7) ! Print season SELECT CASE (month) CASE ("08":"10")
WRITE (,) date, " is in the spring" CASE ("11","12","01":"03") WRITE (,) date, " is in the summer" CASE ("04","05") WRITE (,) date, " is in the autumn" CASE ("06", "07") WRITE (,) date, " is in the winter" CASE DEFAULT WRITE (,) date, " is not a valid date" END SELECT STOP END PROGRAM seasons ! Example 5.6(a), page 159-160, modified for elf ! A program to solve a quadratic equation using a CASE ! statement to distinguish between the three cases PROGRAM quadratic_by_CASE IMPLICIT NONE ! Constant declaration REAL, PARAMETER :: epsilon = 1E- ! Variable declarations REAL :: a, b, c, d, sqrt_d, x1, x INTEGER :: selector ! Read coefficients WRITE (,) "Please type the three coefficients a, b and c" READ (,) a, b, c ! Calculate b2 - 4ac and resulting case selector d = b2 - 4.0ac selector = d/epsilon ! Calculate and print roots, if any SELECT CASE (selector) CASE (1:) ! Print roots sqrt_d = SQRT(d) x1 = (-b + sqrt_d)/(a+a) x2 = (-b - sqrt_d)/(a+a) WRITE(,) "The equation has two roots: ",x1," and ",x CASE (0) ! One root x1 = -b/(a+a)
END PROGRAM quadratic_by_block_IF Chapter 6 ! Example 6.1, page 175, modified for elf90 compiler ! This program prints statistics about a set of exam results PROGRAM examination_marks IMPLICIT NONE ! Variable declarations INTEGER :: i, number, mark, & sum = 0, maximum = - HUGE(1), minimum = HUGE(1) REAL :: average ! Read number of marks, and then the marks WRITE (,) "How many marks are there? " READ (,) number WRITE (,) "Please type ", number, " marks: " ! Loop to read and process marks DO i=1, number READ (,) mark ! On each pass, update sum, maximum and minimum sum = sum + mark IF (mark > maximum) maximum = mark IF (mark < minimum) minimum = mark END DO ! Calculate average mark and output results average = REAL (sum)/number WRITE (,) "Highest mark is ", maximum WRITE (,) "Lowest mark is ", minimum WRITE (,) "Average mark is ", average STOP END PROGRAM examination_marks ! Example 6.2, page 177, modified for elf PROGRAM multiplication_tables IMPLICIT NONE
! A program to print multiplication tables from 2 to 12 times ! Variable declarations INTEGER :: i, j ! Outer loop defines which 'times table' DO i = 2, 12 WRITE (,) " " WRITE (,) i, " times table" DO j = 1, 12 WRITE (,) i, " times ", j, " is ", ij END DO END DO STOP END PROGRAM multiplication_tables ! Example 6.3, page 178, modified for elf ! A program to print multiplication tables from 2 to 12 times PROGRAM multiplication_tables IMPLICIT NONE ! A program to print multiplication tables from 2 to 12 times ! Variable declarations INTEGER :: i, j ! Outer loop defines which 'times table' DO i=2, 12 WRITE (,) " " WRITE (,) i, " times table" DO j=1, i WRITE (,) i, " times ",j," is ",ij END DO END DO STOP END PROGRAM multiplication_tables ! Example 6.4, pages 182-183, modified for elf90 compiler ! This program calculates some simple examination statistics PROGRAM examination_statistics IMPLICIT NONE ! Constant and variable declarations
! Deal with no terminator case IF (num_pupils == max_pupils) THEN WRITE (,) max_pupils, " sets of data read without a & &terminating record" WRITE (,) "Results are based on these pupils only" END IF WRITE (,) "There are ", num_pupils, " pupils. Their average & &mark is ", REAL(total_marks)/num_pupils IF (num_girls > 0) THEN WRITE (,) "There are ", num_girls, " girls. Their & &average mark is", REAL(marks_girls)/num_girls ELSE WRITE (,) "There are no boys in the class" END IF END IF STOP END PROGRAM examination_statistics Chapter 8 ! Example 8.2; modified for elf ! It compiles but does not run; i/o statements should be changed ! for file i/o PROGRAM Radioactive_decay IMPLICIT NONE ! This program processes experimental data relating ! to radioactive decay ! Constant declarations ! max_readings is maximum number of sets of data ! in and out are the unit numbers for reading and writing INTEGER, PARAMETER :: max_readings = 1000 ! Variable declarations INTEGER :: alpha, beta, gamma, i, max_interval = 0 REAL :: time, last_time = 0.0, period, & av_alpha, av_beta, av_gamma, max_av_gamma = 0. ! Print headings
! Process max_readings sets of data in a loop DO i = 1, max_readings READ (UNIT = *, FMT = 101) time, alpha, beta, gamma ! Calculate interval since last readings period = time - last_time last_time = time ! Calculate average rates of emmission av_alpha = alpha/period av_beta = beta/period av_gamma = gamma/period ! Print statistics for this interval WRITE (UNIT = *, FMT = 202) i, period, alpha, beta, gamma, & av_alpha, av_beta, av_gamma ! Check for maximum gamma radiation in this period IF (av_gamma > max_av_gamma) THEN max_av_gamma = av_gamma max_interval = i END IF END DO ! Print details of interval with maximum gamma radiation WRITE (UNIT = *, FMT = 203) max_av_gamma, max_interval ! Format statements 101 FORMAT (F8.2, 3(TR5,I6)) 201 FORMAT ("1","Internal",T11,"Time",T17,"Alpha", & T24,"Beta",T30,"Gamma",T37,"Average",T46,"Average", & T55,"Average"/ & T38,"Alpha",T47,"Beta",T56,"Gamma") 202 FORMAT (I6,F8.2,2I6,I7,2F9.2,F10.2) 203 FORMAT ("0",T3,"Maximum average gamma radiation was", & F7.2," in interval",I5) STOP END PROGRAM Radioactive_decay