UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help |
/******************************************** * File: stats.c * Author: S. Bogar * Date: 8/3/99 * Section 101 * EMail: bogar@cs.umbc.edu * * This file contains some simple statistical * functions each written to handle two integers * as input. They are grouped together in this * file because they are related modules, all simple * statistical functions, and as such could be used * by any program that deals with simple statistics * involving only two integers. * * This file is meant to be compiled separately from * the main program. * * Functions in this file: * Sum (x, y) * Min (x, y) * Max (x, y) * Avg (x, y) * Dist (x, y) * *********************************************/ /*********************************** * All header files needed by the code * found in the functions defined * in this file. NOTE: Since none of * these functions do input or output, * there is no need to #include <stdio.h> ************************************/ #include "stats.h" /******************************************** * Function: Sum * Usage: z = Sum (x, y) * * This function adds the two values passed in * and returns their sum. * * Input: two integers to be added * Output: Returns the sum of x and y. *********************************************/ int Sum (int x, int y) { int sum ; sum = (x + y) ; return sum ; } /********************************************* * Function: Max * Usage: z = Max (x, y) * * This function determines which of the values * passed in is larger. * * Inputs: two integers to be compared * Output: Returns the larger of x and y. *********************************************/ int Max (int x, int y) { int max ; if (x > y) { max = x ; } else { max = y ; } return max ; } /********************************************* * Function: Min * Usage: z = Min (x, y) * * This function determines which of the values * passed in is smaller. * * Inputs: two integers to be compared * Output: Returns the smaller of x and y. *********************************************/ int Min (int x, int y) { int min ; if (x < y) { min = x ; } else { min = y ; } return min ; } /********************************************* * Function: Avg * Usage: z = Avg (x, y) * * This function calculates the average of the * two values passed in. * * Inputs: two integers to be compared * Output: Returns average of x and y as a * *** double *** *********************************************/ double Avg (int x, int y) { double average ; average = (x + y) / 2.0 ; return average ; } /********************************************* * Function: Dist * Usage: z = Dist (x, y) * * This function calculates the distance between * x and y on a number line. * * Input: two integers for which distance is calculated * Output:Returns the absolute value of x - y. *********************************************/ int Dist (int x, int y) { int distance ; distance = x - y ; if (distance < 0) { distance = -distance ; } return distance ; }