CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 8

8. Write a program to calculate area of circle.

Once again, math has reared its focus on an assignment.  Luckily, with the power of search engines, I was able to rediscover the equations for calculating the area of a circle.  However, there are some requirements.  In order to calculate the area of a circle, the user will need to provide one of the following: radius, diameter, or circumference.

Also, here is a quick rundown of the variables.

A = Area
π = pi
r = radius
D = diameter
C = circumference

According to MathIsFun, the area of a circle can be calculated using one of the following formulas.
**Fun fact, in order to type the pi symbol "π", hold down ALT + 2, 2, 7. -- WikiHow**
**For the exponent of 2, hold down ALT + 2, 5, 3. -- AZCentral**


A = π r²                    π (Pi) times the Radius squared

A = ( π / 4 ) * D²      when you know the Diameter

A = C² / 4π               when you know the Circumference


TESTING.... TESTING... TESTING...

Before I begin implementing formulas into the source code, I needed to establish a strong foundation of the overall flow of the program.  With that said, I created a quick menu to interpret the user input.  For example, if the user inputs a lowercase value, the program will first convert the type into an uppercase letter before determining which sub-menu to execute.  (including <string> was not necessary, it has been removed within the later versions of the code)




At this time, everything is working as planned.  When the user inputs either r, d, c, R, D, or C, it will first check if the letter is lowercase, followed by converting it accordingly.  If the user inputs an uppercase letter, the program proceeds to the next sub-menu.  At this time, it simple outputs a statement, such as "RADIUS", "DIAMETER", "CIRCUMFERENCE" or "Invalid input..."






Now that the character conversion and sub-menu options have been confirmed, it was time to implement the formulas for each respective option.  Originally, I did not utilize exponents within each formula.  However, after a bit more research, I was able to implement exponents through the use of "math.h".

For example: π² has been coded as 

M_PI * (pow (radius, 2))

Originally, I coded it as

M_PI * (radius*radius)


Although either coding style would have had the same results, the use of "pow" allows greater flexibility.  Note, the original code is still there, it simply has been commented out.



Success!  I was able to confirm that my program worked as expected.  Below are the output of my code along with screen captures from MathIsFun's Area of a Circle calculator.
















Comments

Popular posts from this blog

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 9

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 6