!     Last change:  A    19 Oct 2004    9:26 pm
PROGRAM Mean_of_a_list_lab5_allocated
!
! Use a 1-D allocatable array and function call and formatted output
!
IMPLICIT NONE
INTEGER, PARAMETER :: MaxInteration = 99
INTEGER :: i, NumEndInteration
REAL :: Root
REAL, DIMENSION(0:MaxInteration) :: X, Absolute, Reletive

WRITE (*,*) "Input first and second numbers: "
READ (*,*) X(0), X(1)

IF (X(0) < 0) THEN
   Root = -2.0
ELSE
   Root = 1.0
END IF

Absolute(0) = ABS( ( X(0) - Root ))
Reletive(0) = Absolute(0) / ABS( X(0) )

Absolute(1) = ABS( ( X(1) - Root ))
Reletive(1) = Absolute(1) / ABS( X(1) )

DO i = 2, MaxInteration
   X(i) = X(i-1) - ( F(X(i-1)) * (X(i-1) - X(i-2)) ) / ( F(X(i-1)) - F(X(i-2)) )
   Absolute(i) = ABS( ( X(i) - Root ))
   Reletive(i) = Absolute(i) / ABS( X(i) )

   IF ( ABS( X(i) - X(i-1) ) < .00001 ) THEN
      NumEndInteration = i
      EXIT
   END IF
END DO

DO i = 0, NumEndInteration
   WRITE (*,'(1X, A, I2, A, F7.4, A, F7.4, A, F7.4)')"Interation ",i," is ",X(i)," Rel Error ",Reletive(i)," Abs Error ",Absolute(i)
END DO


STOP
CONTAINS


FUNCTION F(X)

IMPLICIT NONE
REAL, INTENT (IN) :: X
REAL :: F

F = X**4 + 5.0*X**3 + 6.0*X**2 - 4.0*X - 8.0

RETURN
END FUNCTION F

END PROGRAM Mean_of_a_list_lab5_allocated
