1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran:store loop value in array

  1. Mar 6, 2015 #1
    1. The problem statement, all variables and given/known data

    My program calculates the value of a function. Then it prints them on the screen. This is the part:

    Code (Text):
    do while(x<=b)
    variablesvalues(1)=x
    x=x+delta
    call recog_variables (func, variables)
    answer=evaluate(variablesvalues)
    print*, answer
    end do
     
    What I need to do now is to save theese answers into array and then work with array.
    I don't know how to do it, since I tryed some ways and I get some errors which I don't understand...

    2. Relevant equations


    3. The attempt at a solution


    Code (Text):

    niz1(i)=answer
    1 do while(x<=b)
    variablesvalues(1)=x
    x=x+delta
    call recog_variables (func, variables)
    answer=evaluate(variablesvalues)

    call destroyfunc()
    end do
    print*, niz1
     

    This version works but when I compile it I get this, which has nothing to do with the numbers I should get:
    Code (Text):
      -1.00291508E-09   4.56444949E-41  -3.73914588E-09   4.56444949E-41  -1.00340003E-09   4.56444949E-41              NaN   0.00000000       0.00000000       0.00000000  
     


    My opinion: there should be, in my do while loop, nested do loop which is supposed to write the values in the array niz1(i), but I can't declarate it since I don't know how many values will I get. It depends on the interval which is inputed...


    P.S. How can i insert the FORTRAN code? I have only option to insert java,php,css...
     
  2. jcsd
  3. Mar 6, 2015 #2

    Mark44

    Staff: Mentor

    In each iteration of the loop, the code above stores the current value of x into variablesvalue(1), the first cell in your array. The call to evaluate() is passed the array, but it might as well have been called with x as the parameter. I don't get what you're trying to do here.
    Declare the array with at least as many elements as you think you will need. Another alternative is to use dynamic-sized arrays, a feature that was added to Fortran sometime after I stopped writing any Fortran code.

    Your code above isn't complete, so it's difficult to see what's going on. In the first line you set niz1(i) to answer. This implies that that line of code is inside a counting loop (do i = 1, n). Why are you setting variablesvalues(1) to x? Why is variablesvalues an array if you're only using the first element of the array?
    Seeing your complete program would help.
     
  4. Mar 6, 2015 #3
    If I put avaluate(x) I get the error. There are used modules and many subroutines. I got the routine online, someone shared it and gave instructions which parts I should change in order to use it in my code...

    And, my code is here. This is the first version which works but doesn't store the values into array.

    Code (Text):
    program ekstremi

    use interpreter

    !Variables used by the evaluator to build and to determine the value of the expressions
    real(kind=8)::X
    character(len = 10),  dimension(6) :: variables
    real(kind=8),       dimension(100) :: variablesvalues


    !String variable that will store the function that the evaluator will build
    character (len = 275)  :: func

    !String variable that will return the building of the expression result
    !If everything was ok then statusflag = 'ok', otherwise statusflag = 'error'
    character (len = 5)  :: statusflag


    real(kind=8)     :: answer
    integer::broj,a,b
    real::delta=0.1
    real, dimension(100)::niz1

    print*, 'Dobrodosli! Program racuna  minimum i maksimum unesene funkcije. &
             Unesite zeljenu funkciju:'
    read(*,*) func
    print*, 'Unesite pocetnu i krajnju tocku intervala:'

    read(*,*)a,b


    !Settings
    !The number of variables can be increased
    variables(1) = 'x'
    x=a


    print*,'Izaberi broj:      1)za zatvoreni interval [a,b] &
                               2)za otvoreni interval <a,b> &
                               3)za poluotvoreni interval <a,b] &
                               4) za poluzatvoreni interval [a,b>'

    read(*,*) broj

    select case(broj)
    case(1); goto 1
    case(2); goto 2
    case(3); goto 3
    case(4); goto 4
    end select


    !this is the part which I need to modify in order to store values in array but at the moment not sucessful in that...
    1 do while(x<=b)
    variablesvalues(1)=x
    x=x+delta
    call recog_variables (func, variables)
    answer=evaluate(variablesvalues)
    call destroyfunc()
    end do



    2 variablesvalues(1)=x+delta
    3 variablesvalues(1)=x+delta
    4 variablesvalues(1)=x



    end program
     
     
  5. Mar 6, 2015 #4

    Mark44

    Staff: Mentor

    Code (Fortran):

    integer i !! Added by Mark44
    real (kind = 8) dimension(100) : answerList !! Added by Mark44 -- make larger if you need to. It should have (b - a + 1) * delta cells.
    .
    .
    i = 1 !! Added by Mark44
    .
    .
    .

    !this is the part which I need to modify in order to store values in array but at the moment not sucessful in that...
    1 do while(x<=b)
          variablesvalues(1)=x
          x=x+delta
          call recog_variables (func, variables)
          answerList(i) = evaluate(variablesvalues)  !! Changed
          i = i + 1
          call destroyfunc()
      end do
    Maybe something like this. One thing you'll have to figure out is how big to make what I'm calling answersList. It needs to have (b - a + 1) * delta elements. Possibly you could use dynamic array allocation, which I'm not very well versed in.

    Also, for Fortran code, start off with code=fortran in you code tag.
     
  6. Mar 6, 2015 #5

    Thank you very much!!
    Firstly I declared it as dimension 100, for example but if I have 20 numbers to put there then I will get more 80 numbers which should't be there, I suposse fortran put them there from somewhere in the memory....

    But than I allocated it and it works. Just one thing, i think thats (b-a+1)*delta isn't correct, I counted it and thought a bit, and think it's (b-a)/delta+1...

    It's like this now:
    Code (Text):
    real(kind=8),dimension(:),allocatable::answerlist
    integer::i=1

    read(*,*)a,b

    d=(b-a)/delta+1
    allocate (answerlist(d))
    .
    .
    .
    do while(x<=b)
    variablesvalues(1)=x
    x=x+delta
    call recog_variables (func, variables)
    answerlist(i)=evaluate(variablesvalues)
    i=i+1
    call destroyfunc()
    end do
    print*,answerlist
    deallocate(answerlist)
     

    Once again, thank a lot!
     
  7. Mar 6, 2015 #6

    Mark44

    Staff: Mentor

    You're right about the thing with delta. I didn't put that much thought into it.

    As for you array, if you put 20 numbers in, then only read 20 numbers out. Instead of doing this -- print *, answerlist -- use a counting loop, printing out one element of the array per iteration:
    Code (Fortran):
    do i = 1, d
       print *, answerlist(i)
    end do
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Fortran:store loop value in array
  1. Fortran : Using arrays (Replies: 18)

Loading...