UMBC CS 201, Fall 05
 UMBC CMSC 201 Fall '05 CSEE | 201 | 201 F'05 | lectures | news | help Search Notes:

# middle

A function that returns the middle number of the parameters.

## The Program

/* File: middle.c Author: Sue Bogar Date Written: 3/17/99 Section: 0101 E-Mail: bogar@cs.umbc.edu This program uses a function that returns the middle value of three integers. */ #include <stdio.h> /* Function Prototype */ int Middle(int, int, int) ; int main() { int n1, n2, n3, middle ; /* Get 3 integers from the user */ printf("Enter first number: ") ; scanf ("%d", &n1) ; printf("Enter second number: ") ; scanf ("%d", &n2) ; printf("Enter third number: ") ; scanf ("%d", &n3) ; /* Find & print the middle value */ middle = Middle(n1, n2, n3) ; printf("The middle number is: %d\n", middle) ; return 0; } /* Function: Middle * * Middle returns the middle value of the * 3 integers passed into it as arguments * * Inputs: 3 integers * Output: the middle value of the values passed in */ int Middle(int a, int b, int c) { int middle; /* a is the middle */ if (b >= a && a >= c) { middle = a ; } else if (c >= a && a >= b) { middle = a ; } /* b is the middle */ else if (a >= b && b >= c) { middle = b ; } else if (c >= b && b >= a) { middle = b ; } /* c is the middle */ else if (b >= c && c >= a) { middle = c ; } /* a >= c && c >= b is the only remaining possible combination */ else { middle = c ; } return middle ; }

## The Sample Run

linux3[80] % gcc -Wall -ansi middle.c linux3[81] % a.out Enter first number: 7 Enter second number: 9 Enter third number: 2 The middle number is: 7 linux3[82] % !a a.out Enter first number: 7 Enter second number: 2 Enter third number: 9 The middle number is: 7 linux3[83] % !a a.out Enter first number: 5 Enter second number: 4 Enter third number: 3 The middle number is: 4 linux3[84] % !a a.out Enter first number: 3 Enter second number: 4 Enter third number: 5 The middle number is: 4 linux3[85] % !a a.out Enter first number: 7 Enter second number: 9 Enter third number: 8 The middle number is: 8 linux3[86] % !a a.out Enter first number: 9 Enter second number: 7 Enter third number: 8 The middle number is: 8 linux3[87] % !a a.out Enter first number: 3 Enter second number: 3 Enter third number: 3 The middle number is: 3 linux3[88] %

## Exhaustive Testing

Have we tried every possibility in our testing ?

What have we missed ?

CSEE | 201 | 201 F'05 | lectures | news | help

Thursday, 15-Sep-2005 13:07:15 EDT