Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Write the source code
Index -> C++
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jhooper3581




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
jhooper3581




PostPosted: Mon Aug 31, 2009 2:28 am   Post subject: (No 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.
saltpro15




PostPosted: 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




PostPosted: 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




PostPosted: 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
A.J




PostPosted: 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)
saltpro15




PostPosted: 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
}
DtY




PostPosted: 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)
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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;
}
md




PostPosted: 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
saltpro15




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

Cool
md




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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:   
   Index -> C++
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: