#include "datamat.h"
#include "block.h"
#include "valcnt.h"

static char *findmodeid = "$Id: find_mode.c,v 1.5 1996/12/30 22:29:07 galway Exp $";

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

    find_mode.c

    Copyright (C) 2004  Lionel A. Galway

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




   Routine to find the mode of a particular column, given a valcnt
   data structure with frequencies already computed.

   L. Galway  16-AUG-1996 */
/* ***************************************************************** */

int find_mode(vcnt)
struct valcnt *vcnt;
{
	int i,imode,icnt;

	/* Initialize mode and count to first element */
	imode=vcnt->vals[0];icnt=vcnt->cnts[0];
	/* Search valcnt data structure for larger frequencies */
	for(i=1;i < vcnt->nvals;i++) {
	  if (vcnt->cnts[i] >= icnt) { /* If tie, take larger value */
	    imode=vcnt->vals[i];icnt=vcnt->cnts[i];
	  };
	};
	return(imode);
}
