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

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 * SSN: 123-45-6789 * Section: 0101 * Email : bogar@cs.umbc.edu * * This program prints a list of the even numbers * between 1 and 10. In an ideal implementation, * constants would be used for the limits, but * this program is designed to match the program * example in the text. ****************************************************/ #include <stdio.h> /* Function prototypes */ int IsEven(int n); /* Main program */ int main() { int i; for (i = 1; i <= 10; i++) { 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) { 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 * SSN: 123-45-6789 * Section : 0101 * Email: bogar@cs.umbc.edu * * This program prints a list of the prime numbers * between 2 and 100. ***************************************************/ #include <stdio.h> /* 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; for (i = LOWER; i <= UPPER; i++) { 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 j; for (j = 2; j < n; j++) { if ((n % j) == 0) { return (0); } } 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 S'02 | lectures | news | help

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