UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

Minimum weight and height

The Task

Finding the minimum weight and height of two players .


Version One -- no functions

/*************************************************** ** File: min1.c ** Author: S. Bogar ** Date: 8/2/99 ** SSN: 123-45-6789 ** Section: 0101 ** EMail: bogar@cs.umbc.edu ** ** A sample program which needs to compute ** minimum values. ***************************************************/ #include <stdio.h> int main() { int height1, height2, weight1, weight2 ; int min_height, min_weight ; printf("Player 1 height: ") ; scanf ("%d", &height1) ; printf("Player 1 weight: ") ; scanf ("%d", &weight1) ; printf("Player 2 height: ") ; scanf ("%d", &height2) ; printf("Player 2 weight: ") ; scanf ("%d", &weight2) ; /* Find minimum height using 'if-else' */ if (height1 < height2) { min_height = height1 ; } else { min_height = height2 ; } printf("The lesser height is: %d\n", min_height) ; /* Find minimum weight using if-else */ if (weight1 < weight2) { min_weight = weight1 ; } else { min_weight = weight2 ; } printf("The lesser weight is: %d\n", min_weight) ; return 0; }

The Sample Run

linux3[89] % a.out Player 1 height: 70 Player 1 weight: 145 Player 2 height: 66 Player 2 weight: 158 The lesser height is: 66 The lesser weight is: 145 linux3[90] %


Version Two -- using a function

/*************************************************** ** File: min2.c ** Author: S. Bogar ** Date: 8/3/99 ** Section: 0101 ** SSN: 000-00-0001 ** EMail: bogar@cs.umbc.edu ** ** A sample program which computes ** minimum values using a function. ***************************************************/ #include <stdio.h> /* Function Prototype */ int Minimum(int a, int b); int main() { int height1, height2, weight1, weight2 ; int min_height, min_weight ; /* input player 1 height and weight */ printf("Player 1 height: ") ; scanf ("%d", &height1) ; printf("Player 1 weight: ") ; scanf ("%d", &weight1) ; /* player 2 height and weight */ printf("Player 2 height: ") ; scanf ("%d", &height2) ; printf("Player 2 weight: ") ; scanf ("%d", &weight2) ; /* use function to get minimum height */ min_height = Minimum(height1, height2) ; printf("The lesser height is: %d\n", min_height) ; /* use function to get mimum weight */ min_weight = Minimum(weight1, weight2) ; printf("The lesser weight is: %d\n", min_weight) ; return 0; } /************************************************** ** Function: Minimum() ** Usage: m = Minimum (n1, n2); ** ** Inputs: integers n1 and n2 to compare ** Output: the smaller of the two arguments ***************************************************/ int Minimum(int a, int b) { int min; if (a < b) { min = a ; } else { min = b ; } return min ; }

The Sample Run

linux3[91] % a.out Player 1 height: 70 Player 1 weight: 145 Player 2 height: 66 Player 2 weight: 158 The lesser height is: 66 The lesser weight is: 145


CSEE | 201 | 201 S'02 | lectures | news | help

Thursday, 17-Jan-2002 13:51:56 EST