File: statistics.c

package info (click to toggle)
fitsh 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,768 kB
  • ctags: 4,050
  • sloc: ansic: 53,352; makefile: 1,120; sh: 25
file content (39 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*****************************************************************************/
/* statistics.c								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Standalone library for calculating median and mode of a data set.	     */
/* (c) 2001, 2004, Pal, A. (apal@szofi.elte.hu). 			     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* See function prototypes and the usage of the functions in statistics.h    */
/*****************************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include "statistics.h"

/*****************************************************************************/

static int median_compare(const void *px,const void *py)
{
 if ( *(double *)px < *(double *)py ) 	return(-1);
 else					return(1);
}
double median(double *data,int n)
{
 double m;

 if ( data==NULL || n<=0 )
	return(0.0);

 qsort((void *)data,n,sizeof(double),median_compare);
 if ( n%2 )	m=data[n/2];
 else		m=0.5*(data[n/2-1]+data[n/2]);

 return(m);
}

/*****************************************************************************/