Math 171 Spring 2005, Lab 6

Due Thursday, February 10, 11:59 P.M.

  1. Overview
  2. Your First Fortran Program
  3. Assignment

Overview

The purpose of this lab is to introduce you to writing, compiling, and running Fortran-95 programs using the Intel Fortran compiler.

Top

Your First Fortran Program

As with Maple and Matlab, in order to run the Intel Fortran compiler (ifort) you may need to make some changes to your environment variables. Make sure your PATH variable contains the /usr/intel/fc_80/bin directory and your MANPATH contains /usr/intel/fc_80/man. Now either start a new shell or type


source ~/.zprofile

to update your shell environment without restarting. You should now be able to run ifort by typing ifort at the command prompt.

The steps listed below describe everything you need to do in order to write, compile, and run a minimal Fortran-95 program. You will need to complete steps almost identical to these for every Fortran program you write.

  1. Open a new textfile in your favorite text editor with the extension .f90:
    
    vi hello.f90
    
    
    The .f90 extension indicates to the compiler that this is a Fortran program using free-form source format. Fortran programs can also end with .f to indicate that they use fixed-form source format, but you should never write new programs this way.
  2. Insert three lines at the top of the textfile containing your name, the date, and a short sentence describing the purpose of your program. Each line should begin with the ! character, which tells the compiler that the remainder of the line is a comment and should be ignored. For example, I would type something like:
    
    !  Author: George Butler
    !    Date: Feb 2, 2005
    ! Purpose: This is my first Fortran program.
    
    
  3. Insert a line that says
    
    PROGRAM hello
    
    
    This tells the compiler that you are starting a new Fortran program called hello.
  4. On the next line, type
    
        IMPLICIT none
    
    
    This line tells the compiler that it should disable implicit typing, i.e. that it should not allow you to use any variables that you have not explicitly declared. While not required, this statement has the following benefits:
  5. Since you will not be defining any variables for this program, enter a blank line followed by:
    
        WRITE(*,*)  " Hello World"
    
    
    This statement will write the message contained in double quotes to the screen after you complie and execute your program. The blank line serves to provide a visual division between the declaration section (which contains the program name and variable names) and the execution section (which contains commands that will be executed when you run your program). Note that the lines you wrote in this step and the previous one are both indented. You should try to format your source code so that indentation mirrors the semantic hierarchy of the statements. For example, both the IMPLICIT none statement and the WRITE statement are logically contained in the program hello, so they should have a higher level of indentation than the PROGRAM statement.
  6. Insert another blank line, followed by
    
    END PROGRAM
    
    
    This tells the compiler that it has reached the end of the program called hello. This time, the blank line serves to separate the execution section from the termination section.
  7. Putting it all together, your textfile should look something like the following:
    !  Author: George Butler
    !    Date: Feb 2, 2005
    ! Purpose: This is my first Fortran program.
    
    PROGRAM hello
    
        IMPLICIT none
    
        WRITE(*,*) " Hello World"
    
    END PROGRAM
    
    Note that the compiler doesn't care about how you use spacing, or about whether you use comments in your program. For example, the following Fortran program is functionally equivalent to the one given above:
    PROGRAM hello;IMPLICIT none;WRITE(*,*)" Hello World";END PROGRAM
    
    However, the second program is harder to read, harder to modify, and uglier than the first.
  8. Now that you have finished typing in your source code, save your file and exit the editor.
  9. Compile your program using the ifort command:
    
    ifort hello.f90
    
    
    This will create a binary executable file called a.out in the current working directory.
  10. Run your program by typing the pathname of the executable, followed by the executable name. Since the executable is contained in the current working directory, you can use . as the pathname:
    
    ./a.out
    
    
    You should see the message that you entered following the WRITE statement printed to the screen. If not, ask me or Dan for assistance. Otherwise, congratulations! You have just written, compiled, and executed your first Fortran program.
  11. Note that if you want to use a different executable name, you can indicate this to the compiler using the -o flag:
    
    ifort -o hello hello.f90
    ./hello
    
    

Top

Assignment

You will be writing a short Fortran program for this lab, which will perform a small but useful computation. Write a program called bmi that prompts the user for their height (in feet and inches) and weight (in pounds), and prints out the corresponding body mass index (BMI). Note that BMI is defined as body mass (in kg) divided by the square of height (in m). You can download the executable for my Fortran implementation of the program bmi here or copy the file ~gbutler/www-home/m171/spring_2005/bin/bmi into your home directory. Your own program should duplicate the behavior of mine almost exactly (your answers should equal mine to at least a few decimal places). Once you have your program working, complete the following steps to demonstrate that you know how to compile and execute Fortran code. Note that these steps will not be required in future labs.

  1. Move into your lab directory and start the script program. This is a utility for UNIX shells that provides similar functionality to the diary command in Matlab. All your input to the shell and output from your commands will be stored in a file called typescript.
  2. Compile your program.
  3. Run your program, using a height of 5'11" and a weight of 163 pounds as input.
  4. Exit the typescript program by typing exit.

After completing the above steps, delete your executable and any other extraneous files you have lying in your lab directory, and submit your lab using the ~gbutler/bin/m171_submit command.

Top

Back to the course information page