How to fill an integer array with some nans?

program test
  use ieee_arithmetic
  integer :: n
  integer, allocatable :: x(:)
  n = 5
  allocate(x(n))
  x = [1, 2, 3, 4, 5]
  x(1) = ieee_value(0, ieee_quiet_nan)
  print *, x
end program

This program only works for a real array.

There is no integer NaN. You could transfer real NaN into integer variable but you would get perfectly valid, finite integer value.

In real world, people have been using values like -99999999 to signal error / “nan” but that requires knowing for sure that there will be no such values in normal course of computations.

An integer cannot be set to NaN, but in the allocate statement you can use source to set the values of an integer array to something that “sticks out”, such as -huge(). Here is an example:

program test
  integer :: n
  integer, allocatable :: x(:)
  n = 3
  allocate(x(n), source = -huge(0))
  x(1:n-1) = 0
  print*,x ! 0 0 -2147483647
end program