Suppose your future employer comes up to you and says, ``I need you to write a program that asks the user for the radius of a circle and prints out that circle's area and circumference.'' Where do you start?
First of all, ask yourself, ``What is the input to the program?'' What is it? It's the radius of a circle. Now ask yourself, ``What is the output of the program?'' It's the area and circumference of the circle whose radius was the input. So, if we are given the radius, r, of a circle, how can we find the area and circumference? Well, if you recall, the appropriate formulae are :
OK, so how do we tell the computer to do this? Let's first write out a step-by-step set of instructions. Let's make them so simple and verbose that even a mother who can't program a VCR to record an episode of Star Trek could solve this problem.
How's this :
Now let's refine this even more so that Bart Simpson could do it.
OK, so let's start turning this into a C++ program. Start with the basic program that does nothing and add the above steps in as comments :
#include <iostream.h>
main()
{
// get the input value, the radius
// find the area of the circle (via above formula)
// find the circumference of the circle (via above formula)
// display the area and circumference of the circle
}
So far, we've used integer variables, but the radius of a circle can
certainly be a real number (so instead of an integer data type, we
need to use float). Pi (
) is a real number, so that has to be
a float. The area and circumference have to be float
also. Let's add the variable declarations as
well as the C++ code that does what the comments state.
// file : circle.C
#include <iostream.h>
main()
{
const float pi=3.14159;
float radius;
float area, circumference;
// get the input value, the radius
cout << "Enter the radius : ";
cin >> radius;
// find the area of the circle (via above formula)
area = pi * radius * radius;
// find the circumference of the circle (via above formula)
circumference = 2 * pi * radius;
// display the area and circumference of the circle
cout << "The area is " << area;
cout << " and the circumference is " << circumference << endl;
}
When you type in, compile and run this program, here is what you should get :
Enter the radius : 5 The area is 78.5397 and the circumference is 31.4159
The only new thing we see here is
const float pi=3.14159;
This is a ``constant'' declaration. Since the value of pi never changes, we can declare it to be a constant. So you ask, ``Why use constants?'' Suppose your programs gets very very very large. So large that you begin to forget which variable names represent what. In the beginning of your program, you use pi as the constant 3.14159. But later in your program, you use pi for something else. You may change the value of pi if you didn't declare it to be a constant. This could be disasterous if you later use pi and think that has the value 3.14159. If you declare it to be a constant, then the compiler will give you an error message if you accidentally try to change its value.