Input/Output and Arithmetic Operations

  1. Lets write our first program, what we shall do is copy the last program from the text on input/output (shown below) and save it to our UNIX machine. Then on that machine we shall compile the program and run it, to check it works. Read the getting started instructions on how we can access the UNIX machine and compile and run the program.

          PROGRAM PROJEC
    ************************************************************************
    * This program calculates the velocity and height of a projectile      *
    * given its initial height, initial velocity, and constant             *
    * acceleration.  Variables used are:                                   *
    *    HGHT0   :  initial height                                         *
    *    HGHT    :  height at any time                                     *
    *    VELOC0  :  initial vertical velocity                              *
    *    VELOC   :  vertical velocity at any time                          *
    *    ACCEL   :  vertical acceleration                                  *
    *    TIME    :  time elapsed since projectile was launched             *
    *                                                                      *
    * Input:   HGHT0, VELOC0, TIME                                         *
    * Output:  VELOC, HGHT                                                 *
    ************************************************************************
    
          REAL HGHT0, HGHT, VELOC0, VELOC, ACCEL, TIME
    
          ACCEL = -9.807
          PRINT *, 'ENTER THE INITIAL HEIGHT AND VELOCITY:'
          READ *, HGHT0, VELOC0
          PRINT *, 'ENTER TIME AT WHICH TO CALCULATE HEIGHT AND VELOCITY:'
          READ *, TIME
          HGHT = 0.5 * ACCEL * TIME ** 2  +  VELOC0 * TIME  +  HGHT0
          VELOC = ACCEL * TIME + VELOC0
          PRINT *, 'AT TIME ', TIME, ' THE VERTICAL VELOCITY IS ', VELOC
          PRINT *, 'AND THE HEIGHT IS ', HGHT
          END
    
    

    The program is hyperlinked and we can copy it to the UNIX machine: projec.f

  2. Write a program which will convert a temperature of C degrees on the Celsius scale to the corresponding Fahrenheit temperature and display the Fahrenheit temperature. The two scale are related by the following equation:

    degF = (9/5)*degC + 32

    When you have finished or have difficulties compare it to the program temp.f

  3. Write a program which writes out the square root of every whole number from 1 to 5. Check it to make sure it runs correctly.

  4. Devise a program that takes in three numbers from the keyboard, calls them a, b and c, calculates the equation below and prints the result.

    The program will fail when you run it if your values of a, b and c are such that b2 is less than 4ac; the computer will not square root a negative number.

    When you have finished or have difficulties compare it to the program abc.f

  5. The Quadratic equation

    It's time to write a proper program. Write a program that accepts a, b and c from the keyboard and finds the roots of the quadratic equation:

    [When writing even a short program like this, it is valuable to plan first on paper the steps the program must accomplish. For example, in this problem the steps might be: (a) get values of a, b and c; (b) calculate and print the first root; (c) calculate and print the second root; (d) stop. This logical approach improves the chances the program will work correctly.]

    Try running your program using a=2, b=5, c=2; then try a=2, b=5, c=5. This second set of coefficients should make your program fail, since the program should then try to square root a negative number - we shall see how to circumvent this difficulty shortly.

    When you have finished or have difficulties compare it to the program quad.f

Now return to the Introduction to FORTRAN Programming