! Main for Sum_ErrFree
! Atentie:
! Setati: OPTIMIZATION: Disable
! (sau: utilizati configuratia DEBUG)

use ifcore  ! cvf: use dflib

character(260) fname, title*80
real, allocatable :: x(:)
logical armonic, alt
real(4) sum, sum_EF
logical print_it; common print_it;
real(16) sum16
logical pressed


write(*,'(a,\)') ' Input file name: '
read(*,*) fname

open(1, file =fname, status ='old', form ='formatted')

read(1,'(a80)') title

read(1,*) n, print_it

armonic =title(1:3) =='arm' .or. title(1:3) =='Arm' .or. title(1:3) =='ARM'
alt =title(1:3) =='alt' .or. title(1:3) =='Alt' .or. title(1:3) =='ALT'


IF(armonic .or. alt) THEN
write(1,'(/,a)') ' Numar de termeni = 10^N'
write(1,'(//, a)') '   N   SUMA_EF       Corectie          SUMA_obisnuita  SUMA_exacta'

nn =10**n
allocate(x(nn))

! Armonic/Alt.:
!*
if(n >1) print_it =.false.

DO JN =1,n
nj =10**JN

is =-1
sum =0.
sum16 =0q0

do j=1,nj
x(j) =1_4/real(j)

if(alt) then
is =-is
x(j) =is*x(j)
endif

sum =sum +x(j)
sum16 =sum16 +x(j)

enddo

call sum_N(nj, x(1:nj), sum_EF, cor)		! cor =err

write(1, '(/, I4, 1pg15.7, 1pg15.7, 1pg17.7, 1pg17.8)') JN, sum_EF, cor, sum, sum16
ENDDO

ELSE
allocate(x(n))
read(1,*) x(1:n)

sum =x(1)
sum16 =x(1)
do i =2,n
sum =sum +x(i)
sum16 =sum16 +x(i)
enddo

call sum_N(n, x(1:n), sum_EF, cor)		! cor =SUM(err)

write(1,'(//, a)') '   SUMA_EF       Corectie          SUMA_obisnuita  SUMA_exacta'
write(1, '(/, 1pg15.7, 1pg15.7, 1pg17.7, 1pg17.8)') sum_EF, cor, sum, sum16

ENDIF

write(*, '(/,a)') 'Apasa orice tasta'
DO WHILE (.NOT. pressed)   
pressed = PEEKCHARQQ ( )
END DO

END
