UMBC CS 201, Fall 05
Predicate functions
From the dictionary...
1. pred.i.cate \'pred-i-k*t\ \'pred-i-k*t-iv, 'pred-*-.ka-t-\ n [LL
praedicatum, fr. neut. of praedicatus] 1a: something that is
affirmed or denied of the subject in a proposition in logic {in "paper
is white", whiteness is the ~} 1b: a term designating a
property or relation 2: the part of a sentence or clause that
expresses what is said of the subject and that usu. consists of a
verb with or without objects, complements, or adverbial modifiers -
pred.i.ca.tive aj
In a programming context, predicate functions are functions that return a
boolean result (i.e., either TRUE or FALSE).
Examples
IsEven
/****************************************************
* File: iseven.c
* Author: S. Bogar
* Date: 7/7/99
* Section: 0101
* Email : bogar@cs.umbc.edu
*
* This program prints a list of the even numbers
* between LOWER and UPPER.
****************************************************/
#include
#define LOWER 1
#define UPPER 10
/* Function prototypes */
int IsEven(int n);
/* Main program */
int main()
{
int i;
/* Tests every value between LOWER &
UPPER, inclusive, for even or odd */
for (i = LOWER; i <= UPPER; i++)
{
/* Prints only even values */
if (IsEven(i))
{
printf("%2d\n", i);
}
}
return 0;
}
/*****************************************
* Function: IsEven
* Usage: if (IsEven(n)) . . .
*
* Checks to see if a number is even
*
* Input: The number to test for even-ness
* Output: Returns TRUE if n is even.
*****************************************/
int IsEven(int n)
{
/* Even numbers are evenly divisible by 2 */
return (n % 2 == 0);
}
The Sample Run
linux3[125] % gcc -Wall -ansi iseven.c
linux3[126] % a.out
2
4
6
8
10
linux3[127] %
IsPrime
The Task
Identify the prime numbers between LOWER, the lower limit and UPPER, the upper
limit.
The Program
/***************************************************
* File: isprime.c
* Author: S. Bogar
* Date: 7/7/99
* Section : 0101
* Email: bogar@cs.umbc.edu
*
* This program prints a list of the prime numbers
* between LOWER (2) and UPPER (10).
***************************************************/
#include
/* constants */
#define LOWER 2 /* smallest integer to check */
#define UPPER 100 /* largest integer to check */
/* Function prototypes */
int IsPrime(int n);
int main()
{
int i;
/* Checks all integers between LOWER & UPPER
to see if they are prime numbers */
for (i = LOWER; i <= UPPER; i++)
{
/* Prints the number if it is prime */
if (IsPrime (i))
{
printf("%2d\n", i);
}
}
return 0;
}
/**************************************************
* Function: IsPrime
* Usage: if (IsPrime (n)) . . .
*
* This function determines if a number is prime
*
* Inputs: an integer, n
* Output: True (1, in this case) if n is prime
* False (0) if n is not prime
***************************************************/
int IsPrime(int n)
{
int i;
/* Checks to see if n is evenly divisible by any
number < n, other than 1. */
for (i = 2; i < n; i++)
{
/* If n is evenly divisible by i then it is
not prime */
if ((n % i) == 0)
{
return (0);
}
}
/* If this portion of the code is reached, then n
was not evenly divisble by any integer < n, so it
is prime */
return (1);
}
The Sample Run
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
CSEE
|
201
|
201 F'05
|
lectures
|
news
|
help
Sunday, 21-Aug-2005 09:53:44 EDT