//==============================================================================
//  Function for calculation of histogram
//  input:
//  nPoints     - number of points for processing
//  pfVext      - array with data
//  fMin,fMax   - histogram limits
//  nHistPoints - number of intervals in histogram
//  output:
//  pnHistogram - array with  histogram
//  return:
//  number of points in the histogram
//==============================================================================
int CalcHistogram(int nPoints, double* pfVect,
                  int nHistPoints, double fMin, double fMax,
                  int* pnHistogram)
{
  int i;
  int ii;
  int nCount = 0;
  double fDelta = (fMax - fMin) / (double) nHistPoints;

  // histogram array initializing
  for(i=0; i<nHistPoints; i++)
  {
    pnHistogram[i] = 0;
  }

  // histogram calculation
  for(i=0; i<nPoints; i++)
  {
    ii = (pfVect[i] - fMin) / fDelta;
    if(ii>=0 && ii<nHistPoints)
    {
      pnHistogram[ii]++;
      nCount++;
    }
  }

  return nCount;
}

//==============================================================================

#include <stdio.h>
#include <stdlib.h>

//==============================================================================
//  TEST of function CalcHistogram:
//==============================================================================

#define NPOINTS     10000
#define NHISTPOINTS 10

int main(int nArgs, char** ppArgs)
{
  double dat[NPOINTS];
  int    hist[NHISTPOINTS];
  int i;
  double fMin = 0., fMax = 1.;
  
  for(i=0; i<NPOINTS; i++)
  {
    dat[i] = (double)rand() / RAND_MAX;
  }

  CalcHistogram(NPOINTS, dat, NHISTPOINTS, 0., 1., hist);
  
  puts("Histogram:");
  
  for(i=0; i<NHISTPOINTS; i++)
  {
    printf("hist[%d]= %d\n", i, hist[i]);
  }
  
  return 0;
}
