CS1001 - D Term, 2000
Laboratory 4 - Functions
Date: April 12, 2000




Problem

In this lab exercise you will be writing a program that prints the value of every prime number
between 2 and 100. The program will call a function IsAPrime which returns .TRUE. or .FALSE.
depending on whether the number passed to it is a prime number or not. The main program should
call the function IsAPrime with the following DO-loop:

       DO 10 N=2,100
          IF (IsAPrime(N)) THEN
             PRINT *, N
          ENDIF
10     CONTINUE

Here's what you need to do:

   Using the loop given above, complete the main program. (Don't write the function yet -
   remember, one of the advantages of using subprograms is that we can develop the parts of a
   program independently. We'll worry about the details of the IsAPrime function later.)
   On a separate piece of paper, put your name, wpi login name, and section number, and answer
   the following question: if IsAPrime was to be written as a subroutine instead of as a function,
   what changes would need to be made to the loop in the main program (rewrite the loop with the
   necessary changes). Pass your paper in to the TA.
   Now write the IsAPrime function. To determine whether an integer N is a prime number, test
   all integers smaller than N to see if you can find an integer that is a divisor of N:

        Let logical variable PRIME be .TRUE.
        Let lcv be 2
        DO WHILE (PRIME is .TRUE. and lcv is less than N)
           IF lcv is a divisor of N THEN
              Let PRIME be .FALSE.
           ELSE
              Increment lcv
           END IF
        END DO
        return PRIME as the result of the function

   Compile your program.
   Create a typescript file that shows a sample execution.
   Turn in your program and typescript file electronically by typing in the command

   /cs/bin/turnin submit cs1001 lab4 lab4.f90 lab4.script

See you next week!



Adapted From Prof. Glynis Hamel

About this document ...

This document was generated using the LaTeX2HTML translator Version 97.1 (release) (July 13th,
1997)

Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit,
University of Leeds.

The command line arguments were:
latex2html lab4.tex.



The translation was initiated by Nitin John on 4/12/2000

Nitin John
4/12/2000