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
** Section: 0101
** EMail: bogar@cs.umbc.edu
**
** A sample program which needs to compute
** minimum values.
***************************************************/
#include
int main()
{
int height1, height2, weight1, weight2 ;
int min_height, min_weight ;
/* Get height & weight of Player 1 from user */
printf("Player 1 height: ") ;
scanf ("%d", &height1) ;
printf("Player 1 weight: ") ;
scanf ("%d", &weight1) ;
/* Get height & weight of Player 2 from user */
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
** EMail: bogar@cs.umbc.edu
**
** A sample program which computes
** minimum values using a function.
***************************************************/
#include
/* 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) ;
/* input 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 minimum weight */
min_weight = Minimum(weight1, weight2) ;
printf("The lesser weight is: %d\n", min_weight) ;
return 0;
}
/**************************************************
** Function: Minimum()
** Usage: m = Minimum (n1, n2);
**
** Minimum finds the smaller of the two values
** passed in.
**
** 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