! Example P.2. (Catherine Spade, 4 October 1999) ! Examples of intrinsic library functions. program A02 implicit none real :: A real, parameter :: B = 1.234, C = 5.678 integer, parameter :: I = 9, J = 10 ! start program A02 A = sqrt( B ) + abs( C ) write (unit = *, fmt = *) A, int( A ), nint( A ), real( I ), I write (unit = *, fmt = *) I / J, modulo( I, J ), abs( I ), sign( I, 1 ), sign( I, -1 ) write (unit = *, fmt = *) B, B / C, modulo( B, C ), abs( B ) write (unit = *, fmt = *) sign( B, 1.0 ), sign( B, -1.0 ) write (unit = *, fmt = *) atan2( 0.0, -1.0 ), exp( 1.0 ) write (unit = *, fmt = *) cos( 1.0 ), sin( 1.0 ), tan( 1.0 ), log( 2.0 ), log10( 2.0 ) stop end program A02 ! Example P.4. Prompting for terminal keyboard input. program A04 implicit none real :: X, Y, Average, Diameter, Fahr, Cels real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! start program A04 write (unit = *, fmt = *) " Please enter two numbers to be averaged. " read (unit = *, fmt = *) X, Y Average = (X + Y) / 2.0 write (unit = *, fmt = *) " The average of ", X, " and ", Y, " is: ", Average write (unit = *, fmt = *) " Please enter the diameter of a circle. " read (unit = *, fmt = *) Diameter write (unit = *, fmt = *) " Circumference: ", atan2( 0.0, -1.0 ) * Diameter write (unit = *, fmt = *) " Please enter the Fahrenheit temperature. " read (unit = *, fmt = *) Fahr Cels = (Fahr - OFFSET) / T_SCALE write (unit = *, fmt = *) Fahr, " deg. F = ", Cels, " deg. C " stop end program A04 ! Example P.5. (Catherine Spade, 8 October 1999) ! Temperature Converter with Repetition. program A05 implicit none integer, parameter :: LIMIT = 10 real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 real :: Fahr, Cels integer :: Loop ! start program A05 do Loop = 1, LIMIT write (unit = *, fmt = *) " Please enter Fahrenheit temperature ", Loop, ":" read (unit = *, fmt = *) Fahr Cels = (Fahr - OFFSET) / T_SCALE write (unit = *, fmt = *) Fahr, Cels end do stop end program A05 ! Count high and low temperatures. program A06 implicit none integer, parameter :: LIMIT = 30 real, parameter :: BOUNDARY = 68.0 real :: Temperature integer :: High_Count, Low_Count, Loop ! start program A06 High_Count = 0 Low_Count = 0 do Loop = 1, LIMIT write (unit = *, fmt = *) " Please enter the next temperature. " read (unit = *, fmt = *) Temperature if (Temperature >= BOUNDARY) then High_Count = High_Count + 1 else Low_Count = Low_Count + 1 end if end do write (unit = *, fmt = *) High_Count, " hot days, and ", Low_Count, " cold days this month." stop end program A06 ! Temperature Converter with Procedures. program A08 use A08M implicit none real :: Fahr, Cels ! start program A08 call Input( Fahr ) ! Subroutine reference Cels = Temp_C( Fahr ) ! Function reference call Output( Fahr, Cels ) ! Subroutine reference stop end program A08 module A08M implicit none public :: Input, Temp_C, Output contains subroutine Input( F_Temp ) real, intent(out) :: F_Temp ! start subroutine Input write (unit = *, fmt = *) " Please enter the Fahrenheit temperature. " read (unit = *, fmt = *) F_Temp return end subroutine Input function Temp_C( F_Temp ) result( Temp_C_R ) real, intent(in) :: F_Temp real :: Temp_C_R real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! start function Output Temp_C_R = (F_Temp - OFFSET) / T_SCALE return end function Temp_C subroutine Output( F_Temp, Temp_C_R ) real, intent(in) :: F_Temp, Temp_C_R ! start subroutine Output write (unit = *, fmt = *) F_Temp, " deg. F = ", Temp_C_R, " deg. C" return end subroutine Output end module A08MTally( )