Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help for fortran 90 beginner

  1. Jan 23, 2014 #1
    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:
     
  2. jcsd
  3. Jan 23, 2014 #2
    Here's a checklist:
    * Are you reading the numbers in to 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 in to begin with.
     
  4. Jan 23, 2014 #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 :)
     
  5. Jan 23, 2014 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Jan 23, 2014 #5
    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.
     
  7. Jan 24, 2014 #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!
     
  8. Jan 24, 2014 #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
     
  9. Jan 24, 2014 #8

    Mark44

    Staff: Mentor

    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 (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help for fortran 90 beginner
  1. Help with Fortran 90 (Replies: 0)

  2. Beginner Fortran Help (Replies: 4)

Loading...