How Can I Improve My Fortran Program for Calculating Different Types of Means?

  • Context: Fortran 
  • Thread starter Thread starter rk2ray
  • Start date Start date
  • Tags Tags
    Code Fortran
Click For Summary

Discussion Overview

The discussion revolves around improving a Fortran program designed to calculate various types of means, including arithmetic mean, root-mean-square average, geometric mean, and harmonic mean for a set of numbers. Participants are exploring the implementation details and seeking guidance on potential issues within the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant requests guidance on whether their approach to writing the Fortran program is correct.
  • Another participant suggests calculating the means by hand with a different sequence of numbers to compare results with the program's output.
  • A third participant humorously asserts that the "write way is wrong" without providing specific feedback.
  • A participant mentions their background in basic Fortran programming and expresses difficulty in extending their knowledge to this more complex program.
  • One participant notes they do not see any obvious mistakes in the provided code, asking for clarification on what the original poster is specifically struggling with.

Areas of Agreement / Disagreement

There is no consensus on the correctness of the program or the specific issues faced by the original poster. Participants express differing views on the clarity of the request for help and the effectiveness of the code.

Contextual Notes

Participants have not reached a resolution regarding the specific problems in the code or the best practices for calculating the means. The discussion includes varying levels of experience with Fortran programming, which may affect the perspectives shared.

Who May Find This Useful

Individuals interested in Fortran programming, particularly those working on statistical calculations or seeking to improve their coding skills in the context of mathematical computations.

rk2ray
Messages
7
Reaction score
0
Trying to write a single Fortran program that calculates the arithmetic mean, rms (root-mean-square) average, geometric mean, and harmonic mean for a set of numbers (for example - 4 4 4 4 4).

I don't know if I am doing the write way. Please guide me.

PROGRAM ComputingMeans

IMPLICIT NONE

INTEGER :: Count
INTEGER :: Valid
INTEGER :: i

REAL :: Item
REAL :: Arithmetic, Geometric, Harmonic ! Arithmetic, geometric and harmonic mean


WRITE(*,*) "Write count of items:"
READ(*,*) Count

! Reads items and computes arithmetic, geometric and harmonic mean
Valid = 0
Arithmetic = 0.
Geometric = 1.
Harmonic = 0.
DO i = 1, Count
WRITE(*,*) "Write items:"
READ(*,*) Item
IF (Item > 0) THEN
Valid = Valid + 1
Arithmetic = Arithmetic + Item
Geometric = Geometric * Item
Harmonic = Harmonic + (1 / Item)
END IF
END DO

! Writes results on screen
IF (Valid > 0) THEN
WRITE(*,"(A,I3,A)") "Count of valid items: ", Valid, "."
Arithmetic = Arithmetic / Valid
Geometric = Geometric**(1./Valid)
Harmonic = Valid / Harmonic
WRITE(*,"(A,F9.6,A)") "Arithmetic mean: ", Arithmetic, "."
WRITE(*,"(A,F9.6,A)") "Geometric mean: ", Geometric, "."
WRITE(*,"(A,F9.6,A)") "Harmonic mean: ", Harmonic, "."
ELSE
WRITE(*,*) "ERROR: None of the input is positive!"
END IF

END PROGRAM ComputingMeans
 
Technology news on Phys.org
do you know how to calculate such quantities by hand?
then,
pick a sequence, preferably not 4 4 4 4 4, but another sequence with different numbers
calculate the quantities by hand
run your program
provide the sequence
see what your program calculates and how it compares to the answers
that's easy
then, what might get difficult is go back and figure out why something may not be right

other than that, your "please guide me" request seems way too general for somebody who wrote the program as shown...no specifics whatsoever? that's odd.
 
The write way is wrong. The right way is right.
 
I am aware of the hand calculations.

I am new to fortran programming language and I have written basic programs for average calculation of three number.

But I am stuck with this.

I was trying to write single Fortran program that calculates the arithmetic mean, rms (root-mean-square) average, geometric mean, and harmonic mean for a set of numbers. (that can be 4 4 4 4 4 or 1 2 3 4 5 6 7 or 4, 1, 4, 7, 4, 1, 7).

I have been trying to write it but I'm not able to get it.
 
rk2ray said:
But I am stuck with this.
As gsal said, tell us exactly what you are "stuck" with.

I can't see any obvious mistakes in the code that you posted.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K