c
      function histgram(nx, x, ncls)
c
      implicit none
      integer :: histgram, nx, ncls
      double precision :: x(*)
c
      integer, parameter :: nc = 2000
      integer :: n(nc), n_max
      integer :: isn(nc)
      double precision :: sn(nc)
c
      integer :: i, j
      double precision :: x_min, x_max
c
      histgram = 0
c
      if (ncls > nc) then
        print '(''ncls should be smaller than '',I4)', nc
        histgram = 1
        return
      else if (ncls < 2) then
        histgram = 1
        return
      end if
c
      do j = 1, ncls
        n(j) = 0
      end do
c
      x_min = x(1)
      x_max = x(1)
      do i = 2, nx
        if (x(i) > x_max) then
          x_max = x(i)
        else if (x(i) < x_min) then
          x_min = x(i)
        end if
      end do
c     print *, x_min, x_max
c
      do i = 1, nx
        do j = 1, ncls
          if (x_min + (j - 1) * (x_max - x_min) / ncls <= x(i) 
     $     .and. x(i) < x_min + j * (x_max - x_min) / ncls) then
            n(j) = n(j) + 1
          end if
        end do
        if (x_max <= x(i)) then
          n(ncls) = n(ncls) + 1
        end if
      end do
c
      n_max = n(1)
      do j = 2, ncls
        if (n(j) > n_max) then
          n_max = n(j)
        end if
      end do
c
      do j = 1, ncls
        sn(j) = 40.0 * n(j) / n_max + 0.5
        isn(j) = int(sn(j))
      end do
c
      print '(1p,g15.5,2a)', x_min
     $       , '|----+----1----+----2----+----3'
     $       , '----+----4----+----5'
      do j = 1, ncls
        if (n(j) < 1) then
          print '(f4.2,''-'',f4.2,'':'',i5,'':'')' ,
     $     (j - 1) / dble(ncls), j / dble(ncls), n(j)
        else
          print '(f4.2,''-'',f4.2,'':'',i5,'':'',50a)', 
     $     (j - 1) / dble(ncls), j / dble(ncls), n(j)
     $   , ('*',i=1,isn(j))
        end if
      end do
      print '(1p,g15.5,2a)', x_max
     $       , '|----+----1----+----2----+----3'
     $       , '----+----4----+----5'
c
      return
      end
