Thread: circle problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question 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.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.)
    Last edited by simpleid; 07-13-2007 at 07:23 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Some compilers provide M_PI, although it's non-standard.

    http://www.dbforums.com/showthread.php?t=428391

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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
    Last edited by dwks; 07-13-2007 at 12:17 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    #include <cmath>
    
    const double Pi = 4.*atan(1.);

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think that would be std::atan() unless there was a using directive involved.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    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 View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math-like problem with circles and coords
    By Purity in forum Game Programming
    Replies: 2
    Last Post: 12-12-2005, 01:45 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. problem with drawnin a user-defined line
    By Isometric in forum Windows Programming
    Replies: 3
    Last Post: 02-12-2002, 11:00 PM