Home > Archive > Fortran > December 2006 > writing in array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| nakisa 2006-12-13, 4:18 pm |
| hi every body
i have a question about array .
in this program
allocate A(1000)
do x=1,100,1
i=i+1
function =f(x)
A(i)=function
end do
this program works correctly , fortran can't see the A(i-1) in the
this loop , why ??
i think , fortran store each number in array , and when it save in i-th
array , it can see the arreys before than , such ast (i-1)th , (i-2)th
.....
am i wrong ?
best,nakisa
| |
| Michel Olagnon 2006-12-13, 4:18 pm |
| nakisa wrote:
> hi every body
> i have a question about array .
> in this program
>
>
> allocate A(1000)
> do x=1,100,1
> i=i+1
> function =f(x)
> A(i)=function
> end do
>
> this program works correctly , fortran can't see the A(i-1) in the
> this loop , why ??
> i think , fortran store each number in array , and when it save in i-th
> array , it can see the arreys before than , such ast (i-1)th , (i-2)th
> ....
> am i wrong ?
> best,nakisa
>
Where is i initialized ?
| |
| Ian Bush 2006-12-13, 4:18 pm |
|
Hi Nakisa,
nakisa wrote:
> hi every body
> i have a question about array .
> in this program
>
>
> allocate A(1000)
> do x=1,100,1
> i=i+1
> function =f(x)
> A(i)=function
> end do
>
> this program works correctly , fortran can't see the A(i-1) in the
> this loop , why ??
> i think , fortran store each number in array , and when it save in i-th
> array , it can see the arreys before than , such ast (i-1)th , (i-2)th
> ....
> am i wrong ?
I'm not sure. I'm very sorry but I can't quite see what you are trying to
explain here. Probably the easiest way to do this would be to post the
complete test code that you are using and what error you are getting -
I can see a number of potential problems in the above couple with what you
say, but without the whole code it's difficult to say,
Ian
| |
| nakisa 2006-12-13, 4:18 pm |
| hi ,
here is my program
program test
double precision x, mult ,h
integer i , m
real , dimension (:), allocatable :: A(:)
cccccccccccccccccccccccccccccccccccccccc
cccccccccccc
open (1,file='test.txt')
allocate (A(1000))
allocate (B(1000))
x=43
i=0
do m=1,10,1
i=i+1
ans=x+m
A(i)=ans
h=A(i)*A(i-1)
write (1,*),ans,h
end do
close (1)
deallocate (A)
end program test
----------------------------------------------------------------------------------------------------------------
my question is when saving A(i) ,can fortran see A(i-1) ??? i guess
it can't ,because this program didn't answer
but when i delete "h" , it works !!!
thanks ,nakisa
| |
| nakisa 2006-12-13, 4:18 pm |
| hi ,
here is my program
program test
double precision x, ans,h
integer i , m
real , dimension (:), allocatable :: A(:)
cccccccccccccccccccccccccccccccccccccccc
cccccccccccc
open (1,file='test.txt')
allocate (A(1000))
allocate (B(1000))
x=43
i=0
do m=1,10,1
i=i+1
ans=x+m
A(i)=ans
h=A(i)*A(i-1)
write (1,*),ans,h
end do
close (1)
deallocate (A)
end program test
----------------------------------------------------------------------------------------------------------------
my question is when saving A(i) ,can fortran see A(i-1) ??? i guess
it can't ,because this program didn't answer
but when i delete "h" , it works !!!
thanks ,nakisa
| |
| Ian Bush 2006-12-13, 4:18 pm |
| nakisa wrote:
> hi ,
> here is my program
>
> program test
> double precision x, ans,h
> integer i , m
> real , dimension (:), allocatable :: A(:)
> cccccccccccccccccccccccccccccccccccccccc
cccccccccccc
> open (1,file='test.txt')
> allocate (A(1000))
> allocate (B(1000))
> x=43
>
> i=0
>
> do m=1,10,1
> i=i+1
> ans=x+m
> A(i)=ans
> h=A(i)*A(i-1)
>
> write (1,*),ans,h
>
> end do
>
> close (1)
> deallocate (A)
> end program test
> ----------------------------------------------------------------------------------------------------------------
> my question is when saving A(i) ,can fortran see A(i-1) ??? i guess
> it can't ,because this program didn't answer
> but when i delete "h" , it works !!!
> thanks ,nakisa
Classic error in the above: What is the indices of A are you using when m = 1
in the above loop ? What is the lowest allowed index of A ?
You might also want to look at your compiler documentation for "array bounds
checking" and recompile using it.
Also you should really use
Implicit None
in all your programs, even test ones,
Ian
| |
| nakisa 2006-12-13, 4:18 pm |
| hi Ian.
i can't understand your meaning , i use dynamic array (named A) ,
before starting , i wrote "i=0 " and i loop "i=i+1 " , so in each
cycle
it add one number to i ,then store it in A(i) .
i can't understad where my error is ,yet ?!!!
can you say more , thanks
| |
| nakisa 2006-12-13, 4:18 pm |
| hi Ian.
i can't understand your meaning , i use dynamic array (named A) ,
before starting , i wrote "i=0 " and i loop "i=i+1 " , so in each
cycle
it add one number to i ,then store it in A(i) .
i can't understad where my error is ,yet ?!!!
can you say more , thanks
| |
| Ian Bush 2006-12-13, 4:18 pm |
| nakisa wrote:
> hi Ian.
> i can't understand your meaning , i use dynamic array (named A) ,
> before starting , i wrote "i=0 " and i loop "i=i+1 " , so in each
> cycle
> it add one number to i ,then store it in A(i) .
> i can't understad where my error is ,yet ?!!!
> can you say more , thanks
Â_ Â_ Â_ allocate (A(1000))
i = 0
Â_ Â_ Â_ do m=1,10,1
Â_Â_Â_Â_Â_Â_Â_Â_ Â_ i=i+1
Â_Â_Â_Â_Â_Â_Â_ Â_ Â_h=A(i)*A(i-1)
Think carefully about the above line when m = 1 ( i.e. i = 1 )
In particular what is the smallest allowed index for A ?
Try compiling with array bounds checking and see what happens,
Ian
| |
| Mark Morss 2006-12-13, 4:18 pm |
| Perhaps you don't understand that in Fortran, unlike in C, an array
dimension index by default starts at 1, not zero. In your code, when m
== 1, do you see that i == 1? And do you also see that further on with
still i == 1, you refer to A(i-1)? This reference, to A(0), is
illegal, just like your compiler is telling you.
nakisa wrote:
> hi Ian.
> i can't understand your meaning , i use dynamic array (named A) ,
> before starting , i wrote "i=0 " and i loop "i=i+1 " , so in each
> cycle
> it add one number to i ,then store it in A(i) .
> i can't understad where my error is ,yet ?!!!
> can you say more , thanks
| |
| glen herrmannsfeldt 2006-12-13, 4:19 pm |
| Ian Bush <I.J.Bush@nospam.dl.ac.uk> wrote:
[color=darkred]
Does this declare a two dimensional array, or,
as some might say, a one dimensional array of one
dimensional arrays? Or is the dimension attribute
a default and overridden be the one after A.
In either case, for a 1D array you should only need one (:).
-- glen
| |
| Beliavsky 2006-12-13, 4:19 pm |
|
glen herrmannsfeldt wrote:
> Ian Bush <I.J.Bush@nospam.dl.ac.uk> wrote:
>
>
> Does this declare a two dimensional array, or,
> as some might say, a one dimensional array of one
> dimensional arrays? Or is the dimension attribute
> a default and overridden be the one after A.
>
> In either case, for a 1D array you should only need one (:).
That is correct, but it appears that the syntax shown is valid and
declares a 1-D array.
For the program
program xalloc_dim
implicit none
real, dimension (:), allocatable :: A(:)
allocate (a(2))
a = 1.0
print*,"shape(a),a=",shape(a),a
end program xalloc_dim
the result for g95, gfortran, and ifort is
shape(a),a= 2 1. 1.
I wonder why the committee allowed this redundant form of declaration.
| |
| Michael Metcalf 2006-12-13, 4:19 pm |
|
"glen herrmannsfeldt" <gah@seniti.ugcs.caltech.edu> wrote in message
news:elpkhk$hoe$1@naig.caltech.edu...
> Ian Bush <I.J.Bush@nospam.dl.ac.uk> wrote:
>
>
> Does this declare a two dimensional array, or,
> as some might say, a one dimensional array of one
> dimensional arrays? Or is the dimension attribute
> a default and overridden by the one after A.
>
Just so (the last).
Regards,
Mike Metcalf
| |
| glen herrmannsfeldt 2006-12-13, 7:05 pm |
| Michael Metcalf <michaelmetcalf@compuserve.com> wrote:
(I wrote)
[color=darkred]
[color=darkred]
> Just so (the last).
So one could write, for example:
real, dimension(10) :: a, b, c, d(20)
Where a, b, and c would be (10), and d(20), overriding
the (10)?
Array of arrays seems more obvious to me...
-- glen
| |
| Michael Metcalf 2006-12-13, 7:05 pm |
|
"glen herrmannsfeldt" <gah@seniti.ugcs.caltech.edu> wrote in message
news:elpuac$leh$1@naig.caltech.edu...
>
> So one could write, for example:
>
> real, dimension(10) :: a, b, c, d(20)
>
> Where a, b, and c would be (10), and d(20), overriding
> the (10)?
>
Just so.
Regards,
Mike
|
|
|
|
|