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.
code:
#include <iostream>
#include <cmath>
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.

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 ? Laughing

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:
c++:

#include <iostream>
#include <cmath>
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)

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
c++:

#include<cstdio>
#include<cmath>

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
}

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:


c++:

#include <iostream>
#include <cmath>

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 <<other.val;
}

int main()
{
        INT a=2;
        INT b=3;
        std::cout<<"a="<<a<<std::endl
                    <<"b="<<b<<std::endl
                    <<"a+b="<<(a+b)<<std::endl
                    <<"b-a="<<(b-a)<<std::endl
                    <<"a^b="<<(a^b)<<std::endl;
        return 0;
}

Author:  md [ 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<cstdio>
#include<cmath>

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
}


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 Razz

Author:  saltpro15 [ Mon Aug 31, 2009 7:06 pm ]
Post subject:  RE:Write the source code

Cool

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

code:
[syntax=" "][/syntax]


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!


: