Fortran:store loop value in array

  • Context: Comp Sci 
  • Thread starter Thread starter Antonija
  • Start date Start date
  • Tags Tags
    Array Loop Value
Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming problem where participants are trying to store the results of a function evaluation in an array during a loop. The scope includes homework-related coding challenges and technical explanations of array handling in Fortran.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their initial attempt to store function evaluation results in an array but encounters unexpected output and errors.
  • Another participant suggests that the array should be declared with a sufficient size and mentions the possibility of using dynamic arrays.
  • A participant questions the logic behind using only the first element of an array for storing values and suggests that a counting loop might be necessary for proper indexing.
  • There is a proposal to modify the loop to correctly store evaluated results in an array, with a suggestion to allocate the array dynamically based on the expected number of results.
  • One participant reflects on the calculation of the required size for the array, correcting a previous assumption about the formula used to determine the number of elements needed.
  • Another participant emphasizes the importance of printing array elements correctly by using a loop to iterate through the array.

Areas of Agreement / Disagreement

Participants express varying opinions on how to handle array sizes and dynamic allocation, with some agreeing on the need for dynamic arrays while others provide different perspectives on the implementation details. The discussion remains unresolved regarding the best approach to store and print the array values.

Contextual Notes

Participants mention limitations related to the size of arrays and the need for dynamic allocation, as well as the potential for memory issues if arrays are not properly managed. There are also unresolved questions about the overall structure of the code and the specific requirements for the function being evaluated.

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 successful 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 successful 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 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K