Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Write the source code
Author Message
jhooper3581

Posted: Mon Aug 31, 2009 2:17 am   Post subject: Write the source code

Write a C++ source code that will read an input of radius of a circle and outputs the area of the circle.

jhooper3581

Posted: Mon Aug 31, 2009 2:28 am   Post subject: (No subject)

My solution.
 code: #include #include using namespace std; int main() {   // Mathematically it's obvious that r >= 0, where r is the radius.   double r;   cout << "Enter a radius where it is greater than or equal to 0: ";   cin >> r;   const double PI(3.1416);   double area(PI * pow(r, 2));   cout << "The area of the circle with radius " << r << " is " << area << "." << endl;     system("PAUSE");    return 0; }

If any other interesting solutions are available, please post them.
saltpro15

Posted: Mon Aug 31, 2009 8:03 am   Post subject: RE:Write the source code

Nice. My only recommendation is to use a longer value of Pi, as really, really big numbers could give you precision errors. I like to use the first 9 digits.
Insectoid

Posted: Mon Aug 31, 2009 8:20 am   Post subject: RE:Write the source code

Is ^ not a function in C++? I notice you use pow(x, 2) which I assume means x^2. How hard IS it to put that into a language? (Or is it already reserved for a different command?)
saltpro15

Posted: Mon Aug 31, 2009 8:38 am   Post subject: RE:Write the source code

in C++ "^" is used as XOR.

and why the heck would you not just use x * x ?
A.J

Posted: Mon Aug 31, 2009 8:48 am   Post subject: RE:Write the source code

You could also output the area to a given number of decimal places like so:
 c++: #include #include using namespace std; double r, area; int DecimalPlaces; int main() {     cout << "Enter a radius where it is greater than or equal to 0: " << endl;     cin >> r;     cout << "To how many decimal places do you want the area to be given to you?" << endl;     cin >> DecimalPlaces;     area = M_PI * (r*r);     cout << "The area of the circle with radius " << r << " is ";     cout.setf (ios_base::fixed, ios_base::floatfield);     cout.precision (DecimalPlaces);     cout << area << "." << endl;     system ("PAUSE");     return 0; }

(Note: M_PI is a built-in constant that can be accessed by importing cmath/math.h)
saltpro15

Posted: Mon Aug 31, 2009 8:57 am   Post subject: Re: Write the source code

Just because I'm waiting for my pancakes to finish cooking, here's another version
 c++: #include #include double r, area; main() {     printf("Enter a radius greater than or equal to 0\n");     scanf("%f", &r); // scanf needs a pointer when inputting variables, hence &r instead of r     area= M_PI * (r*r);     printf("%.5f\n", area); // the .5 will print to 5 decimal places }
DtY

Posted: Mon Aug 31, 2009 10:47 am   Post subject: Re: RE:Write the source code

insectoid @ Mon Aug 31, 2009 8:20 am wrote:
Is ^ not a function in C++? I notice you use pow(x, 2) which I assume means x^2. How hard IS it to put that into a language? (Or is it already reserved for a different command?)

I believe the reason that there is no power operator in C/++ is because there's no good way to implement it (that works with decimals and all that), and giving an operator will make people dependant on it (like using x**2 instead of x*x), instead it's a lot more work to #include <math.h> and then link it with the math library (if you want to do decimal powers)

CodeMonkey2000

Posted: Mon Aug 31, 2009 2:21 pm   Post subject: Re: RE:Write the source code

insectoid @ Mon Aug 31, 2009 8:20 am wrote:
Is ^ not a function in C++? I notice you use pow(x, 2) which I assume means x^2. How hard IS it to put that into a language? (Or is it already reserved for a different command?)

As saltpro15 already said, ^ is the XOR operation. The function pow is part of the cmath library, which is a C library. You could theoretically create your own number class, and overload the ^ operator for powers. Like so:

 c++: #include #include class INT {         public:                 //does all standard operations like a good integer should                 INT();                 INT(int value) {val=value;}                 INT operator*(INT other) const {return INT(val*other.val);}                 INT operator/(INT other) const {return INT(val/other.val);}                 INT operator+(const INT &other) const {return INT(val+other.val);}                 INT operator-(const INT &other) const {return INT(val-other.val);}                 INT operator^(const INT &other) const {return INT((int)pow((double)val,(double)other.val));}                 INT operator-() const {return INT(val*-1);}                         const INT &operator*=(const INT &other){val*=other.val; return *this;}                 const INT &operator/=(const INT &other){val/=other.val; return *this;}                 const INT &operator+=(const INT &other){val+=other.val; return *this;}                 const INT &operator-=(const INT &other){val-=other.val; return *this;}                 const INT &operator=(const INT &other){val=other.val; return *this;}                                 //some code shamelessly stolen from wtd                 friend std::ostream& operator<<(std::ostream& out, const INT& other);         private:                 int val; }; std::ostream& operator<<(std::ostream& out, const INT& other) {     return out <
md

Posted: Mon Aug 31, 2009 6:02 pm   Post subject: Re: Write the source code

saltpro15 @ 2009-08-31, 8:57 am wrote:
Just because I'm waiting for my pancakes to finish cooking, here's another version
 c++: #include #include double r, area; main() {     printf("Enter a radius greater than or equal to 0\n");     scanf("%f", &r); // scanf needs a pointer when inputting variables, hence &r instead of r     area= M_PI * (r*r);     printf("%.5f\n", area); // the .5 will print to 5 decimal places }

Also... I am moving this out of C++ Help, since obviously it has nothing to do with help
saltpro15

Posted: Mon Aug 31, 2009 7:06 pm   Post subject: RE:Write the source code

md

Posted: Mon Aug 31, 2009 7:17 pm   Post subject: RE:Write the source code

Nein. "math.h" is the C math library - infact the exact same as cmath in most cases (sometimes some things are undefinded).

However, using the C math library is really the correct thing to do in this case - if you use it as "cmath".
jhooper3581

Posted: Mon Aug 31, 2009 7:35 pm   Post subject: (No subject)

How do you guys use that type of code typing posting in this forum? (Colored texts, et cetra).
DtY

Posted: Mon Aug 31, 2009 7:36 pm   Post subject: Re: RE:Write the source code

md @ Mon Aug 31, 2009 7:17 pm wrote:
Nein. "math.h" is the C math library - infact the exact same as cmath in most cases (sometimes some things are undefinded).

However, using the C math library is really the correct thing to do in this case - if you use it as "cmath".

Does c++ have it's own math library?
bbi5291

Posted: Mon Aug 31, 2009 8:24 pm   Post subject: Re: Write the source code

I guess there really was no reason to introduce a new math library in C++. You can't add templates, since the math functions are coded in assembly. (I suppose I/O does get a new library because there's much more room for flexibility)
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: