Re: allocation error
- From: "lane straatman" <grumpy196884@xxxxxxxxxxx>
- Date: 30 Dec 2006 13:22:06 -0800
Richard Maine wrote:
lane straatman <grumpy196884@xxxxxxxxxxx> wrote:You'd think I would have done it like malloc.
allocate(pascal(1:n))...
write (*,'(a)', advance='no') 'give me an int'
read (*, '(i3)') n
*** Error 112, Reference to undefined variable, array element or
function result (/UNDEF)
That's because your allocate is before you read the value of n. Allocate
is an executable statement. It does its thing when it is executed, which
in this case is before n is defined. My guess is that you are thinking
it does something more complicated like make the array size track the
value of n as n changes. Nope, it is much simpler than that.
Put the allocate after the read.
program pascal1
IMPLICIT NONE
integer, dimension(:), allocatable :: pascal
integer :: limit, i, j
write (*,'(a)', advance='no') 'give me an int less than 14: '
read (*, '(i2)') limit
allocate(pascal(0:limit))
!initialize
do i=0, limit
pascal(i) = 0
end do
pascal(limit) = 1
!outer loop
do j=1,limit
do i=1, limit
pascal(i-1)=pascal(i-1)+pascal(i)
if (pascal(i).eq.0) then
write (*,'(a)',advance='no') " "
else
write (*,'(i4)',advance='no') pascal(i)
end if
end do
write(*,*)
end do
!done
deallocate (pascal)
end program pascal1
This turns out to be a nifty little program once you get some traction
on it (MR&C 4.7.3). I'm simply surprised that the output looks as nice
as it does. (I still remember the headache it was in college trying to
get fortran output to look proper, but that was a different century.)
One thing I really miss from C while I'm coding is the C-style
comments, /* ... */, that envelope a problem block while you're shaking
the bugs out. Does fortran have something other than '!'? LS
.
- Follow-Ups:
- Re: allocation error
- From: Beliavsky
- Re: allocation error
- References:
- allocation error
- From: lane straatman
- Re: allocation error
- From: Richard Maine
- allocation error
- Prev by Date: Re: Proper way to return a string
- Next by Date: Re: Proper way to return a string
- Previous by thread: Re: allocation error
- Next by thread: Re: allocation error
- Index(es):
Relevant Pages
|
|