Be a Supporter!

C++ sphere math

  • 868 Views
  • 16 Replies
New Topic Respond to this Topic
Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
C++ sphere math 2005-07-09 08:57:43 Reply

Just made a C++ prog that takes a number and finds the circumfrence, surface area, area, and volume of a sphere with that raduis. However im not sure if these equasions work correctly. I forgot the forumlas for finding these measurments so i had to look them up. However doing them on paper seems to be easier than doing them in C++. I have gotton some very odd measurments. heres the program.

#include<iostream>
using namespace std;

int main() {
double radius, circum, SA, vol, area, pie;

pie = 3.14;

cout << "Enter a radius: ";
cin >> radius;
cout << endl;

//circumference
circum = pie * (2 * radius);
cout << "Circumference: " << circum << endl;

//surface area
SA = 4 * pie * radius;
SA = SA * SA;

cout << "Surface area: " << SA << endl;

//Volume
vol = 4 * pie * radius;
vol = (vol * vol * vol) / 3;

cout << "Volume: " << vol << endl;

//Area
area = pie * radius * radius;
cout << "Area: " << area << endl;

fflush( stdin );
cin.get();
return 0;
}

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to C++ sphere math 2005-07-09 09:04:30 Reply

it's spelled "PI" not pie :P

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++ sphere math 2005-07-09 09:07:26 Reply

At 7/9/05 09:04 AM, Inglor wrote: it's spelled "PI" not pie :P

That isnt by biggest problem. Im asking about the equasions. If a variable is mispelled, so what at this point.

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++ sphere math 2005-07-09 09:55:08 Reply

I see you're playing with the HTS challenges I gave you. Anyway, what you may want to do is include <math>. That lib includes a const double value of pie. Just test the formula's on paper and then in the program and see if it works. I'll post my code later if you wish.

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ sphere math 2005-07-09 10:56:41 Reply

Stuff like this is much easier in Python:

def getFacts():
pi = 3.141592
while ( 1 ):
r = raw_input( "Enter the radius of a sphere: " )
try:
r = float( r )
break
except:
print "Try entering a number next time..."
print
print "The circumference is:", 2 * pi * r
print "The volume is:", 4.0 / 3 * pi * r * r * r
print "The surface area is:", 4 * pi * r * r

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++ sphere math 2005-07-09 11:31:25 Reply

k i did
#include <math.h>
#define PI 3.1415926535
at the top of my program and removed pie from my program and used PI

I havnt tested these on paper yet. but i would like to ask what the hell this run means:
<--start run
Enter a radius: 500

Circumfrence: 3141.59
Surface area: 3.94784e+007
Volume: 8.26834e+010
Area: 785398
end run-->

Is that saposed to be C++'s scientific notation or somthing?and if so how do i
i) read it (personally, not the program) as a hole number? Im quite good with scientific notations but i dont know what is what in that reading.

ii) make the program show a hole number instead of a scientific notation?

ill try some of these on paper now and see what result i get.

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++ sphere math 2005-07-09 12:07:44 Reply

At 7/9/05 10:56 AM, Begoner wrote: Stuff like this is much easier in Python:

It's the same thing, just depends on how you write it. This write decided to save computations into a value and display the value. In your code, you just directly displayed the final computation of the equation and didn't save it to a variable. So to say that it is much easier is pretty much useless, because it achieved the specified task in a different way.

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ sphere math 2005-07-09 12:36:04 Reply

At 7/9/05 12:07 PM, Ravens_Grin wrote:
At 7/9/05 10:56 AM, Begoner wrote: Stuff like this is much easier in Python:
It's the same thing, just depends on how you write it. This write decided to save computations into a value and display the value. In your code, you just directly displayed the final computation of the equation and didn't save it to a variable. So to say that it is much easier is pretty much useless, because it achieved the specified task in a different way.

I'm saying that Python is much more usefu and easier to codel for stuff like this, not that it's shorter.

HalfAssed
HalfAssed
  • Member since: Jun. 24, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to C++ sphere math 2005-07-09 12:45:57 Reply

At 7/9/05 11:31 AM, BulletProof57 wrote: Enter a radius: 500

Circumfrence: 3141.59
Surface area: 3.94784e+007
Volume: 8.26834e+010
Area: 785398
end run-->

i) read it (personally, not the program) as a hole number? Im quite good with scientific notations but i dont know what is what in that reading.

ii) make the program show a hole number instead of a scientific notation?

i) Volume, for instance, is the same as 8.26834*10^10 or 82,683,400,000. "e+XXX" is the same as "10 to the power od XXX"

ii) You could always do it yourself by turning the result into a string, truncatiing the e+XXX portion, and move the decimal place XXX places right, inserting zeroes when necessary.

Amoebaman
Amoebaman
  • Member since: May. 18, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++ sphere math 2005-07-10 02:54:55 Reply

I see you're playing with the HTS challenges I gave you. Anyway, what you may want to do is include <math>.

it's <cmath>!

Amoebaman
Amoebaman
  • Member since: May. 18, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++ sphere math 2005-07-10 03:14:04 Reply

bulletproof 57, make those SA, radius etc. variables to long long double, that can hold BIG numbers, then that inputting 500 should give clear results.

Amoebaman
Amoebaman
  • Member since: May. 18, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++ sphere math 2005-07-10 03:15:10 Reply

Oh, and don't use math.h, if you don't have like some 10 year old compiler, use <cmath>

Amish
Amish
  • Member since: Mar. 13, 2003
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++ sphere math 2005-07-10 05:12:52 Reply

Ermm heres how i did it.

#include <iostream>

using namespace std;

int main() {
int rad;
double pi = 3.1415;

cout<<"Circle Stats \n\n";
cout<<"Enter a radius: ";
cin>>rad;
cin.ignore();
cout<<"\nThe area of a circle with radius "<< rad << " is: \t"<< pi*rad*rad << endl;
cout<<"The circumference of a circle with radius "<< rad << " is: \t"<< rad*2*pi <<endl;
cout<<"The surface area of a sphere with radius "<< rad << " is: \t"<< 4*pi*rad*rad <<endl;
cout<<"The volume of a sphere with radius "<< rad << " is: \t"<< (4/3)*pi*rad*rad*rad <<endl;
cin.get();
return 0;
}

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++ sphere math 2005-07-10 11:11:16 Reply

At 7/10/05 05:12 AM, --Amish-- wrote: Ermm heres how i did it.

And guess who gave him the link too.......... :p

How's the FT project?

Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to C++ sphere math 2005-07-10 11:57:19 Reply

Also, try rounding, 3.141592635 to 3.14159264 that way if you com;ile it, you an get it to work with most Texas Instruments.

Jezek
Jezek
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to C++ sphere math 2005-07-10 12:39:30 Reply

lol wut r we talking about???????????

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to C++ sphere math 2005-07-10 12:40:48 Reply

At 7/10/05 12:39 PM, imtheawesomest wrote: lol wut r we talking about???????????

Just leave, please. You offer nothing to the conversation because you have no idea as to what's going on. At least learn the basics of one or more langauges before you return to just chat.

To Craige, that's neat. =)