campo-sirio/gfm/median.c
alex ba237a9d91 Patch level : no patch
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :
Aggiunti i sorgenti per Greenleaf Math Library (gfm.dll)


git-svn-id: svn://10.65.10.50/trunk@10079 c028cbd2-c16b-5b4b-a496-9718f37d4682
2002-02-26 12:19:02 +00:00

93 lines
1.5 KiB
C
Executable File

/* DEC *Median(pDst,pSrc,n)
*
* ARGUMENT
* DEC *pDst;
* DEC **pSrc;
* int n;
*
* DESCRIPTION
* Sets pDst to the median of the n DEC elements of the array pSrc.
* n <= 0 is not allowed. pDst is unchanged if an error occurs.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Returns pointer to the average if successful, otherwise
* returns GM_NULL.
*
* POSSIBLE ERROR CODES
*
* GM_NULLPOINTER
* GM_ARGVAL
* GM_NOMEMORY
*
* AUTHOR
* Jared Levy
* Copyright (C) 1987-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*/
#include <stdio.h>
#include <malloc.h>
#include "gm.h"
#include "gmsystem.h"
#ifndef DOS
extern char *calloc(unsigned ,unsigned );
#endif
DEC *Median(pDst,pSrc,n)
DEC *pDst;
DEC **pSrc;
int n;
{
int i;
DEC **tmpa, dsum, *sum=&dsum;
extern void qsort(char *, unsigned, unsigned, int (*)(void));
_MacStart(GM_MEDIAN);
if (!pSrc) {
_MacErr(GM_NULLPOINTER);
_MacRet(GM_NULL);
}
if (n<=0) {
_MacErr(GM_ARGVAL);
_MacRet(GM_NULL);
}
for (i=0; i<n; i++)
_MacInVarD(pSrc[i]);
_MacOutVarD(pDst);
/* allocate array of pointers */
tmpa = (DEC **) calloc(n, sizeof(DEC *));
if (!tmpa) {
_MacErr(GM_NOMEMORY);
_MacRet(GM_NULL);
}
/* copy pointers */
for (i=0;i<n;i++)
tmpa[i] = pSrc[i];
/* sort pointers */
qsort((void *)tmpa,n,sizeof(DEC *), _SortInc);
if (n%2)
_MacDCopy(pDst, tmpa[(n-1)/2]);
else {
(void) _AddDec80Bit(sum, tmpa[n/2-1], tmpa[n/2]);
MultiplyDecimal(pDst, sum, &decPoint5);
}
free ((char *) tmpa);
_MacRet(pDst);
}