Problem Statement
The arithmetic mean (i.e., average), geometric mean and harmonic mean of a set of n numbers x1, x2, ..., xn are defined as follows:

Since computing geometric mean requires taking square root, it is further required that all input data values must be positive. As a result, this program must be able to ignore those non-positive items. However, this may cause all input items ignored. Therefore, before computing the means, this program should have one more check to see if there are valid items.
Solution
! ----------------------------------------------------------
! This program reads a series of input data values and
! computes their arithmetic, geometric and harmonic means.
! Since geometric mean requires taking n-th root, all input
! data item must be all positive (a special requirement of
! this program , although it is not absolutely necessary).
! If an input item is not positive, it should be ignored.
! Since some data items may be ignored, this program also
! checks to see if no data items remain!
! ----------------------------------------------------------
PROGRAM ComputingMeans
IMPLICIT NONE
REAL :: X
REAL :: Sum, Product, InverseSum
REAL :: Arithmetic, Geometric, Harmonic
INTEGER :: Count, TotalNumber, TotalValid
Sum = 0.0 ! for the sum
Product = 1.0 ! for the product
InverseSum = 0.0 ! for the sum of 1/x
TotalValid = 0 ! # of valid items
READ(*,*) TotalNumber ! read in # of items
DO Count = 1, TotalNumber ! for each item ...
READ(*,*) X ! read it in
WRITE(*,*) 'Input item ', Count, ' --> ', X
IF (X <= 0.0) THEN ! if it is non-positive
WRITE(*,*) 'Input <= 0. Ignored' ! ignore it
ELSE ! otherwise,
TotalValid = TotalValid + 1 ! count it in
Sum = Sum + X ! compute the sum,
Product = Product * X ! the product
InverseSum = InverseSum + 1.0/X ! and the sum of 1/x
END IF
END DO
IF (TotalValid > 0) THEN ! are there valid items?
Arithmetic = Sum / TotalValid ! yes, compute means
Geometric = Product**(1.0/TotalValid)
Harmonic = TotalValid / InverseSum
WRITE(*,*) 'No. of valid items --> ', TotalValid
WRITE(*,*) 'Arithmetic mean --> ', Arithmetic
WRITE(*,*) 'Geometric mean --> ', Geometric
WRITE(*,*) 'Harmonic mean --> ', Harmonic
ELSE ! no, display a message
WRITE(*,*) 'ERROR: none of the input is positive'
END IF
END PROGRAM ComputingMeans
Click here to download this program. Program Input and Output
This program uses the same format of input as discussed in previous example. More precisely, the first line gives the number of data items, followed by that number of input line on each which is a data value. Except for the first input value, which gives the number of input, all other values are real numbers.- If the input data is
it will generate the following output. In this input, all data values are positive and none of them is ignored.5 1.0 2.0 3.0 4.0 5.0
Input item 1 --> 1. Input item 2 --> 2. Input item 3 --> 3. Input item 4 --> 4. Input item 5 --> 5. No. of valid items --> 5 Arithmetic mean --> 3. Geometric mean --> 2.6051712 Harmonic mean --> 2.18978071
- In the following input, the fourth value is negative.
The output is shown below:6 1.0 2.0 3.0 -4.0 5.0 6.0
Input item 1 --> 1. Input item 2 --> 2. Input item 3 --> 3. Input item 4 --> -4. Input <= 0. Ignored Input item 5 --> 5. Input item 6 --> 6. # of items read --> 6 # of valid items -> 5 Arithmetic mean --> 3.4000001 Geometric mean --> 2.82523465 Harmonic mean --> 2.27272725
- Now, let us try the following input in which all values are non-positive:
We shall get the following output. The program correctly detects there is no valid data values and displays a message.4 -1.0 -2.0 0.0 -3.0
Input item 1 --> -1. Input <= 0. Ignored Input item 2 --> -2. Input <= 0. Ignored Input item 3 --> 0.E+0 Input <= 0. Ignored Input item 4 --> -3. Input <= 0. Ignored ERROR: none of the input is positive
No comments:
Post a Comment
=(*_*)------------------------(^_^)=
:::::|berkomentar dengan sopan adalah akhlak kemulian|:::::