/* CLOSEST * Return the element in the table that's closest to the number specified * returns -1 if table is empty */ closest(num,tab) int num; /* wot to look for */ int *tab; /* where to look */ { int got; /* the best we got */ unsigned diff; /* the difference between the best we got and num */ unsigned dift; /* tempory difference */ /* set returned code to error by default */ got = -1; /* scan through the table until a 0 is found or we're close as can get*/ for(diff=65535 ; ((*tab) && (got!=num)) ; tab++ ) { /* if a closer number, then set it to that */ if((dift=abs(*tab - num)) < diff ) { diff=dift; got=*tab; } } /* return wot we got */ return(got); }