Debugging Example - Sphere Program
The following is 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 should work and how to verify that the results are correct:
Here is the program:
#include <stdio.h>
int main(void)
{
int r
double v;
print("Please enter the radius of the sphere: );
scanf("%lf",&r);
//according to google, volume of sphere is 4/3 PI r^3, so we
//implement that here:
v = 4/3 * 3.14159 * r^3
print("The volume of the sphere with radius of %.2lf is: %.2lf\n",r,v);
return 0;
}
However, there are some bugs in the above program. Identify:
a) all stylistic issues b) all syntactic errors c) all logical errors.
How to solve this debugging problem:
To solve this debugging problem, take the following steps:
- read an understand the problem, what is the program suppose to do?
- fix the formatting so that the code is easy to read (identifying stylistic issues along the way)
- identify syntax errors
- look for typos
- look for missing semi-colons
- look mismatching backets and/or quotes
- look for logical errors, is the code doing what it is suppose to do?
Understanding the problem.
Based on the specs, these are the sequence of steps our program should perform:
- Create variables to store the radius and volume
- Prompt user for radius (output)
- Read in the radius value (input)
- Calculate the volume of a sphere (calculation)
- Display the result along with the original radius. (output)
Fix formatting and stylistic issues.
Looking at our program there are several stylistic issues.
- there is no indentation to the code. we need to indent all code in main
- we are using single character variable names. These should be fixed.
Fixing the formatting only we get this:
#include stdio.h
int main(void)
{
int radius
double volume;
print("Please enter the radius of the sphere: );
scanf("%lf",&radius);
//according to google, volume of sphere is 4/3 PI r^3, so we
//implement that here:
4/3 * 3.14159 * radius^3 = volume
print("The volume of the sphere with radius of %.2lf is: %.2lf\n",radius,volume);
return 0;
}
Fix Syntax errors:
scan the code looking for syntax errors...
Details
- PI is undefined.
- missing semicolon in line 4
- spelling error for function printf() (should be printf() not print()), lines 6 and 11
- missing ending double quote (") in printf()
- missing semicolon in line 10
- radius type does not match format code (or spec)... this could potentially be considered to be logical as the type does not meet spec.
- = operator is not communicative. We need to put the variable where we want result to go in line 10 on the left side of assignment not right
- the ^ operator does not mean exponent, it means bitwise XOR... this can be considered syntactic (as function won't compile) or logical as it doesn't do what it should. Note that there is no exponent operator in C. We can either use a library function or for something where the exponent value is small, we can just do the multiplications.
The assignment operator is NOT commutative. A = B is not the same as B = A. This is a really common errors for students first learning to program
Fixing Syntax errors.
- We can create a constant called PI and assign to it the value of PI.
#define PI 3.14159
- add semicolon to end of line 4
- fix spelling of printf() in lines 6 and 11
- add the missing double quote (")
- add semicolon to end of line 10
- change type for radius to double
- flip the left/right side of the = operator
- change
radius^3toradius * radius * radius
#include <stdio.h>
int main(void)
{
int radius;
double volume;
print("Please enter the radius of the sphere: );
scanf("%lf",&radius);
//according to google, volume of sphere is 4/3 PI r^3, so we
//implement that here:
4/3 * 3.14159 * radius^3 = volume;
print("The volume of the sphere with radius of %.2lf is: %.2lf\n",radius,volume);
return 0;
}
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 in a different way. Since exponents are just repeated multiplication, we can calculate 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.
Logical errors:
Fixing all syntactic errors results in this:
#include <stdio.h>
int main(void)
{
double radius;
double volume;
printf("Please enter the radius of the sphere: ");
scanf("%lf",&radius);
//according to google, volume of sphere is 4/3 PI r^3, so we
//implement that here:
volume = 4/3 * 3.14159 * (radius * radius* radius);
printf("The volume of the sphere with radius of %.2lf is: %.2lf\n",radius,volume);
return 0;
}
Logical Errors:
Once you have a program that compiles, your next part is to check for logical errors.
Here is the result of a run using the above program (the one without syntax errors):
Please enter the radius of the sphere: 5.0
The volume of the sphere with 5.00 radius is: 392.70
We get a program.. but does it produce the correct results? Using the same formula and doing the math, we see that program does not produce the correct results even though it compiled. This is a logical error. The result should be be 523.6 but it isn't... why?
So, what went wrong in our program and how do we find it?
If we look at this program, we know a few things...
Aside from the variable declarations we have only 4 lines of code. The most likely cause of the issue is line 10 where we calculate volume. It is not likely that radius is wrong since we echo that result out at the end and the radius is correctly outputed in the second line.
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?
Using 1.0 for the radius the expression essentially becomes
4/3 * pi * (1.0 * 1.0 * 1.0) = 4/3 * pi
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
The only way we would get 3.14 is if 4/3 * pi was 3.14. Notice 3.14 is the value of pi taken to 2 decimals, so the only way we can get this value is if 4/3 is 1.
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 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;
}