Re: allocation error




lane straatman wrote:

<snip>

You'd think I would have done it like malloc.
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

I would use

read(*,*) limit

since it would work for integers larger than two digits, if the program
is modified to work with them in the future.

allocate(pascal(0:limit))
!initialize

do i=0, limit
pascal(i) = 0
end do

You can simply write

pascal = 0

instead of the three lines above.

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

== is an alternative to .eq.

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

Fortran does not have C-style comments. Maybe there is a preprocessor
that can simulate them. I use

if (.false.) then
some code
end if

but this is not the same as commenting out a block.

.