## 1 EXAMPLE FORTRAN PROGRAM

A program is a sequence of instructions that enables the computer to solve a problem. Writing a program to solve the problem can be a formidable task if the problem is complex. The overall programming process can often be simplified by using a top-down approach. The process is called a top-down design because the programmer starts with the original problem and breaks it down into smaller tasks which can be addressed separately. First, the large tasks are identified and then each of these tasks is refined filling in more details. This process allows the programmer to initially concentrate on the overall steps without getting bogged down in the details of the program, thus making the programming task more manageable. Another advantage of breaking a program down into tasks is that each task can be checked for correctness apart from the other tasks. An algorithm or plan, in the form of a flowchart or pseudocode (English-like statements), can be used to describe the problem solving process.

### Example - Surface area and volume of a sphere

Problem Statement

Find the surface area and volume of a sphere with a given radius.

Mathematical Equations
The surface area of a sphere is and the volume is , where r is the radius.

Algorithm
1. Input the value of the radius of the sphere.
2. Compute the surface area and volume of the sphere.
3. Print the values of the radius, the surface area, and the volume of the sphere.

Code

Example Output
```Enter the radius of the sphere.
64.0
64.0000 is the value of the radius.
In a sphere of radius    64.0000 , the surface area is    51471.9
and its volume is    1.09807E+06.
```
Note: Very large or very small numbers may be expressed in exponential notation. A value expressed in exponential notation is written as a number between 0.1 and 10, called the mantissa, multiplied by an appropriate power of 10. The letter E separates the mantissa and the exponent of the base 10. The value 1098070 expressed in exponential notation is 1.09807E+06 which represents .

### Column Format

From the time FORTRAN was invented until the mid 1970's, most FORTRAN programs were punched on 80-column cards. Since cards had 80 punched columns, they could hold a maximum of 80 characters. The column restrictions in FORTRAN 77 are a carry over from the punched cards. The 80 positions on each line are numbered from left to right and are restricted to the following use:
columns 1-5: These columns are reserved for statement labels. Statement labels are positive integers from one to five digits in length. They are not required for every statement, but are necessary for statements that are referenced by other statements.

column 6: This column is used to indicate that a statement has been continued from the previous statement. Any character, except the number zero, can be used for a continuation symbol. A FORTRAN statement may have up to 19 continuation lines.

columns 7-72: These columns are reserved for FORTRAN statements. All characters past column 72 are ignored.

### Programming Style and Techniques

• It is considered good programming practice to include comment lines. Comment lines, indicated by a * or c in column 1, add information, such as the purpose of the program, definition of variables, and the purpose of statements within the program. A comment indicating the units of measure that correspond to numeric values will also help in the interpretation of results. Comments are printed in the listing of a program, but are otherwise ignored; they do not affect program execution. The following statements are comment lines from the example program:

Purpose of program
c This program determines the surface area and volume of a sphere, given its radius.

Definition of variables
c rad = radius, area = surface area, volume = volume of the sphere

Purpose of statements within the code
c Compute the surface area and volume of the sphere.

Units of measure
c Print the values of the radius (given in cm), the surface area (sq cm),
c and the volume (cubic cm).

• Within the code, use blank lines to separate the different steps of the algorithm. This style technique will be used in all example programs. Note the blank lines used in the PROGRAM sphere code.

### Keywords

The FORTRAN language does not differentiate between upper and lower case letters. In this textbook, upper case letters are used for all keywords that have a special meaning in FORTRAN. Lower case letters will be used for names created by the user.

The following explains each of the keywords used in the example code:

PROGRAM sphere
The PROGRAM statement identifies the beginning of a program and assigns the program name. The program name
• is a unique name to the program (a variable cannot have the same name)
• begins with a letter
• contains only the letters A-Z and a-z, and the digits 0-9
• consists of a maximum of 6 characters

This is a specification statement. Specification statements are used to assign memory locations and to specify the type (REAL, INTEGER, DOUBLE PRECISION, COMPLEX, CHARACTER, OR LOGICAL) of values to be stored in those locations.

The word PRINT declares that the output will be sent to the standard output device, usually the monitor. The *specifies that the values will be printed in a list. A list of what is to be printed follows the PRINT command. Single quote marks are used around literal information that is to be printed. In this example, if 4.0 is the value of rad, the following will be printed:

4.000000 is the value of the radius.

The READ statement suspends execution of the program as it waits for data to be entered from some input device, such as the keyboard. The entered value will be assigned to the variable rad. The *specifies that the values will be read from a list.

STOP
The STOP statement terminates execution of the program. Although most compilers will automatically add a STOP before the END statement if it does not exist, it is wise not to leave the addition of this statement to a compiler. STOP can appear anywhere in the program that makes sense and can appear as often as necessary.

END
The END statement marks the physical end of the program.

### EXERCISES

1. Calculate, by hand or calculator, the surface area and volume of a sphere whose radius is 2.0.

2. Using a text editor, key in the code given for the sphere problem.

For example, the command pico sphere.f will invoke the pico text editor and name a new file sphere.f. Remember that all FORTRAN 77 files must end in .f. As you key in the lines of PROGRAM sphere, pay close attention to the column positions.
3. Compile PROGRAM sphere with the command
f77 sphere.f

The compilation process will convert the FORTRAN code of sphere.f to executable (machine) code. The executable code will be called a.out unless you give it another name. If you get error messages at this point and your program does not compile, you will need to edit your program. Check to make sure that all your column positions are correct and that there are no typos.

4. Once your program compiles, execute it by keying the name of the executable code: a.out

The first line that will appear on the screen is

Enter the length of the radius of the sphere.
1. Key in the test value 2.0. Compare the area and volume with the values you calculated in exercise 1.
2. Run the machine code (a.out) several times, entering the given values for the radius and recording the results in Table 1. If the unit of the radius is cm (centimeters), what are the units of the area and volume?

Table 1: Surface Area and Volume of a Sphere
2.0

4.0

8.0

16.0

32.0

64.0

After reviewing the data in Table 1, answer the following question:

As the radius of the sphere increases by a factor of 2, the area increases by a factor of _______ and the volume increases by a factor of _______.

5. Problem Statement
Find the area of a triangle given the length of its base and height.

Algorithm
• Read the values of the base and height of the triangle.
• Determine the area of the triangle using the formula A=(1/2)bh.
• Print the area of the triangle.

Create a file called area.f. Key in the following code and make it more readable by inserting comments and blank lines. Your comments should explain the purpose of the program and the coded steps of the algorithm. You should also add your name and exercise number as comments at the beginning of the code. Then compile and execute area.f.

```        PROGRAM area
REAL base, height, area
PRINT *,'Enter the values for the base and height of a triangle.'