      subroutine scale (x,s,n,k)
c
c @(#)scale.f	1.1  4/3/89 
c
      dimension x(1),t(3)
      data t / 1.0,2.0,5.0 /
      m = n*k
      xmax = x(1)
      xmin = x(1)
      do 10 i = 1, m, k
         xmax = amax1(x(i),xmax)
         xmin = amin1(x(i),xmin)
   10 continue
      x0 = 0.0
      dx = 1.7e38
      w = 0.99999*(xmax-xmin)/s
      if (w.eq.0.0) w = 0.99999/s
      do 20 i = 1, 3
         b = 1.0+alog10(w/t(i))
         if (b.lt.0.0) b = b-1.0
         c = t(i)*10.0**ifix(b)
         if (c.le.dx) then
            d = c*int(1.00001*xmin/c)
            if (xmin.lt.d) d = d-c
            if (1.00001*s*c+d.ge.xmax) then
               x0 = d
               dx = c
            endif
         endif
   20 continue
      x(m+1) = x0-dx*aint((s-(xmax-xmin)/dx)/2.0)
      x(m+k+1) = dx
      return
      end
