UMBC CS 201, Spring 09
UMBC CMSC 201
Spring '09

UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help

CMSC 201
Programming Project One

Number Theory

Out: Wednesday 2/18/09
Due Date: Sunday 3/1/09, before 11:59 PM

The Objective

The objective of this assignment is to get you familiar with good design practices, writing in C in a Unix environment, using functions, and using separate compilation. It may also heighten your appreciation of mathematics.

The Background

The study of mathematics is separated into many fields. The one that tends to lead students to discover the beauty of mathematics is the field of number theory. This field deals with numbers and their relationships to each other.

The field of number theory has lead to discoveries such as :

Positive integers can be categorized in many different ways. This project will let you investigate some familiar and some new ways of looking at integers.

The Task

Your mission is to write a program that examines positive integers within a range that the user specifies and categorizes them as follows:

  1. ODD or EVEN
  2. PRIME or COMPOSITE -- a prime number is an integer greater than one that is divisible only by itself and one. Any number that is not "prime" is "composite". 1 is composite by definition.
  3. PERFECT / ABUNDANT / DEFICIENT --- as described above
  4. SQUARE -- as described above
  5. TRIANGULAR -- as described above
Your program will also count the number of integers in the specified range that fall into each category and print a summary at the end. You should restrict the user to positive integers between 1 and 100000, inclusive. The ending number specified by the user should always be greater than or equal to the starting number s/he chose. You must validate the user's input. I guarantee that all input used to test your program will be of type int.

Your program MUST include the following functions. Do NOT change the function prototypes given below. You may choose to use more functions if you wish, but these are sufficient. All "predicate" functions below should return 1 for TRUE and 0 for FALSE. You have some flexibility in implementing the other functions. Choose wisely (and use #defines)

void PrintGreeting (void); 
     -- displays a suitable greeting to the user
int  GetValidInt (int min, int max); 
     -- gets an integer from the user between min and max, inclusive and 
        returns that valid integer.
void PrintTableHeading (void); 
     -- prints the heading of the table
int  IsOdd (int n);   
     -- a predicate function that returns TRUE if n is Odd and returns FALSE 
        if not.
int  IsPrime (int n); 
     -- a prediate funtion that returns TRUE if n is prime, and FALSE if not
int  CheckForPerfect (int n); 
     -- classifies n as "perfect", "abundant" or "deficient".  A different 
        value is returned for each different category.
int  SumDivisors (int n); 
     -- returns the sum of the divisors of n.
int  IsDivisor (int a, int b); 
     -- a predicate function that returns TRUE if a is a divisor of b, and 
        FALSE if not
int  IsSquare (int n); 
     -- returns TRUE if n is a perfect square, FALSE if not
int  IsTriangular (int n); 
     -- returns TRUE if n is a triangular number, FALSE if not
void PrintTableLine (int n, int odd, int prime, int perfect, int square, 
                     int triangular); 
     -- prints the information for one number on one line of the table
void PrintTotals (int first, int last, int numOdd, int numEven, int numPrime,
                  int numComposite, int numDeficient, int numPerfect, 
                  int numAbundant, int numSquare, int numTriangular);
     -- prints the summary of the number of different classifications found in
        the range specified.

You MUST use separate compilation for this project.

Since you will have to use a function from the C math library, you will not only need to #include <math.h> but you must also link using the -lm option. Here is how you'll need to compile your files :
gcc -c -Wall -ansi proj1.c
gcc -c -Wall -ansi numbers.c
gcc -c -Wall -ansi util.c
gcc proj1.o numbers.o util.o -lm

Sample Run

ecs225d-linux-01[257] a.out This program classifies positive integers as Odd/Even, Prime/Composite, Perfect/Abundant/Deficient, Square, and Triangular You will now get to choose the range of positive integers that you would like to see classified. Start with which positive integer ? Please enter an integer between 1 and 100000 : 1 End with which positive integer ? Please enter an integer between 1 and 100000 : 30 Int Classifications.................................... -------------------------------------------------------------------------- 1 Odd Composite Deficient Square Triangular 2 Even Prime Deficient 3 Odd Prime Deficient Triangular 4 Even Composite Deficient Square 5 Odd Prime Deficient 6 Even Composite Perfect Triangular 7 Odd Prime Deficient 8 Even Composite Deficient 9 Odd Composite Deficient Square 10 Even Composite Deficient Triangular 11 Odd Prime Deficient 12 Even Composite Abundant 13 Odd Prime Deficient 14 Even Composite Deficient 15 Odd Composite Deficient Triangular 16 Even Composite Deficient Square 17 Odd Prime Deficient 18 Even Composite Abundant 19 Odd Prime Deficient 20 Even Composite Abundant 21 Odd Composite Deficient Triangular 22 Even Composite Deficient 23 Odd Prime Deficient 24 Even Composite Abundant 25 Odd Composite Deficient Square 26 Even Composite Deficient 27 Odd Composite Deficient 28 Even Composite Perfect Triangular 29 Odd Prime Deficient 30 Even Composite Abundant Between 1 and 30 there were 15 Odd integers and 15 Even integers 10 Prime numbers and 20 Composite numbers 2 Perfect, 5 Abundant and 23 Deficient numbers 5 Square(s) and 7 Triangular numbers ecs225d-linux-01[258] a.out This program classifies positive integers as Odd/Even, Prime/Composite, Perfect/Abundant/Deficient, Square, and Triangular You will now get to choose the range of positive integers that you would like to see classified. Start with which positive integer ? Please enter an integer between 1 and 100000 : -10 Please enter an integer between 1 and 100000 : 20 End with which positive integer ? Please enter an integer between 20 and 100000 : 10 Please enter an integer between 20 and 100000 : 200000 Please enter an integer between 20 and 100000 : 50 Int Classifications.................................... -------------------------------------------------------------------------- 20 Even Composite Abundant 21 Odd Composite Deficient Triangular 22 Even Composite Deficient 23 Odd Prime Deficient 24 Even Composite Abundant 25 Odd Composite Deficient Square 26 Even Composite Deficient 27 Odd Composite Deficient 28 Even Composite Perfect Triangular 29 Odd Prime Deficient 30 Even Composite Abundant 31 Odd Prime Deficient 32 Even Composite Deficient 33 Odd Composite Deficient 34 Even Composite Deficient 35 Odd Composite Deficient 36 Even Composite Abundant Square Triangular 37 Odd Prime Deficient 38 Even Composite Deficient 39 Odd Composite Deficient 40 Even Composite Abundant 41 Odd Prime Deficient 42 Even Composite Abundant 43 Odd Prime Deficient 44 Even Composite Deficient 45 Odd Composite Deficient Triangular 46 Even Composite Deficient 47 Odd Prime Deficient 48 Even Composite Abundant 49 Odd Composite Deficient Square 50 Even Composite Deficient Between 20 and 50 there were 15 Odd integers and 16 Even integers 7 Prime numbers and 24 Composite numbers 1 Perfect, 7 Abundant and 23 Deficient numbers 3 Square(s) and 4 Triangular numbers ecs225d-linux-01[259]

Although your output need not be identical to the above, all information (including the greeting) must be present.

Submitting the Program

You must name your source file that contains main() and the other functions that are specific to this project proj1.c.
The file that contains the functions relating to number theory should be called numbers.c and its header file should be called numbers.h.
The file that contains utility functions, i.e. GetValidInt(), that you are likely to reuse in other projects, should be called util.c and its header file should be called util.h.

To submit your project, type the following at the Unix prompt:

submit cs201 Proj1 proj1.c numbers.c numbers.h util.c util.h

To verify that your project was submitted, you can execute the following command at the Unix prompt. It will show all files that you submitted in a format similar to the Unix 'ls' command.

submitls cs201 Proj1


UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help

Thursday, 19-Feb-2009 08:25:52 EST

`