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

  • Context: Fortran 
  • Thread starter Thread starter Bussell93
  • Start date Start date
  • Tags Tags
    Beginner Fortran
Click For Summary

Discussion Overview

The discussion revolves around programming in Fortran, specifically focusing on calculating the average of a set of numbers and finding the trajectory of an object given initial velocity components. Participants share their code, seek help with debugging, and discuss programming concepts related to loops and input handling.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant struggles with a Fortran program that consistently outputs zero when calculating the average of numbers, questioning the need for arrays and the use of the SUM intrinsic.
  • Another participant suggests checking if the program is reading numbers correctly and whether the sum is being accumulated properly.
  • A participant later confirms that the issue was due to not reading input correctly and describes a successful approach of summing numbers before dividing by the count.
  • In a separate query, a participant seeks assistance with a program to calculate the trajectory of an object, expressing confusion about input handling within a loop.
  • Another participant points out that the repeated input prompts in the trajectory program are caused by the READ statements being inside the loop and suggests a potential fix.
  • There is a technical note about the syntax of the DO loop in Fortran, specifically regarding the placement of commas.

Areas of Agreement / Disagreement

Participants generally agree on the need to read inputs correctly and the structure of loops in Fortran. However, there are differing views on the necessity of using arrays and the best practices for input handling in the context of the trajectory program.

Contextual Notes

Some participants express uncertainty about specific Fortran products and compilers, indicating a lack of familiarity with recent developments in Fortran programming tools.

Who May Find This Useful

This discussion may be useful for students learning Fortran programming, particularly those dealing with input handling, loops, and basic numerical calculations in their coding assignments.

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   Reactions: 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   Reactions: 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   Reactions: 1 person

Similar threads

  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 59 ·
2
Replies
59
Views
12K
Replies
7
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K