UMBC CS 201, Spring 07
UMBC CMSC 201
Spring '07

CSEE | 201 | 201 S'07 | lectures | news | help

CMSC 201
Programming Project One

Regular Polygons

Out: Monday 2/26/07
Due: Before Midnight, Sunday 3/4/07

The Objective

The objective of this assignment is to get you started writing programs in C in the UNIX environment. Its main emphasis is design, so we will be designing the project together in class. This project will also give you practice writing functions, loops, switch statements and using separate compilation.

The Background

A REGULAR POLYGON is defined as a convex polygon with all sides congruent and all angles congruent. The labeled drawings below show what is meant by the terms: exterior angle, interior (or vertex angle), central angle, and diagonal.

Realize that any regular polygon, not just the regular hexagon shown, can be divided into the same number of triangles as it has sides, with the base of each triangle being a side of the polygon and that the "tips" of the triangles join in the center of the polygon.

All formulae shown in the drawings and also those for perimeter and area shown later are courtesy of mathwords.com

For all formulae shown:

Names of Regular Polygons

Number of Sides Name
3Equilateral Triangle
4Square
5Regular Pentagon
6Regular Hexagon
7Regular Heptagon
8Regular Octagon
9Regular Nonagon
10Regular Decagon

The Task

Design (to be done in class) and code a project that after printing a greeting to the user which contains information about regular polygons, presents the user with a nicely-aligned table that shows :

for all n-gons from 3 to 10, inclusive. The values in this table may NOT be hard coded. All of the values in the table MUST be calculated by the modular functions described in the design.

After presenting the table, your program must direct the user to input the number of sides of a polygon and the length of the sides. Use this information to calculate the perimeter and area of that polygon. Display the name of the polygon with its perimeter and area to the user. Restrict the user to regular polygons with n of 3 - 10, inclusive. The user should be allowed to continue to enter polygons as long as s/he wants and should signal the end by entering the sentinel 0 for n.

Since you are distributing this software to others, you must use separate compilation and provide the interface for the user. This is in the form of a well commented header (.h) file. You must have 3 files. The file that contains main() MUST be called proj1.c; the file containing the function definitions related to polygons MUST be called polygons.c and the header file that contains the function prototypes for the functions found in polygons.c MUST be called polygons.h

You are required to use a switch statement to handle the printing of the polygon's names.

You must also check user input for validity and reprompt for a new value when the user is in error. This should be accomplished in the functions GetValidNumSides() and GetPositiveValue().

You must adhere to the design given in class. The function prototypes given below must be used and may NOT be altered in anyway.

void PrintGreeting (void); void GenerateTable (int min, int max); void PrintInstructions (void); int GetValidNumSides (int sentinel, int min, int max); double GetPositiveValue (void); void PrintName (int numSides); double FindExteriorAngles (int numSides); int FindAngleSum (int numSides); double FindInteriorAngles (double angleSum, int numSides); int FindNumDiagonals(int numSides); double FindPerimeter (int numSides, double side); double FindArea (int numSides, double side); double ConvertDegreesToRadians (double degrees); The following symbolic constants should be used and they should be placed in your proj1.c file above main(): #define SENTINEL 0 #define MIN 3 #define MAX 10 The following symbolic constants should be used and they should be placed at the top of your polygons.c file. NOTE: These symbolic constants would only be placed in the polygons.h file if they were being used by both the proj1.c and polygons.c files. These are not used in proj1.c. It is okay to use them within modular functions' code since they are contained within the same file AND their values will never change. #define HALF_ROTATION 180 #define FULL_ROTATION 360 #define PI 3.14159 #define TRIANGLE 3 #define SQUARE 4 #define PENTAGON 5 #define HEXAGON 6 #define HEPTAGON 7 #define OCTAGON 8 #define NONAGON 9 #define DECAGON 10

Where should the function definitions go ?

Most of the functions you are writing for this project deal with polygons and might be used as modules for future projects. Those function definitions belong in the polygons.c file and their prototypes in the polygons.h file.

The functions PrintGreeting() and PrintInstructions() are specific to this project, so their definitions belong at the bottom of the proj1.c file. Their prototypes belong before main() in the proj1.c file.

Details about the math library

  1. You must #include <math.h>
  2. You must link using the -lm option
  3. The math library does not have a cotangent function, but it does have a tangent function called tan(), which takes a double and returns a double. You should remember and use the trig identity:
    cot(theta) = 1/tan(theta)
  4. All of the math library trig functions take the angle measurements in radians, not degrees, so you'll have to write the function called ConvertDegreesToRadians(), which is a good module to have.

Sample Run with Compilation Shown

linux1[101] ls polygons.c polygons.h proj1.c linux1[102] gcc -c -Wall -ansi proj1.c linux1[103] gcc -c -Wall -ansi polygons.c linux1[104] gcc proj1.o polygons.o -lm linux1[105] ls a.out polygons.c polygons.h polygons.o proj1.c proj1.o linux1[106] a.out Your greeting goes here Table of Regular Polygon Facts Name # Sides Ext Ang Int Ang Sum Int Ang Diags Equilateral triangle 3 120.000 180 60.000 0 Square 4 90.000 360 90.000 2 Regular pentagon 5 72.000 540 108.000 5 Regular hexagon 6 60.000 720 120.000 9 Regular heptagon 7 51.429 900 128.571 14 Regular octagon 8 45.000 1080 135.000 20 Regular nonagon 9 40.000 1260 140.000 27 Regular decagon 10 36.000 1440 144.000 35 Your instructions about entering the number of sides and the length of one side for the calculations of perimeter and area go here. Enter number of sides, 0 to quit : 4 Enter the length of one side : 4 Square with sides of 4.000 Perimeter : 16.000 Area : 16.000 Enter number of sides, 0 to quit : 3 Enter the length of one side : 4 Equilateral triangle with sides of 4.000 Perimeter : 12.000 Area : 6.928 Enter number of sides, 0 to quit : 6 Enter the length of one side : 4 Regular hexagon with sides of 4.000 Perimeter : 24.000 Area : 41.569 Enter number of sides, 0 to quit : 2 Sorry 3 - 10 or 0 only Try again. Enter number of sides, 0 to quit : 1 Sorry 3 - 10 or 0 only Try again. Enter number of sides, 0 to quit : -5 Sorry 3 - 10 or 0 only Try again. Enter number of sides, 0 to quit : -1 Sorry 3 - 10 or 0 only Try again. Enter number of sides, 0 to quit : 5 Enter the length of one side : 2.5 Regular pentagon with sides of 2.500 Perimeter : 12.500 Area : 10.753 Enter number of sides, 0 to quit : 7 Enter the length of one side : 3.75 Regular heptagon with sides of 3.750 Perimeter : 26.250 Area : 51.102 Enter number of sides, 0 to quit : 8 Enter the length of one side : 25.67 Regular octagon with sides of 25.670 Perimeter : 205.360 Area : 3181.690 Enter number of sides, 0 to quit : 9 Enter the length of one side : 15.29 Regular nonagon with sides of 15.290 Perimeter : 137.610 Area : 1445.214 Enter number of sides, 0 to quit : 10 Enter the length of one side : 17 Regular decagon with sides of 17.000 Perimeter : 170.000 Area : 2223.628 Enter number of sides, 0 to quit : 6 Enter the length of one side : 0 Positive values only! Try again : -3 Positive values only! Try again : 6.2 Regular hexagon with sides of 6.200 Perimeter : 37.200 Area : 99.870 Enter number of sides, 0 to quit : 0 Ending program linux1[107]

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

Please be aware that this is definitely NOT a complete test of the program. Your testing should be much more extensive.

Submitting the Program

You are to use separate compilation for this project, so you will be submitting three files.
Your C source code file that contains main() MUST be called proj1.c. The file which contains the polygon function definitions MUST be called polygons.c and its header file MUST be called polygons.h

To submit your project, type the following at the Unix prompt. Note that the project name starts with uppercase 'P'.

submit cs201 Proj1 proj1.c polygons.c polygons.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


CSEE | 201 | 201 S'07 | lectures | news | help

Monday, 26-Feb-2007 11:00:07 EST