Examples of Programs

Points and Lines in the Plane

Distance Between Two Points

•     program to compute distance between two points in the plane

•     points specified by their x and y coordinates

•     if p = (a,b) and q = (c,d) are the points then distance between p and q is

 

(a-c)2 + (b-d)2

Program to Compute Distance

program point_distance

implicit none

real :: a,b,c,d,distance

print * , “type values of a, b, c, d”

read * , a, b, c, d

distance = sqrt((a-c)**2+(b-d)**2)

print * , “distance between points (”, a, &

“,”, b, “) and (”, c, “,”, d, “) is”, distance

end program point_distance

Fortran Program

•     Fortran programs consist of a sequence of statements

•     one statement per line (up to 132 characters)

•     statement may be anywhere on a line

•     statements have a precisely defined syntax

•     all variable names and reserved words must be separated by blanks

•     no blanks in names or reserved words

•     consecutive blanks are treated as one

Program Statement

•     all Fortran programs start with

program  program_name

–   same rules apply for program names as for variable names

–   all names must be distinct

•     all Fortran programs end with

end program program_name

•     indicates no more statements, program stops

Implicit None Statement

•     following the program statement is the  implicit none statement

•     required for historical reasons

•     in earlier versions of Fortran all variables were not declared

•     type of a variable was implied by its name

•     led to serious errors due to misspelling

•     implicit none ensures all variables are declared (else program will not compile)

Read Statement

•     read statement used to read values of variables from the terminal

•     simplest form    read * ,  list_of_variables

•     * indicates read from terminal without any format ( see later)

•     values of variables given in the same sequence as in the read statement

•     values are given in decimal as for constants

Print Statement

•     print statement used to output values of variables to the terminal

•     simplest form   print * , list_of_variables

•     can also print messages which must be enclosed in “ …… ”

•     values printed in decimal in the same order, separated by blanks, on one line 

•     reals printed with fixed or floating point

•     can continue statement on next line using &

SQRT Function

•     sqrt is an intrinsic function in Fortran

•     sqrt(e) is a real expression whose value is  non-negative square root of real expression e

•     sqrt(2) is invalid but sqrt(2.0) is correct

•     an error will occur during execution if e happens to have a negative value

•     care should be taken to avoid this

Program to Compute Distance

program point_distance

implicit none

real :: a,b,c,d,distance

print * , “type values of a, b, c, d”

read * , a, b, c, d

distance = sqrt((a-c)**2+(b-d)**2)

print * , “distance between points (”, a, &

“,”, b, “) and (”, c, “,”, d, “) is”, distance

end program point_distance

Output of Program

•     try running the program

•     inaccuracy in numbers

•     how much is the error?

•     print statement contains too much data

•     split print statement

•     output should be printed in readable form

•     formatted output possible ( see later)

 

 

Modified Print Statement

print *, “distance between points”

print *, “(”, a, “,”, b, “) and”

print *, “(”, c, “,”, d, “) is”

print *, distance

•     each print statement prints on a new line

•     run the modified program

•     output looks slightly better

Point to Line Distance

•     distance to the nearest point on a line from a given point

•     ax + by + c = 0 is equation of the line

•     (p,q) is the given point

•     drop  perpendicular to line from point

•     length given by the formula

Example Program

program point_line_distance

implicit none

real :: a, b, c, p , q, distance

print *, “type values of a, b, c, p, q”

! ax+by+c is equation of line

! (p,q) is the point

read *, a, b, c, p, q

distance = abs(a*p+b*q+c)/sqrt(a**2+b**2)

print *, distance  ! length of perpendicular

end program point_line_distance

Comment Statements

•     comment statements included in programs for explanation

–   meaning of variables ( also units used)

–   input and output of program

–   complicated expressions

•     comment added by putting ! ( exclamation)

•     anything on a line after ! is ignored

•     comments should be used liberally

Abs Function

•     the abs function computes the absolute value

•     can be used with integers as well as real

•     abs(e) has the same type as e

•     e can have real or integer type

•     abs(-1) is an integer expression with value 1

•     abs(-1.0) is a real expression with value 1.0

Intersection of Lines

•     compute the point of intersection of 2 lines

•     ax + by + c = 0 and px + qy + r = 0 are the two lines

•     solve two equations in two variables

Example Program

program line_intersection

implicit none

real :: a, b, c, p, q, r, x, y

print *, “type values of a, b, c, p, q, r”

read *, a, b, c, p, q, r

! ax+by+c=0 and px+qy+r=0 are the lines

x = (b*r-c*q)/(a*q-b*p)

y = (c*p-a*r)/(a*q-b*p) ! compute and store aq-bp

print *, “x =”, x, “y =”, y

end program line_intersection

Output of Program

type values of a, b, c, p, q, r

1.0, 0.0001, -1.0001, 0.9999, 0.0001, -1.0

x =   1.0000000 y =   1.0001659

                check it

type values of a, b, c, p, q, r

100.0, 1e-4, -100.0001, 99.9999, 1e-4, -100.0

x =   1.0000000 y =   0.9918213

•     what if a and p are increased further?

 

Summary

•     Fortran statements

–   program and end program

–   implicit none

–   read and print

–   comments

–   intrinsic functions sqrt and abs

•     example Fortran programs

•     numerical errors