CS 1001 Introduction to Computers -- D Term, 2000
Assignment 3 Due: Tuesday, April 11, 2000

Purpose
This assignment will give you practice in writing a FORTRAN program that utilizes:
 
- the IF statement
- DO loops

- nested control structures

Problem

Write a FORTRAN program that can do the following:
- compute the average of a set of real numbers,
- compute the standard deviation of a set of real numbers,

- compute the greatest common divisor of two integers

The program should output a menu that asks the user:

        What would you like to do now?
        Type:

          0 to quit
          1 to compute the average of a set of real numbers
          2 to compute the standard deviation of a set of real numbers
          3 to compute the greatest common divisor of two integers

        Your choice (type a number and press RETURN)
If the user enters a 1, 2, or 3, the program should perform the appropriate calculation then return to the menu. If the user enters a 0, the program should terminate. If the user enters any other number, the program should print out an error message, then return to the menu.

Program Design

The main part of the program is executed over and over again until the user enters 0 to terminate the program. The bulk of the program, therefore, resides in a pretest DO loop terminating with the condition that the option entered by the user was 0.
        DO
             IF (UserOption .NE. 0)...
             main part of program goes here
        ENDDO
The computational routines can be controlled with an IF statement based on the number of the option entered.
        IF (UserOption .EQ. 1) THEN
           compute the average
        ELSE IF (UserOption .EQ. 2) THEN
           compute the standard deviation
        ELSE IF (UserOption .EQ. 3) THEN
           compute the greatest common divisor
        ELSE 
           print error message
        ENDIF
The overall structure of the program, then, will look like this:
        PROGRAM HW3
        {declarations for all variables go here}

        print out menu
        read in UserOption

        DO
            IF (UserOption .NE. 0)...
      
           IF (UserOption .EQ. 1) THEN
               {all the statements that calculate the average go here}
           ELSE IF (UserOption .EQ. 2) THEN
               {all the statements that calculate the standard deviation
                go here}
           ELSE IF (UserOption .EQ. 3) THEN
               {all the statements that calculate the greatest common
                divisor go here}
           ELSE
               print an error message
           ENDIF

           print out menu
           read in UserOption

        END DO
        
        END HW3

Writing the Computational Routines

AVERAGING ALGORITHM
The code for the averaging routine should ask the user how many numbers she wants to average. Use a Pretest DO  loop to get the numbers. Each time through the loop, prompt the user for the next number, read it in, and maintain a running total as each number is entered.
        prompt user to enter number-of-numbers to average
        get number-of-numbers
        initialize running-total to 0
        initialize number-left to number-of-numbers
        DO IF number-left > 0
                prompt user for next number
                get number
                add to running-total
                decrement number-left
        END DO
        write out average (running-total/number-of-numbers)
STANDARD DEVIATION ALGORITHM
The code for the standard deviation routine is very similar to that of the averaging routine, except that the formula for calculating the standard deviation requires you to also keep track of the sum of the squares of the numbers entered. For computational purposes, the standard deviation can be calculated this way:

StdDev = square root of ( (S2 -  (SUM**2) /N) /(N-1) )  where

S2
is the sum of the squares of the numbers
SUM
is the sum of the numbers
N
is the number of numbers
GREATEST COMMON DIVISOR ALGORITHM
The greatest common divisor can be calculated this way:

a). divide A, the larger of the two numbers, by B, the smaller, and get the remainder R (where 0 < = R < B) . If R = 0, then B is the greatest common divisor.

b). If  R <> 0, do (a) again, but use B as the new larger number, A, and use R as the new smaller number, B. The algorithm follows:

        prompt for the two numbers and read them in
        determine which of the two numbers is larger, and put it in Large
        put the other number in Small
        calculate the Remainder of Large divided by Small (use function MOD)
        DO IF (Remainder .NE. 0)
              put the smaller number in Large
              put the Remainder in Small
              calculate the Remainder of Large divided by Small
        ENDDO
        write out the greatest common divisor (the value of Small)

Assumptions and Restrictions

You may assume that all data entered by the user of the program is of the correct data type. You may assume that the "number of numbers'' entered for the averaging and standard deviation calculations is greater than or equal to 1.

Grading

This assignment will be graded based on 100 points. Point values will be allocated as follows:
Documentation
(20 points) Your program should begin with an introductory comment that gives a brief description of what the program does. Your name and section number, your wpi login name, and the date should also be included with the introductory comments. Variable names should be meaningful. If a variable needs further explanation, it should be commented (where it is declared) to describe its purpose in the program. Main sections of the program should be separated with blank lines and described with comments. The program should exhibit proper and consistent use of indentation to help delimit the various control structures.
Functionality
(60 points) The program should work correctly on our test data.
Output
(20 points) The program should include an introductory message to the user, useful prompts, and properly labeled output. Blank lines and white space should be used to make the output readable.

Deliverables

Using the turnin program, turn in your Fortran source file and a typescript file showing successful compilation and a sample execution. Your files must be turned in prior to 12:00 p.m. on April 11, 2000  to receive full credit. The command you should use to turn in this assignment is: /cs/bin/turnin submit cs1001 hw3 hw3.f90 hw3.script


Adapted From Prof. Glynis Hamel