Volume of a Sphere- Need Help

Hello! I am writing a program for a class I am taking, but I am stuck. The directions for the assignment are:

Write a program that (using a function and no math libraries) displays the volume of a sphere with a radius from 0 to 4.0 in .2 increments. Use float type for the return type and argument type.

The program runs fine; however, my teacher said it is incorrect because "You did not write a function for any of these programs. The result must take place in a function and be returned to main()" I'm lost.

Here is my REVISED code:

#include <iostream> //preprocessor directive

using namespace std;

float VolSphere (float r)

{
float volume;
volume = (1.33) * (3.14) * (r * r * r);
return volume;
}

int main()

{
float r = 0;
while (r <= 4)

{
cout << "Radius: " << r << " Volume: " << VolSphere (r) << endl;
r = r + .2;

}
return 0;

}
Last edited on
I removed the last return so the code reads:

{
cout << "Radius: " << r <<" Volume: " << VolSphere (r) << endl;
r + .2;
return 0;
}


}


The program now stops after one line, but I am still getting "0" for both radius and volume.
r + .2;

This adds the value .2 and whatever is in r, but doesn't actually update the value in r. You need an assignment statement.

Edit - the return statement ending the main function shouldn't be in the while loop.
Last edited on
This adds .2 to r, but doesn't actually update the value in r. You need an assignment statement.

An assignment statement is something such as:

r = r + .2
I think so, at least.

Otherwise, make a for statement, with syntax and example as follows:

1
2
3
4
for (iterator; conditional; increment)
{
//do stuff until the conditional isn't met.
}


1
2
3
4
for (double something; something < 10; something = something + 1)
{
//do stuff
}
I have tried adding "r =" in front of "r + .2" but I still get the same thing. I'm not sure where else to assign r.
I added back the "r=r+.2" statement and moved the return statement as you all suggested, and it works! Thank you!!
1
2
3
4
5
6
float r = 0;
while (r <= 4)
{
    cout << "Radius: " << r <<" Volume: " << VolSphere (r) << endl;
    r += .2;
}


note the += operator.
that's a shorter way of writing
 
    r = r + .2;

I submitted the assignment to my teacher, but he returned it saying that I did not write a function. I need to write a function that returns to main. I am lost. What do I need to do?
http://www.cplusplus.com/doc/tutorial/functions/

However, VolSphere is a function (that takes a float and returns a float, as requested). Ask your teacher what the VolSphere is.
Last edited on
closed account (z05DSL3A)
What was the code you handed in?

I need to write a function that returns to main. I am lost. What do I need to do?
He might mean that he wants the value returned from VolSphere(r) to be assigned to a variable in main before printing the variable to the screen.
Last edited on
@Grey Wolf-

The code I handed in:

#include <iostream> //preprocessor directive

using namespace std;

float VolSphere (float r)

{
float volume;
volume = (1.33) * (3.14) * (r * r * r);
return volume;
}

int main()

{
float r = 0;
while (r <= 4)

{
cout << "Radius: " << r << " Volume: " << VolSphere (r) << endl;
r = r + .2;

}
return 0;

}
I submitted the assignment to my teacher, but he returned it saying that I did not write a function. I need to write a function that returns to main.

If that is all he said, then he was mistaken.

You did write a function, and it does return to main.
@Chervil- That's what I thought :S
closed account (z05DSL3A)
I would say that he does want you to return the value to a local variable and then display the result. Try it but keep an eye on 0.0 and 4.0.

Also, you should try to make the formula more accurate. 1.33 is only sort of close to 4/3 and 3.14 is only sort of close to pi.

If you #include <math.h> then you may be able to use the constant M_PI. So the formula would become 4.0/3.0*M_PI*r*r*r. Note that it's 4.0/3.0, not 4/3. Since 4 and 3 are integers, 4/3 is 1.
Topic archived. No new replies allowed.