Created
March 10, 2010 21:26
-
-
Save natebenes/328425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROGRAM calculateAverage | |
IMPLICIT NONE | |
! Programmer: Nate Benes | |
! Purpose: Calculates the average of integers entered into the console | |
! Note: We are guaranteed only integers will be entered | |
INTEGER :: sum ! Keep a running total | |
INTEGER :: count ! Keep track of how many numbers were entered | |
INTEGER :: input ! A place for the user's input | |
! Initialize our variables | |
sum = 0 | |
count = 0 | |
DO | |
READ(*,*) input ! fill up input everytime | |
IF (input == 0) THEN | |
EXIT ! User wants to quit | |
END IF | |
sum = sum + input ! increment our counters | |
count = count + 1 | |
END DO | |
IF (count == 0) THEN ! Don't divide by 0 or program will crash | |
WRITE(*,*) "No values entered" | |
ELSE ! Calculate the average | |
! Force sum and count to prevent integer division issues | |
WRITE(*,*) REAL(sum)/REAL(count) | |
END IF | |
END PROGRAM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment