Program 1 -- Polygons on the March

Due Date:    Lecture 8:   Friday, April 4
             Lecture 14:  Thursday, April 3

Some Background

A polygon is a flat shape with 3 or more sides, all of which are straight (e.g. a triangle is a 3-sided polygon). When all the sides of a polygon have the same length, the polygon is called regular. Equilateral triangles and squares are regular polygons with 3 and 4 sides respectively.

All regular polygons can be fit perfectly within a circle (such that the circle touches all the corners of the polygon). This circle is called the circumscribed circle.

The Problem

You are to write a program that will perform a few calculations on regular polygons. Your program will ask the user (1) how many sides the polygon has, and (2) what the length of each side is. Remember, all sides have the same length so part (2) is just one question! The number of sides should be stored in an integer variable, and all other variables should be of type double.

As output, your program will display (1) the area of the polygon, and (2) the radius of the circumscribed circle. Just use the formulas given below; you don't need to understand them at all!

In addition to the requirements above, print a welcoming message when the program starts and an exiting message when the program ends (see sample printout; use any messages you like!).

Finally, put a comment at the beginning of your program briefly describing the purpose of the program (use 1 or 2 sentences). Of course, put your name and login name in comments at the beginning of the program just like in assignment 0.

The Formulas

In the following equations, sides is the number of sides, edge is the length of each side, radius is the radius of the circumscribed circle, and area is the area of the polygon. Note that sides and edge are what the user enters, and radius and area are what the program outputs. Also note that edge is squared in the formula for area.

The formula is too complicated
to view in text form; you need to see the picture

A Sample Run

Step inside the amazing polygonator... 

Number of sides? 4
Length of each side? 7.82

Area of polygon: 61.1524
Radius of circumscribed circle: 5.529575

That's all, folks!

Hints

  • Don't write the whole program at once! For example, start by writing a program which only displays some messages. When that is working, create some variables and add the input commands. Test your input statements by redisplaying what the user has typed in. Finally, add the math commands to finish the program.
  • You can use pow(x,y) for calculating the square root. For example, pow(7, 0.5) calculates the square root of 7. Alternatively, you can just use sqrt(x).
  • Remember to use #include<math.h>, since you will need cos(), sin(), and pow(). Of course, you also need #include<iostream.h> in order to use cout and cin.
  • Use meaningful variable names. Avoid one-letter names unless they make your program more understandable.
  • Create some variables to store intermediate calculations.
  • Test your program on several cases for which you already know the correct answers. For instance, see the sample run given above for a square. Here's another case: when the number of sides is 3 and the length of each side is 2.1, the area is 1.909586 and the radius is 1.2124356.

    What to turn in

    Call your assignment program1.cpp and turn it in electronically. See the instructions at the end of assignment #0 if you need help, or see me or a consultant.

    NOTE: Only turn in the file program1.cpp. Don't turn in files with names like "program1.obj" or "program1.exe".