By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,123 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,123 IT Pros & Developers. It's quick & easy.

Finding the area of the circle using C++

P: 1
a c++ program to solve circle area
Mar 17 '07 #1
Share this Question
Share on Google+
21 Replies


Savage
Expert 100+
P: 1,764
a c++ program to solve circle area
What have you done so far?

Post your code here and someone will help you.
Mar 17 '07 #2

DeMan
100+
P: 1,806
I assume you know that:

A = (pi)r^2

If you make a program that implements this formula (pi is approximately 3.1415926535898 -> but you mayt be better off using the value in the math library)......
Mar 18 '07 #3

P: 5
remember man that in C++ the ' ^ ' symbol doesnt apply. you need to use this formula-
A=3.14*pow(R,2);
in pow the number goes first then the power.
Mar 18 '07 #4

arne
Expert 100+
P: 315
I assume you know that:

A = (pi)r^2

If you make a program that implements this formula (pi is approximately 3.1415926535898 -> but you mayt be better off using the value in the math library)......
If it is defined. Standard C does not have a constant for pi. If you need it, you have to define it for yourself (either by using #define or by means of the trigonometric functions).
Mar 18 '07 #5

DeMan
100+
P: 1,806
If it is defined. Standard C does not have a constant for pi. If you need it, you have to define it for yourself (either by using #define or by means of the trigonometric functions).
Hence the mention of the math library....which has M_PI
Mar 18 '07 #6

arne
Expert 100+
P: 315
Hence the mention of the math library....which has M_PI
Every C math lib has M_PI defined?
Mar 18 '07 #7

P: 5
can i just say that since floats and doubles(i assume thats what you're using)only allow for 3 digits after the desimal you really dont need an enormously acurate pi. 3.14 should do fine.
Mar 18 '07 #8

DeMan
100+
P: 1,806
floats and doubles only allow three places after the decimal?
Mar 19 '07 #9

Savage
Expert 100+
P: 1,764
can i just say that since floats and doubles(i assume thats what you're using)only allow for 3 digits after the desimal you really dont need an enormously acurate pi. 3.14 should do fine.

I think that floats and doubles allow up to six decimal places.

They have a next syntax +/-***.******,where * reprsent a integer number

So pi would be:3.141593,which is way more accurate than 3.14.
Mar 19 '07 #10

arne
Expert 100+
P: 315
I think that floats and doubles allow up to six decimal places.

They have a next syntax +/-***.******,where * reprsent a integer number

So pi would be:3.141593,which is way more accurate than 3.14.
No.
Float and double are much more precise than that. Google for IEEE 754.
Mar 19 '07 #11

Savage
Expert 100+
P: 1,764
No.
Float and double are much more precise than that. Google for IEEE 754.
Yes,but I was pointing on default float that have up to six decimal places,if float
is modifed it can show up to 39-40 decimal places in c.

I tested this in c by using "%.40f" in printf command.
Mar 19 '07 #12

P: 84
Yes,but I was pointing on default float that have up to six decimal places,if float
is modifed it can show up to 39-40 decimal places in c.

I tested this in c by using "%.40f" in printf command.

Any idea how to write pi without writing a number, i mean, is there any constant already defined in c++???
Mar 19 '07 #13

P: 5
coming back to what i said earlier you can change the number of digits after the decimal.(somthing about setf)but when you cout a float it usually goes to 3
Mar 20 '07 #14

Roonie
P: 99
actually, i believe that the way floats are stored is more like scientific notation; regardless where the decimal point is it is stored as: x.xxxxx^xxx. the decimal point is not at any set point in the number: the location of the decimal is stored in the exponent portion of the variable.

i believe my compiler uses 4 bytes to store a float, so there must be a good few digits stored.

and actually, i read something somewhere once about the continuity of floats. when used to represent a number with many digits before the decimal point, there are fewer digits to make reolution behind the decimal point. so when you plot out the numbers that can be stored in a float, there are places where resolution "thins out" so to speak.

p.s. has anyone heard from the original poster? :)
Mar 20 '07 #15

Roonie
P: 99
or make that that as values get larger (+ or -), the possible float values spread out on the real number line . . . the decimal is pushed out of the picture by a large exponent.
Mar 20 '07 #16

DeMan
100+
P: 1,806
couple with that the fact that they are represented in binary (not decimal)......

ie combinations of 1/2 , 1/4,1/8, 1/16, 1/32 etc etc etc
Mar 20 '07 #17

arne
Expert 100+
P: 315
Any idea how to write pi without writing a number, i mean, is there any constant already defined in c++???
As pointed out earlier in the thread there is no constant for PI defined in standard, but there's M_PI defined in (at least my) math.h.
Mar 20 '07 #18

P: 14
If You want a program to find the Area of a circle you can download my program at the fallowing Link removed as per Posting Guidlines . The source codes are also included in the file.

Alexandre Proulx,
May 25 '07 #19

P: 14
If You want a program to find the Area of a circle you can download a good program at www.alprogramming.wetpaint.com . The source codes are also included in the file so you can check the codes and compile them yourself (Nothing Complicated).

Alexandre Proulx,
May 25 '07 #20

P: 1
To get Pi I use the following trick:
#include <cmath>
const double Pi = 4 * atan((double)1.0)
May 28 '07 #21

Expert 10K+
P: 11,448

Post your reply

Sign in to post your reply or Sign up for a free account.