Skip to main content

Sphere

Write a program that will ask the user to enter the radius of a sphere (a floating point value). The program will then print out the volume of that sphere.

This video shows how the program would work and how to verify that the results are correct:

What we need:

  1. Create something to store the radius
  2. Prompt user for radius (output)
  3. Read in the radius value (input)
  4. Calculate the volume of a sphere (calculation)
  5. Display the result along with the original radius.

Now, if we look at the above, we actually already know how to do steps 1, 2, 3 and 5. This is very similar to the echo program we wrote in the previous chapter. The only step that is really new is the need to do a mathermatical calculation.

Getting Started:

Applying what we already know, we can start with the following program:

#include <stdio.h>
int main(void)
{

double radius; //the problem description states that radius is a floating point
//use double for better accuracy

double volume; //it stands to reason that if the radius is a floating point, the
//volume will also be floating point

printf("Please enter the radius of the sphere: "); //prompt
scanf("%lf",&radius); //read data into radius

/*do some math here to find volume*/


//echo the input and display the result
printf("The volume of the sphere with %.2lf radius is: %.2lf\n",radius, volume);
}

The code in the above is not all that different than our echo program in the previous chapter. The only difference is that we need to add a calculation to figure out what volume is. Currently volume is underfined.

Doing any web search, we find the following formula to calculate the volume of a sphere:

volume of sphere = (43)(π)(radius3){({{4}\over{3}})}(\pi)({radius}^3)

How would we apply this formula?

In C, whenever we want to do a calculation, the variable where we want the result to go must be on the left hand side of the assignment operator (=). The expression that is used for the calculation must go on the right hand side of the assignment operator.

Thus, our expression must look like the following:

volume = expression to calculation volume goes here
warning

The assignment operator is NOT commutative. A = B is not the same as B = A. This is a really common errors for students starting in programming.

So, what we need to do now is turn the expression we found and convert it into an expression that C can calculate. Here are some basic arithmetic operators:

Operator NameOperator Symbol
Multiplication*
Division/
Modulus (remainder)%
Addition+
Subtraction-

Given the above operation, lets start by simply doing a simple translation of the expression:

volume = 4/3 * PI * radius^3;

Syntax errors

Now, if we tried to compile the program, we will get a syntax error:

error: use of undeclared identifier 'PI'

This is because the compiler doesn't know what PI is. there is nothing in our program named PI

At this point, we have two options:

  1. We can create a constant called PI and assign to it the value of PI. A constant is similar to a variable, in that it is a piece of information that has a name. However, constants are not modifiable once it is initialized. There are two wayts to create a constant in C. One way is to use the #define pre processor statement. Add this statement to the top of your file below your includes

    #define PI 3.14159
  2. Alternatively, you can also simply replace PI with the value of π\pi in the expression. This is the solution we will use below:

volume = 4/3 * 3.14159 * radius^3;

If you try this you will get another error:

invalid operands to binary expression ('double' and 'int')

The reason for this error has to do with ^ operator. We often view ^ as exponent operator. However, this is not the case here. I turns out that the ^ operator is not an exponent operator in C. It is the bitwise XOR operator. In fact, there is no exponent operator in C at all. Thus we need to calculate radius3radius^3 in a different way. Since exponents are just repeated multiplication, we can calculate radius3radius^3 by multiplying radius together 3 times. This turns our expression into this.

 volume = 4/3 * 3.14159 * radius * radius * radius;

If you put this expression into the program, it will compile and you can run the program. However, when you check the output, you will find that it isn't quite right.

Here is the result of a run using the above expression:

Please enter the radius of the sphere: 5.0
The volume of the sphere with 5.00 radius is: 392.70

Now, we know that this value is actually wrong... the result should be 523.6 but it isn't... why?

The type of error represented by the above is called a logical error. A logical error is an error where the logic of the program is not correct... this could be a miscalculation, a wrong sequencing of statements, a misplaced logical structure and more. Logical errors are not caught by the compiler because the program is syntactically correct. It is similar to writing out a recipe with gramatically perfect sentences.

So, what went wrong in our program and how do we find it?

One technique is to go through the calculation with a simpler value. If I used 1.0 for the radius, what is the result? What should the result be?

When we run the program with a 1 for the radius this is what happens:

Please enter the radius of the sphere: 1
The volume of the sphere with 1.00 radius is: 3.14

If we apply the formula by hand (or use an online calculator) you will find that the result is roughly 4.187

The interesting thing about using the value of 1 is that we can simplify the equation because anything multiplied by 1 is 1.

Our expression is this:

 volume = 4/3 * 3.14159 * radius * radius * radius;

Now, since radius is 1, we are calculating:

 volume = 4/3 * 3.14159 * 1 * 1 * 1

However, 1 _ 1 _ 1 = 1

So we effectively the expression is:

 volume = 4/3 * 3.14159 * 1

Now, anything multiplied by is itself... so the calculation that is actually being made is just:

 volume = 4/3 * 3.14159

Some how, this expression is 3.14...which is just pi expressed to 2 decimal places... so it would seem like 4/3 is coming out to 1... why would this be?

The reason that this is happening has to do with data types. If the operands (the values we are using to do the calculation) are integers, the result will be an integer. If one or more of the operands are floating point then the result is a floating point.

In the case of 4/3, both 4 and 3 are integers. Thus the result of this expression is an int. It isn't 1.333333 since that would be a floating point value. Thus 4/3 results in an integer value. For division, the way that this happens is that decimal values are simply chopped off (truncated). The result is not rounded. if you calculated 5/3, the result would still be 1 because 5/3 = 1.666667 however we chop off all the values after the decimal place so 5/3 = 1.

Thus to fix our problem, we need to ensure that we are doing floating point calculations. If we make even one of the two values a floating point, we will get the expression we want. We can do this by simply adding a .0 after one or both of the values:

 volume = 4.0/3 * 3.14159 * radius * radius * radius;

Putting all of this together, our final program is:

#include <stdio.h>
int main(void)
{

double radius; //the problem description states that radius is a floating point
//use double for better accuracy

double volume; //it stands to reason that if the radius is a floating point, the
//volume will also be floating point

printf("Please enter the radius of the sphere: "); //prompt
scanf("%lf",&radius); //read data into radius

volume = 4.0/3 * 3.14159 * radius * radius * radius;


//echo the input and display the result
printf("The volume of the sphere with %.2lf radius is: %.2lf\n",radius, volume);

return 0;
}