Re: allocation error
- From: "Beliavsky" <beliavsky@xxxxxxx>
- Date: 30 Dec 2006 14:12:29 -0800
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.
.
- References:
- allocation error
- From: lane straatman
- Re: allocation error
- From: Richard Maine
- Re: allocation error
- From: lane straatman
- allocation error
- Prev by Date: Re: Proper way to return a string
- Next by Date: Re: OT: Disappointed with Intel
- Previous by thread: Re: allocation error
- Next by thread: Re: allocation error
- Index(es):