Sum and Construct an Array in one statement

215 views Asked by At

I am preparing for an exam, and working through some practice questions, which unfortunately does not provide solutions. I would like to program this function using the SUM function, an array constructor and an implied do-loop:

Sum from n=2 to 100, (1+2*N)*LOG10(N)

I have tried:

WRITE(*,*) SUM(real:: x(99) = (/ ( (1+2*N)*LOG10(REAL(N)) , N=2,100 )  /))

But all the IDE says is that the statement is not recognised. I have tried a variety of other ways, but no luck.

1

There are 1 answers

0
Alexander Vogt On

It is not allowed to have the declaration of x inside a statement. Also, it is not required, you can process the implied loop directly:

program test
  implicit none
  integer :: n
  WRITE(*,*) SUM( (/ ( (1+2*N)*LOG10(REAL(N)) , N=2,100 )  /))
end program

This gives

18187.0469