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

In summary: Output statement to provide the trajectory on a graph. The input variables are the initial velocity and the height of the object above the ground.In summary, the program finds the trajectory of the object by first calculating the velocity (which is the input) and then plotting it on a graph.
  • #1
Bussell93
9
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
  • #2
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.
 
  • #3
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 :)
 
  • #4
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
  • #5
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
  • #6
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!
 
  • #7
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
 
  • #8
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

1. What is Fortran 90?

Fortran 90 is a high-level programming language used for scientific and engineering applications. It is an extension of the original Fortran language and was released in 1991. It offers new features such as modules, dynamic memory allocation, and structured programming constructs.

2. How do I get started with Fortran 90?

To get started with Fortran 90, you will need to have a Fortran compiler installed on your computer. There are several options available, such as GNU Fortran, Intel Fortran, and IBM XL Fortran. Once you have a compiler, you can begin learning the syntax and structure of the language.

3. What are some common mistakes made by Fortran 90 beginners?

Some common mistakes made by Fortran 90 beginners include forgetting to declare variables, not using the appropriate data types, and not properly using control structures such as loops and if statements. It is important to thoroughly understand the language and its syntax to avoid these errors.

4. Are there any resources available for learning Fortran 90?

Yes, there are many resources available for learning Fortran 90, including online tutorials, books, and courses. Some popular resources include Fortran Wiki, Fortran 90 for Scientists and Engineers by Stephen Chapman, and Coursera's "Modern Fortran" course.

5. What are some advantages of using Fortran 90?

Fortran 90 is a powerful language for scientific and engineering applications due to its efficient handling of numerical computations and its support for parallel programming. It also has a large library of mathematical functions and is widely used and supported in the scientific community.

Similar threads

  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
980
  • Programming and Computer Science
Replies
5
Views
617
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top