Need Help with my Fortran Code

In summary: However, it is always helpful to get a little more information about the problem you are trying to solve. For example, it would be helpful to know what kind of data you are working with (integer, real, etc.), and what type of arithmetic, geometric, and harmonic mean you are trying to calculate. Also, it would be helpful to know what sequence you are working with.
  • #1
rk2ray
9
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
  • #2
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.
 
  • #3
The write way is wrong. The right way is right.
 
  • #4
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.
 
  • #5
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.
 

1. What is Fortran?

Fortran is a high-level programming language primarily used for scientific and numerical computing. It is widely used in fields such as physics, engineering, and meteorology.

2. Why do I need help with my Fortran code?

Fortran is a complex language with many features and nuances. It is common for programmers, especially beginners, to encounter errors and difficulties while writing code. Seeking help from more experienced programmers can help solve these issues and improve the overall quality of the code.

3. Where can I find help with my Fortran code?

There are various resources available for getting help with Fortran code. You can consult online forums and communities, attend workshops and classes, or seek assistance from a professional programmer or tutor.

4. How can I improve my Fortran coding skills?

Practicing regularly and actively seeking feedback from more experienced programmers can help improve your Fortran coding skills. You can also refer to online tutorials and documentation to learn new techniques and best practices.

5. What are some common mistakes to avoid while writing Fortran code?

Some common mistakes in Fortran coding include not using proper syntax, not declaring variables, and using incorrect data types. It is also essential to pay attention to the order of operations and to properly close loops and conditional statements. Regularly testing and debugging your code can help identify and avoid these mistakes.

Similar threads

  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top