DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T h

⟦c5287f877⟧ TextFile

    Length: 2207 (0x89f)
    Types: TextFile
    Names: »histogram.c«

Derivation

└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
    └─ ⟦this⟧ »cph85dist/stat/src/histogram.c« 

TextFile

/*LINTLIBRARY*/
#include "unixstat.h"
FUN(histogram,plot a histogram,5.0,1985)
/* Copyright (c) 1982 Gary Perlman (see Copyright file) */

#define scale(x,min,max,width) ((int) (width*((x)-(min))/(max==min?1:(max)-(min))))

histogram (vec, n, hist, freq, prop, base, interval)
float	*vec;
double	base;
double	interval;
	{
	char	*malloc ();
	double	max = *vec;
	int 	bins, bin;
	int 	point;
	int 	*frequency;
	for (point = 0; point < n; point++)
		if (vec[point] > max) max = vec[point];
	if (fzero (interval))
		{
		interval = (max-base)/sqrt(2.0*n);
		if (fabs (interval) > 1.0)
			interval = floor (interval);
		else if (interval <= FZERO) interval = 1.0;
		}
	if (base > max) return (0);
	if ((bins = (max-base)/interval) == 0) bins = 1;
	if (frequency = (int *) malloc ((unsigned) bins * sizeof (*frequency)))
		{
		for (bin = 0; bin < bins; bin++)
			frequency[bin] = 0;
		for (point=0; point<n; point++)
			if ((bin = scale(vec[point],base,max+FZERO,bins)) >= 0)
				frequency[bin]++;
		barhist (frequency, bins, hist, freq, prop, base, interval);
		free ((char *) frequency);
		}
	return (bins);
	}

barhist (frequency, bins, hist, freq, prop, base, interval)
int 	*frequency;
double	base;
double	interval;
	{
	int 	maxfreq = 0;
	int 	bin, point;
	double	midpoint = base - interval/2.0;
	int 	cumf = 0;
	double	fcumf = 0.0;
	int 	sum = 0;
	for (bin = 0; bin < bins; bin++)
		{
		if (frequency[bin] > maxfreq) maxfreq = frequency[bin];
		sum += frequency[bin];
		}
	if (sum == 0) sum = 1;
	printf ("%8s", "Midpt");
	if (freq) printf ("%8s", "Freq");
	if (freq > 1) printf ("%8s", "Cum");
	if (prop) printf ("%8s", "Prop");
	if (prop > 1) printf ("%8s", "Cum");
	putchar ('\n');
	for (bin = 0; bin < bins; bin++)
		{
		printf ("%8.3f", midpoint += interval);
		if (freq) printf ("%8d", frequency[bin]);
		if (freq > 1) printf ("%8d", cumf += frequency[bin]);
		if (prop) printf ("%8.3f", frequency[bin]/(double)sum);
		if (prop > 1) printf ("%8.3f", (fcumf += frequency[bin])/sum);
		if (hist)
			{
			int 	repeat = hist==1
						? frequency[bin]
						: scale(frequency[bin],0.0,maxfreq,hist);
			putchar (' ');
			for (point = 0; point < repeat; point++) putchar ('=');
			}
		putchar ('\n');
		}
	}