Fortran:store loop value in array

In summary: The number of elements in the array variablesvaluesvariablesvalues(1) = xcall recog_variables (func, variablesvalues)!The number of iterations in the do while loopdo while(x<=b)!The code that will be executedcall eval_func (x, a, b, func)!The code that will be executed if the do while loop failscall destroyfunc ()!The code that will be executed if the statusflag variable is setstatusflag = 'error'end doend doDobrodosli! Program
  • #1
Antonija
18
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
  • #2
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...
 
  • #3
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
 
  • #4
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.
 
  • #5
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!
 
  • #6
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
 

FAQ: Fortran:store loop value in array

What is a "store loop value" in Fortran?

A "store loop value" in Fortran refers to the process of saving a specific variable or data value into an array during a loop iteration. This allows the values to be accessed and manipulated later on in the program.

How do I store loop values in an array in Fortran?

To store loop values in an array in Fortran, you will need to declare an array variable and use a loop structure such as DO or DO WHILE to iterate through the desired values. Within the loop, you can use the array index to assign the values to the array.

Can I store loop values in multiple arrays in Fortran?

Yes, you can store loop values in multiple arrays in Fortran by declaring multiple array variables and using the same loop structure to assign values to each array index. This allows for efficient storage and organization of data in the program.

What are the benefits of storing loop values in an array in Fortran?

Storing loop values in an array in Fortran allows for easy access and manipulation of data, as well as efficient use of memory. It also allows for the use of array operations, such as finding the maximum or minimum value, which can save time and effort in programming.

Are there any limitations to storing loop values in an array in Fortran?

One limitation of storing loop values in an array in Fortran is that the array size must be declared before the program runs. This means that the total number of values to be stored must be known beforehand, which may not always be possible. Additionally, if the array size is too small, it may result in an overflow error.

Similar threads

Replies
2
Views
3K
Replies
3
Views
6K
Replies
17
Views
2K
Replies
2
Views
919
Replies
13
Views
3K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
5
Views
7K
Back
Top