PC_load_letter

March 8th, 2013, 11:56 PM

Today is my first day to learn Fortran, I look for code samples and I stumble upon this one for the calculation of the area of a closed cylinder, here:

program cylinder

! Calculate the surface area of a cylinder.

!

! Declare variables and constants.

! constants=pi

! variables=radius squared and height

implicit none ! Require all variables to be explicitly declared

integer :: ierr

character(1) :: yn

real :: radius, height, area

real, parameter :: pi = 3.141592653589793

interactive_loop: do

! Prompt the user for radius and height

! and read them.

write (*,*) 'Enter radius and height.'

read (*,*,iostat=ierr) radius,height

! If radius and height could not be read from input,

! then cycle through the loop.

if (ierr /= 0) then

write(*,*) 'Error, invalid input.'

cycle interactive_loop

end if

! Compute area. The ** means "raise to a power."

area = 2 * pi * (radius**2 + radius*height)

! Write the input variables (radius, height)

! and output (area) to the screen.

write (*,'(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)') &

'radius=',radius,'height=',height,'area=',area

yn = ' '

yn_loop: do

write(*,*) 'Perform another calculation? y[n]'

read(*,'(a1)') yn

if (yn=='y' .or. yn=='Y') exit yn_loop

if (yn=='n' .or. yn=='N' .or. yn==' ') exit interactive_loop

end do yn_loop

end do interactive_loop

end program cylinder

Saved the file as cylinder.f90, then I compiled the code w/ gfortran by typing

$ gfortran cylinder.f90 -o cylinder.out

Now after it was compiled, I ran the executable from terminal by typing

$ ./cylinder.out

The problem is the code works for any dimensions except for multiples of 10, so if you enter 10, 10, or 10, 20, or 30, 100...etc. the answer will be ******?!!!!!!

Why is that? Anyone knows?

program cylinder

! Calculate the surface area of a cylinder.

!

! Declare variables and constants.

! constants=pi

! variables=radius squared and height

implicit none ! Require all variables to be explicitly declared

integer :: ierr

character(1) :: yn

real :: radius, height, area

real, parameter :: pi = 3.141592653589793

interactive_loop: do

! Prompt the user for radius and height

! and read them.

write (*,*) 'Enter radius and height.'

read (*,*,iostat=ierr) radius,height

! If radius and height could not be read from input,

! then cycle through the loop.

if (ierr /= 0) then

write(*,*) 'Error, invalid input.'

cycle interactive_loop

end if

! Compute area. The ** means "raise to a power."

area = 2 * pi * (radius**2 + radius*height)

! Write the input variables (radius, height)

! and output (area) to the screen.

write (*,'(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)') &

'radius=',radius,'height=',height,'area=',area

yn = ' '

yn_loop: do

write(*,*) 'Perform another calculation? y[n]'

read(*,'(a1)') yn

if (yn=='y' .or. yn=='Y') exit yn_loop

if (yn=='n' .or. yn=='N' .or. yn==' ') exit interactive_loop

end do yn_loop

end do interactive_loop

end program cylinder

Saved the file as cylinder.f90, then I compiled the code w/ gfortran by typing

$ gfortran cylinder.f90 -o cylinder.out

Now after it was compiled, I ran the executable from terminal by typing

$ ./cylinder.out

The problem is the code works for any dimensions except for multiples of 10, so if you enter 10, 10, or 10, 20, or 30, 100...etc. the answer will be ******?!!!!!!

Why is that? Anyone knows?