Fortran How can I fix my program to calculate the average of a set of numbers correctly?

  • Thread starter Thread starter Bussell93
  • Start date Start date
  • Tags Tags
    Beginner Fortran
AI Thread Summary
A first-year physics undergraduate is seeking help with a Fortran program to calculate the average of a set of numbers, which is returning zero regardless of input. The discussion highlights the need for proper input reading, incrementing a count, and accumulating a sum within a loop, clarifying that using an array or the SUM intrinsic is unnecessary. The user also inquires about downloading Fortran for Windows and receives guidance on using text editors for coding. Additionally, the user shares a separate trajectory program but is advised that the repeated input prompts are due to the READ statements being inside the loop, which should be modified for correct functionality. The conversation emphasizes the importance of correctly structuring loops and input handling in programming.
Bussell93
Messages
9
Reaction score
0
Hi, I'm a first year physics undergrad studying the fortran language with NAG compiler for computer programming.

Question 1: How do I write a program that finds the average of a set of n numbers?
I've written a program already but my answers are continuing to be 0.0000, no matter what values I input.
Do I have to use an array because the question I'm trying to answer suggests not to use one. However, I think I need to use the SUM intrinsic, but this is only valid for arrays.
Please help!

Many Thanks :smile:
 
Technology news on Phys.org
Here's a checklist:
* Are you reading the numbers into begin with. If you edit the program so that the input and output statements are still there with nothing more than an assignment statement setting the average to the input value do you still get zero?
* For each new number you read, you are incrementing a count and accumulating to a sum. Does the sum look something like "sum = sum + new"?
* You are performing a division at the end. Is the variable that holds the result of the division the same one that you are printing out?

My guess is that you are not reading the numbers into begin with.
 
Thanks for your reply, I'm not in the lab but I'll check tomorrow and get back as you're probably correct. Also, do you know how I am able to download fortran for my windows laptop? I have a cd containing Fortran 4.91 and I am also sure that I need a compiler and an editor to make it work correctly?
I usually use a Linux with Terminal and Gedit editor.

Any help would be appreciated :)
 
Bussell93 said:
Hi, I'm a first year physics undergrad studying the fortran language with NAG compiler for computer programming.

Question 1: How do I write a program that finds the average of a set of n numbers?
I've written a program already but my answers are continuing to be 0.0000, no matter what values I input.
Do I have to use an array because the question I'm trying to answer suggests not to use one. However, I think I need to use the SUM intrinsic, but this is only valid for arrays.
Please help!

Many Thanks :smile:

Show us the code you have written. In response to some of your questions, you do not have to use an array, and hence you don't need to use the SUM intrinsic. In any case, it's probably better that you don't, as you will get more experience writing your own code.

You will need to have a loop that executes some number of times. How many times it runs, and the type of loop depend on how many numbers you need to add and what is used to indicate how many of them there are.

Inside the body of the loop you need to do these things:
1. Input a number.
2. Increment a loop counter (assumed to start at 0).
3. Accumulate the input number onto a running total (also assumed to start at 0).

When your loop finishes, you should have the sum of all of the input numbers and the number of inputs. The average is the quotient of these two numbers.

Again, show us your code.
 
  • Like
Likes 1 person
Bussell93 said:
Thanks for your reply, I'm not in the lab but I'll check tomorrow and get back as you're probably correct. Also, do you know how I am able to download fortran for my windows laptop? I have a cd containing Fortran 4.91 and I am also sure that I need a compiler and an editor to make it work correctly?
I usually use a Linux with Terminal and Gedit editor.

Any help would be appreciated :)
Sorry. I'm not familiar with specific Fortran products - at least not recent ones. I haven't coded in Fortran in about 12 years. If you're taking about the Silverfox compiler, I would put the CD is and see what it does. You can create and edit the source files with notepad, wordpad, one of the Microsoft IDE's, or any of made simple text editors.
 
  • Like
Likes 1 person
I've managed to do it correctly now, it was due to me not reading in the numbers each time that were being inputed by the keyboard. Instead of trying to calculate the average as the program goes, I've now summed all the numbers and then divided by n at the very end. Thanks for all your help - very much appreciated!
 
Question 2 - finding the trajectory of an object with initial velocity

I have written a program to find the trajectory of an object, after the initial components of the velocity are entered at the keyboard. The program contains a DO loop (which I want as the time, with time intervals of 0.01s, starting at t=0 and ending at t=10)
However, I want the program to automatically calculate the final velocity after every 0.01s so then it can carry on working it out all the way to t=10s. However, the program still asks me to insert values for the initial velocity components.

I don't have any computer programming experience so please help in simple terms!
Thanks :smile:

Here is the code so far:

!This program finds the trajectory of an object
!when the initial components of the velocity are entered at the keyboard

PROGRAM trajectory
IMPLICIT NONE

REAL :: x,ux,x_new,ux_new,y,uy,y_new,uy_new,t,i,tint,ax, ay,vx,vy

ax = 0.0
ay = -9.81
x = 0.0
y = 0.0
tint = 0.01

WRITE(6,*)'This program calculates the trajectory of an'
WRITE(6,*)'object, when the initial components of the'
WRITE(6,*)'velocity are entered at the keyboard'
DO,t=0.0,10.0,0.01
WRITE(6,*)'Enter the horizontal component of the'
WRITE(6,*)'initial velocity, ux at t=',t
READ(5,*) ux
WRITE(6,*)''
WRITE(6,*)'Enter the vertical component of the'
WRITE(6,*)'initial velocity, uy at t=',t
READ(5,*) uy
IF (ux<0 .OR. uy<0) THEN
CYCLE
ELSE
x_new = x + ux*t + 0.5*ax*(t**2)
y_new = y + uy*t + 0.5*ay*(t**2)
IF (y_new<0) EXIT
END IF
ux = ux + ax*(t)
uy = uy + ay*(t)
END DO
WRITE(6,*)'Final x position =', x_new
WRITE(6,*)'Final y position =', y_new
WRITE(6,*)'Final Horizontal velocity =',vx
WRITE(6,*)'Final Vertical velocity =',vy

END PROGRAM trajectory
 
In the future, when you want to discuss a new problem, please start a new thread.

I have copied your DO loop below. The reason that your code keeps asking for input values is that the READ statements are inside your loop. Each iteration of the loop causes your program to ask for the horizontal and vertical components of the velocity, and then take input for ux and uy. Can you think of how to fix this?
Code:
DO,t=0.0,10.0,0.01
  WRITE(6,*)'Enter the horizontal component of the'
  WRITE(6,*)'initial velocity, ux at t=',t
  READ(5,*) ux
  WRITE(6,*)''
  WRITE(6,*)'Enter the vertical component of the'
  WRITE(6,*)'initial velocity, uy at t=',t
  READ(5,*) uy
  IF (ux<0 .OR. uy<0) THEN
     CYCLE
  ELSE
     x_new = x + ux*t + 0.5*ax*(t**2)
     y_new = y + uy*t + 0.5*ay*(t**2)
     IF (y_new<0) EXIT 
  END IF
  ux = ux + ax*(t)
  uy = uy + ay*(t)
 END DO

Your DO loop has "DO, t=0.0,10.0,0.01" - There should not be a comma between DO and t.
 
  • Like
Likes 1 person

Similar threads

Replies
14
Views
5K
Replies
59
Views
11K
Replies
9
Views
2K
Replies
6
Views
3K
Replies
3
Views
3K
Replies
5
Views
2K
Replies
8
Views
4K
Back
Top