Computer Science Canada Write the source code 
Author:  jhooper3581 [ 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. 
Author:  jhooper3581 [ Mon Aug 31, 2009 2:28 am ]  
Post subject:  
My solution.
If any other interesting solutions are available, please post them. 
Author:  saltpro15 [ 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. 
Author:  Insectoid [ 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?) 
Author:  saltpro15 [ 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 ? 
Author:  A.J [ 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:
(Note: M_PI is a builtin constant that can be accessed by importing cmath/math.h) 
Author:  saltpro15 [ 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

Author:  DtY [ 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) 
Author:  CodeMonkey2000 [ 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:

Author:  md [ Mon Aug 31, 2009 6:02 pm ]  
Post subject:  Re: Write the source code  
saltpro15 @ 20090831, 8:57 am wrote: Just because I'm waiting for my pancakes to finish cooking, here's another version
That's C; using C++ links to C headers. Also... I am moving this out of C++ Help, since obviously it has nothing to do with help 
Author:  saltpro15 [ Mon Aug 31, 2009 7:06 pm ] 
Post subject:  RE:Write the source code 
Author:  md [ 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". 
Author:  jhooper3581 [ Mon Aug 31, 2009 7:35 pm ] 
Post subject:  
How do you guys use that type of code typing posting in this forum? (Colored texts, et cetra). 
Author:  DtY [ 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? 
Author:  bbi5291 [ 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) 
Author:  bbi5291 [ Mon Aug 31, 2009 8:27 pm ] 
Post subject:  Re: RE:Write the source code 
saltpro15 @ Mon Aug 31, 2009 8:03 am wrote: 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.
You can use M_PI, a macro defined in math.h / cmath, or acos(1.0) (the latter looks cooler, but I think it returns a double, so if you want a long double pi, then you're not taking advantage of all the bits of precision... I may be wrong though) 
Author:  CodeMonkey2000 [ Mon Aug 31, 2009 8:27 pm ] 
Post subject:  Re: Write the source code 
Out of curiosity, how do you register your own operators? For example I have a vector class, and I want to use "cross" as an operator, so I can do something like: A cross B; or A cross= B; 
Author:  bbi5291 [ Mon Aug 31, 2009 8:28 pm ] 
Post subject:  Re: Write the source code 
CodeMonkey2000 @ Mon Aug 31, 2009 8:27 pm wrote: Out of curiosity, how do you register your own operators? For example I have a vector class, and I want to use "cross" as an operator, so I can do something like: A cross B; or A cross= B;
You can't. Not in C++. (I wanted to edit my previous post to say that acos(1.0l) works) 
Author:  A.J [ Mon Aug 31, 2009 8:40 pm ] 
Post subject:  Re: RE:Write the source code 
@jhooper Use the following tag: [syntax='cpp'] Code goes here [/syntax] (except that you would replace the single quotes around cpp to double quotes) 
Author:  saltpro15 [ Mon Aug 31, 2009 8:40 pm ]  
Post subject:  
jhooper3581 @ Mon Aug 31, 2009 wrote: How do you guys use that type of code typing posting in this forum? (Colored texts, et cetra).
use syntax tags
insert language in between the brackets, cpp for C++ 
Author:  DtY [ Mon Aug 31, 2009 8:40 pm ] 
Post subject:  Re: Write the source code 
bbi5291 @ Mon Aug 31, 2009 8:24 pm wrote: 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)
Actually, now that I think about it, I remember reading that all the functions in math.h that use floating point numbers (all of them?) are overloaded to take in a float, double or long double, and return the same type (whereas they all take just double in C), so I guess <cmath> is different than <math.h>, or the compiler just automatically overloads them. 
Author:  andrew. [ Mon Aug 31, 2009 11:42 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?) It's like that in Java as well. 
Author:  jhooper3581 [ Mon Aug 31, 2009 11:55 pm ] 
Post subject:  
Thanks, guys! 
: 