## Example programs for histograms

The following program shows how to make a simple histogram of a column of numerical data supplied on `stdin`. The program takes three arguments, specifying the upper and lower bounds of the histogram and the number of bins. It then reads numbers from `stdin`, one line at a time, and adds them to the histogram. When there is no more data to read it prints out the accumulated histogram using `gsl_histogram_fprintf`.

```#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_histogram.h>

int
main (int argc, char **argv)
{
double a, b;
size_t n;

if (argc != 4)
{
printf ("Usage: gsl-histogram xmin xmax n\n"
"Computes a histogram of the data "
"on stdin using n bins from xmin "
"to xmax\n");
exit (0);
}

a = atof (argv[1]);
b = atof (argv[2]);
n = atoi (argv[3]);

{
int status;
double x;

gsl_histogram * h = gsl_histogram_alloc (n);

gsl_histogram_set_uniform (h, a, b);

while (fscanf(stdin, "%lg", &x) == 1)
{
gsl_histogram_increment(h, x);
}

gsl_histogram_fprintf (stdout, h, "%g", "%g");

gsl_histogram_free (h);
}

exit (0);
}
```

Here is an example of the program in use. We generate 10000 random samples from a Cauchy distribution with a width of 30 and histogram them over the range -100 to 100, using 200 bins.

```\$ gsl-randist 0 10000 cauchy 30
| gsl-histogram -100 100 200 > histogram.dat
```

A plot of the resulting histogram shows the familiar shape of the Cauchy distribution and the fluctuations caused by the finite sample size.

```\$ awk '{print \$1, \$3 ; print \$2, \$3}' histogram.dat
| graph -T X
```

@image{histogram,4in}