Advertisement

Newbie C++ Problem

Started by August 13, 2002 04:45 PM
5 comments, last by Noky 22 years, 3 months ago
Well it''s just as much of a math problem as it is a C++ problem. I need to find the radius (or diameter, or even circumference) of a circle when I''m only given the area. I doubt many people have the book but it is Exercise 4 of Chapter 3 in Teach Yourself C++ by Richard Riley. I have the math problem square(area/pi) to achieve the radius but there doesn''t seem to be a way to do that without using a function. So right now I''m looking for an operator (+,-,/,*) that I can use to square something.... Or can someone please give me a formula I can use to fine the radius/diameter/circumference or a circle with only knowing the value of the area? Area of a Circle: radius*radius*pi Circumference: diameter*pi Diameter: radius*2 Radius: Diameter/2 Bleu Shift - www.bleushift.tk
area/pi * area/pi, mayhaps?

But that just handles your square, doesn't fix the fact that the math underneath is wrong already.

Area = pi*r^2, so Radius = sqrt(area/pi)

[edited by - Deyja on August 13, 2002 5:57:37 PM]
Advertisement
r = sqrt((A/PI));
d = r + r;
c = 2*PI*r;

:: h0-0t ::
:: h0-0t ::
To square something without a function just multiply the variable to be squared by itself.

int x;
int y;
. . .

cout << endl
<< "Enter number:";
cin >> x;
y = x*x;
cout << y;
. . .

You can't do it without a function, unless you inline the functionality of sqrt (found in <math> ). If you really have to do it that way, try this:
float rad_squared = AREA / 3.1415927f;int iter = 0;float error = 0.0001f;float guess = rad_squared;float result = rad_squared;while((rad_squared-guess*guess) > 0 ? 	  (rad_squared-guess*guess) :      -(rad_squared-guess*guess) > error){    guess = result;    result = 0.5f*(guess+rad_squared/guess);    if(++iter > 100)        break;}float radius = guess; 

Please note: this method is heinous, attrocious, dispicable, inefficient, and all-around evil. I don't know why you might want to do it that way, it just seems to be what you were asking.

A better way is this (after including math and using namespace std):
float rad_sq = AREA/3.141593f;
float radius = sqrt(rad_sq);

Peace,
ZE.


//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on August 13, 2002 6:03:39 PM]

[twitter]warrenm[/twitter]

Some of you, I believe mis understood me when I said square(area/pi). Square was short for square root not ^2.

"But that just handles your square, doesn''t fix the fact that the math underneath is wrong already."

How so the formulas underneath were for reference not for solving my problem. radius x radius x pi is the same as pi x r^2.

Anyway, it seems the book is messed up then because it hasn''t taught me functions (even though I know them a little) and it expects me to perform an mathematical equation that demands the use of a function. Oh well... I will just leave that part of the program out and move on. Thanks for your help people.

Bleu Shift - www.bleushift.tk
Advertisement
Well, um, you use a lot of features before you know just what they are. For example, can you tell me how you would go about overloading the << operator as a friend method of an output stream class? Probably not, but you use it all the time with cout. So, there''s a difference in learning functions (that is, learning how they work and how to create them) and using functions. In this case, all you have to do is include a header and use the function sqrt(...) like a black box.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement