# circle problem

Printable View

• 07-12-2007
gunghomiller
circle problem
Hello all,
I have tried unsuccessfully to create program using a header file, implemenation file, document file, and driver program that compute the area of a circle given only the radius. I was told to use the header file for the function header only and the implementation file for the actual code. Can someone explain to me what I did wrong. Here is the code;
header file.h
Code:

```/* area.h provides an interface for a library of the area of a circle. ***************************************************************/ #include <cmath> #include <iostream> const double PI = 3.14159; double radius; double area(double radius, const double PI) {         return PI * pow(radius, 2); }```
implementation file.cpp
Code:

``` /* area.cpp converts a given radius of a circle to the area of a circle. *  using function area() that is stored in the library area. * *  Input: Radius of a circle *  Output: Area of a circle **************************************************************/ #include <iostream> using namespace std; #include "area.h" #include <cmath> int main() {         double radius, area1;                 cout << "This program converts a given radius of a circle\n"                 << "to the area of a circle.\n";         cout << "\nEnter the length of your radius: ";         cin >> radius;         area1 = area(radius,PI);                 cout << "A radius length of: " << radius <<"," << "\nusing the correct equation converts to an area of: "                 << area1 << endl; }```
driver
Code:

```/*driver.cpp test the functions and constants in area.h . * *********************************************************/ #include<iostream> using namespace std; #include "area.h" #include <cmath> double area(double radius, const double PI); int main() {         cout << "2 => " << area(2, PI) << "\n";         cout << "4 => " << area(4, PI) <<"\n"; }```
It works the way that I made it, however, i was told to I was told to use the header file for the function header only and the implementation file for the actual code. I am lost.
• 07-12-2007
Brad0407
• 07-12-2007
QuestionC
Understand that the header/implementation/driver distinction is really of importance for larger programs. Headers and implementation are generally seperated for compiling efficiency.

You appear to be confused on terms. Here is a very bare-bones 3-file program which converts between temperature formats.

This is the header file.
Code:

```// Header files should only contain function prototypes. float farenheit_to_celcius (float celcius);```
This is the implementation file.
Code:

```#include "f2c.hpp" // The implementation file contains the functions which are prototyped in the header. // Convert from farenheit temperature to Celcius. float farenheit_to_celcius (float farenheit) {         return (farenheit - 32) * 5.0 / 9.0; }```
This is the driver.
Code:

```#include <iostream> #include "f2c.hpp" // The driver function is the one that actually -uses- the functions prototyped in the header. int main (void) {         float celcius;         float farenheit;         std::cin >> farenheit;         celcius = farenheit_to_celcius (farenheit);         std::cout << celcius;         return 0; }```
Note that we have two .cpp files which rely on each other. We have to compile them together, using this command (assuming you are using linux)
Code:

`\$ g++ main.cpp f2c.cpp`
• 07-13-2007
simpleid
You don't need to define PI, cmath has M_PI; and that it's more accurate than your pi... (not that it -really- matters, it just seems you didn't realize it.)
• 07-13-2007
robatino
Some compilers provide M_PI, although it's non-standard.

http://www.dbforums.com/showthread.php?t=428391
• 07-13-2007
dwks
M_PI is not standard. You might want to #define PI yourself. As for accuracy . . . http://3.141592653589793238462643383...om/index1.html

In an old project of mine I used
Code:

```#undef PI #define PI 3.14159265358979323846264338327950288419716939937510582097494459230\ 781640628620899862803482534211706798214808651328230664709384460955058223172535\ 940812848111745028410270193852110555964462294895493038196442881097566593344612\ 847564823378678316527120190914564856692346034861045432664821339360726024914127\ 3724587006606315588174881520920962829254091715364367892590360011330530548820466```
but that's rediculous. Did you know that with 39 digits of pi you can calculate the circumference of the universe, accurate to the width of a hydrogen atom? http://www.straightdope.com/classics/a3_357.html
• 07-13-2007
robatino
Code:

```#include <cmath> const double Pi = 4.*atan(1.);```
• 07-13-2007
dwks
I think that would be std::atan() unless there was a using directive involved.
• 07-13-2007
robatino
Yes, I keep forgetting that. Using gcc, it doesn't complain when I forget to put in the std::. I think I read something about the <c*> include files putting everything in both the global and std namespaces for now, or is it just gcc doing that?
• 07-13-2007
dwks
• 07-14-2007
gunghomiller
I appreciate u explaining it to me. I was unsure as to why i would compile the 2 .cpp together when one is user activated by inputing a radius and the other had fixed values i didnt know that I could do that thanks again.

Quote:

Originally Posted by QuestionC
Understand that the header/implementation/driver distinction is really of importance for larger programs. Headers and implementation are generally seperated for compiling efficiency.

You appear to be confused on terms. Here is a very bare-bones 3-file program which converts between temperature formats.

This is the header file.
Code:

```// Header files should only contain function prototypes. float farenheit_to_celcius (float celcius);```
This is the implementation file.
Code:

```#include "f2c.hpp" // The implementation file contains the functions which are prototyped in the header. // Convert from farenheit temperature to Celcius. float farenheit_to_celcius (float farenheit) {         return (farenheit - 32) * 5.0 / 9.0; }```
This is the driver.
Code:

```#include <iostream> #include "f2c.hpp" // The driver function is the one that actually -uses- the functions prototyped in the header. int main (void) {         float celcius;         float farenheit;         std::cin >> farenheit;         celcius = farenheit_to_celcius (farenheit);         std::cout << celcius;         return 0; }```
Note that we have two .cpp files which rely on each other. We have to compile them together, using this command (assuming you are using linux)
Code:

`\$ g++ main.cpp f2c.cpp`