- the IF statement- DO loops
- nested control structures
- 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.
DO
IF (UserOption .NE. 0)... main part of program goes here ENDDOThe 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 ENDIFThe 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
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
StdDev = square root of ( (S2 - (SUM**2) /N) /(N-1) ) where
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)