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

  • Thread starter Thread starter rk2ray
  • Start date Start date
  • Tags Tags
    Code Fortran
AI Thread Summary
The discussion focuses on creating a Fortran program to calculate various means, including arithmetic, geometric, and harmonic means, for a set of numbers. The original poster expresses confusion about whether their approach is correct and seeks guidance. Participants suggest testing the program with different sequences of numbers and comparing results with manual calculations to identify potential errors. They also emphasize the need for the poster to specify the exact issues they are facing, as the provided code appears mostly correct. Overall, the conversation highlights the importance of debugging through comparison and clarifying specific programming challenges.
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
3
Views
3K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
8
Views
2K
Back
Top