Comp Sci Fortran:store loop value in array

  • Thread starter Thread starter Antonija
  • Start date Start date
  • Tags Tags
    Array Loop Value
AI Thread Summary
The discussion focuses on modifying a Fortran program to store computed function values in an array for further processing. The user initially struggles with storing results in an array, receiving unexpected output when attempting to do so. Suggestions include declaring an appropriately sized array and using dynamic allocation to handle varying input sizes. The user successfully implements dynamic allocation and corrects the calculation for the array size based on the input interval and delta. The conversation concludes with advice on printing array elements individually to avoid displaying uninitialized values.
Antonija
Messages
18
Reaction score
0

Homework Statement



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

Code:
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...

Homework Equations

The Attempt at a Solution

Code:
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:
  -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...
 
Physics news on Phys.org
Antonija said:

Homework Statement



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

Code:
do while(x<=b)
variablesvalues(1)=x
x=x+delta
call recog_variables (func, variables)
answer=evaluate(variablesvalues)
print*, answer
end do
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.
Antonija said:
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...

Homework Equations

The Attempt at a Solution

Code:
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:
  -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...
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.
Antonija said:
P.S. How can i insert the FORTRAN code? I have only option to insert java,php,css...
 
Mark44 said:
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.

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:
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)  :: statusflagreal(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=aprint*,'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
 
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.
 
Mark44 said:
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.
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 that's (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:
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!
 
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:
Fortran:
do i = 1, d
   print *, answerlist(i)
end do
 

Similar threads

Replies
2
Views
3K
Replies
3
Views
7K
Replies
17
Views
2K
Replies
2
Views
1K
Replies
13
Views
3K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
5
Views
7K
Back
Top