Calculate the surface of the sphere radius

My code is work, but I have an issue with the result of my code. when I run with f10 the user is asked to input the value of r3 value and r2 value to calculate both of Volume and Large. with formula :
Volume = 4/3 * pi(3.14) * r^3
Large = 4 * pi(3.14) * r^2
for example : I give 2 value for both of them, then I run it and show the
value of Volume is 24, and
value of Large is 48.
pow function work fine,
r^3 which mean r*r*r, in example 2*2*2 = 8 and r^2 which mean r*r, in example 2*2=4.
BUT, when I count both Volume and Large on scientific calculator the results are different. on Calculator the volume result is 33.493333 and the large result is 50.24

My Question is why the result of calculate in my code is different with the result of calculate on scientific calculator ?
is there something wrong in my code ?
How do I make the same result in my code as calculator's result ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  /*  Calculate the surface of the sphere radius */
#include<iostream> //cout //cin
#include<math.h>  //Pow
using namespace std;
main(void)
{
	int a, pi;
	a=4/3;
	pi=3.14;
	float r3, r2, Volume, Large;
	cout<<"Calculate the surface of the sphere radius"<<endl;
	cout<<"Volume = 4/3 x Pi x r^3"<<endl;
	cout<<"Large = 4 x pi x r^2"<<endl;
	cout<<"Input r3 value to calculate the Volume = ";cin>>r3;
	cout<<"Input r3 value to calculate the Large   = ";cin>>r2;
	r3 = ("r^3 = %f\n", pow (r3, 3.0));
	cout<<"r^3 = "<<r3;
	cout<<endl;
	Volume = a * pi * r3;
	cout<<endl;
	r2 = ("r^2 = %f\n", pow (r2, 2.0));
	cout<<"r2 = "<<r2;
	cout<<endl;
	Large = 4 * pi * r2;
	cout<<endl;
	cout<<"Volume Result = "<<Volume<<endl;
	cout<<"Large Result  = "<<Large<<endl;
	cout<<endl;
}
Last edited on
a is int, and 4/3 is 1 as an int.
pi is an int. it has value 3
Hmmm... I thougt a and 4/3 are one as an int, pi is an int. This is my homework actually, I was asked to make algorithm and program to calculate volume and large of the sphere radius. with note : r is inserted as float type, as well as the output. that's why I insert r3, r2, Volume and Large as float type.
#include<math.h> //Pow
math.h is the C library. You are using C++ and not C so use the C++ library, cmath, instead.

main(void)
Missing the int for the return type.

float r3, r2, Volume, Large;
float's precisions are easily exceeded when doing calculations, especially when using pi. Prefer to use double instead.

1
2
	cout<<"Input r3 value to calculate the Volume = ";cin>>r3;
	cout<<"Input r3 value to calculate the Large   = ";cin>>r2;

It is counter-productive to ask the user for the radius twice, unless they really want to calculate the volume and surface area of different spheres. If they wanted to do that, they could run the program again. You only need a single variable for the radius.

r3 = ("r^3 = %f\n", pow (r3, 3.0));
Use cout to print variables.
Now my question is How to make the same calculate result in my code as calculator result ? any hints ?
My friend code work correctly without pow function, his formula is Volume = 4/3*pi*r*r*r and the large's formula is Large = 4*pi*r*r
here is his code

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
main()
{
float r;
float large;
float volume;
float pi=3.14;
cout<<"Input r value";cin>>r;

large=4*pi*r*r;
volume=4/3*pi*r*r*r;

cout<<"Large Value is = "<<large<<endl;
cout<<"Volume Value is = "<<volume<<endl;

return 0;
}

it's work correctly for large's calculate result as Calculator's result but not for the Volume's result.

sorry my english grammar is not very good
Hi integralfx, I gonna try it
Also:

Consider writing 4 as 4.0 It reinforces the idea that you are dealing with doubles.

It is a little inefficient to use the std::pow function for squaring or cubing values (because it probably might use a binomial expansion to calculate it), just multiply them out:


1
2
3
4
constexpr double FourThirds = 4.0/ 3.0;
constexpr double pi = std::acos(-1.0);

double Volume = FourThirds * pi * r * r * r;


Delay the declaration of a variable until you have a sensible value to assign to it, like I did with Volume above.

With the radius, you ask for a value and store it in variable r3, but then overwrite with the value of r cubed. Avoid doing that. You really only need one variable for the radius, I wouldn't bother with a separate one for r cubed.

Large is not a good name for a variable. Name things for what they are - maybe SurfaceArea ?

SurfaceArea ? Hmmm well, I'll change the name for that variable. I gonna try the codes and hints from you guys. Thanks for trying to help me.
YEAAAHHH IT'S WORKING!!! 100% CORRECT, I really really happy it's works 100%!!
wait...
I will show you guys the code later... now I can sleep peacefully

Thanks to Mr. jonnin, Mr. Integralfx and Mr. TheIdeasMan for give me the hints to solve my homework.
Last edited on
Ok This is the code :

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float r;
double FourThirds = 4.0/3.0, pi= 3.14, Volume, SurfaceArea;
cout<<"Calculate the surface of the sphere radius"<<endl;
cout<<"Enter r Value = ";cin>>r;
Volume = FourThirds*pi*r*r*r;
SurfaceArea = 4*pi*r*r;
cout<<"Volume = "<<Volume<<endl;
cout<<"SurfaceArea = "<<SurfaceArea<<endl;
return 0;
}

PS:

I am not use constexpr like example from TheIdeasMan, when I use it the code not working or error, so I just make it double and single variable for the r. I am very newbie so I don't know a lot about C++ keywords and how to use them correctly.

Once again, Thank You Very Much for Your Help.
pi= 3.14

If you press the PI key on your calculator, I doubt that it will return 3.14. :) Use more digits for higher accuracy.
for casual work, pi is often rounded at the 9: 3.1416. For schoolwork, even 3.14 isn't uncommon. But it does not cost anything to get a real value either... pulling up win/calc and hitting pi, copy, paste.. 10 seconds later ive got
pi = 3.1415926535897932384626433832795

Wow.... That increase my knowledge :) Thank you very much ! Mr. dahyden and Mr. jonnin
You can also get pi by acos( -1.0 ), as mentioned by TheIdeasMan above.
Last edited on
closed account (48T7M4Gy)
pi:
- take the sequence 113355
- take the second half 355 and divide by the first half 113
- pi = 355/113 approx
Topic archived. No new replies allowed.