Understand the problem
Understand what problem you are trying to solve. Read the problem description. Perhaps focus on the input-transform-output aspects of the problem. Or consider the objects and their actions in the problem. For now, we are using the first approach.
Work through an example on paper to see that you understand what is required. For example, if the distance is 60 and the speed is 60, what is the desired answer? Are you sure? What if the distance is 10 and the speed is 40? Hold onto these samples to use as test cases later.
If you don't understand the question and can't answer it on paper, how can you expect to write directions for a computer to do it?
Sketch out a solution on paper
Write down the inputs and outputs and their units on paper. It might look like:
Write out what you would expect the interaction with the program to look like:
Enter distance in miles: 10 Enter speed in miles per hour: 40 It takes 15 minutes to drive 10 miles at 40 miles per hour.
Develop a skeleton of the program
Write the skeleton of the program. This will be a minimal program and could be based on an existing similar program. In this case, we'll start from scratch. You could do this on a couple of sheets of paper, but most of us just start typing.
This skeleton should include
|
// Given a distance to travel in miles
// and the speed to travel in miles per hour,
// prints the number of minutes to travel.
// Beth Katz, September 2000
#include <iostream.h>
int main()
{
cout << "Computes time to travel "
<< "given distance and speed" << endl;
return 0;
} |
Obtain and print the input
We need to obtain the input, do the computation, and print the output. That approach works for a wide variety of problems. The next version/iteration of the program might obtain the input and print it.
Before obtaining the input, we must have appropriate space to hold it. This storage space must be the correct type (for now, integer, real, character, or string). Its name should remind you and the reader of its contents. If needed, you may assign an initial value. You might think of this declaration of a variable (or a constant if it doesn't change) as obtaining a "thing" of the appropriate size and shape to hold the information and writing a label on it. Think box for "thing" if you must be concrete. The input is distance in miles and speed in miles per hour or MPH. Both are integers. While including the units in the name is a bit bulky to type, it helps you remember what they are:
int distanceInMiles; int speedInMPH;
Now, ask for (prompt for) the input and read it. This is easily done in two statements for each input value. Remember that the stream operator << is used for output because it funnels the words and values into the cout (see-out) stream. The >> operator funnels input from the input stream cin (see-in) into a variable. Each variable read into or expression printed must have either >> or << in front of it. This piece of code prompts the user for the distance and reads the user's answer into the distanceInMiles integer variable.
cout << "Enter distance in miles: "; cin >> distanceInMiles;Do the same for the speed.
Then print what was read (echo it). Remember to put << in front of each expression to be printed. Include spaces inside the quote marks of the string literals so that the words do not squash together with the numeric values printed.
cout << "It takes ??? minutes to travel " << distanceInMiles
<< " miles at " << speedInMPH << " miles per hour" << endl;
The current main function is shown at right. Note that the order of the
statements is very important.
Calculate the time and print it | int main()
{
int distanceInMiles;
int speedInMPH;
cout << "Computes time to travel "
<< "given distance and speed" << endl;
cout << "Enter distance in miles: ";
cin >> distanceInMiles;
cout << "Enter speed in miles per hour: ";
cin >> speedInMPH;
cout << "It takes ??? minutes to travel "
<< distanceInMiles << " miles at "
<< speedInMPH << " miles per hour" << endl;
return 0;
}
|
Now we can do the computation and print the output.
To do this, we need a variable to hold the result of the calculation.
(We could do the calculation in the output statement,
but the longer approach is easier to understand.)
Declare an integer variable called timeInMinutes:
int timeInMinutes;
The natural formula to compute the time is distance divided by speed but we must consider the units as well as the characteristics of integer division. To handle the units, we would have distance divided by speed times 60 minutes in an hour.
However, whenever we use division with integers, we must be careful that we
don't lose any information in fractional parts. It is often a good idea
to do a multiplication first and then a division.
For example, 10 / 40 is 0.25 in real numbers but 0 in integer division.
Multiplying by 60 minutes in an hour first (10 * 60) gives 600 which
is then divided by 40 resulting in 15. Therefore, the expression to
compute the time is
distanceInMiles * 60 / speedInMPH
rather than the incorrect
distanceInMiles / speedInMPH * 60 // BAD calculation
It is also important to recognize that in C++ the = operator means
assignment. The value of the expression on the right-hand side of
the equal sign is put into the variable on the left-hand side.
Order is very important here. The object on the left-hand side must
be able to change. It must be one object. So we would write:
timeInMinutes = distanceInMiles * 60 / speedInMPH;
The compiler would complain about not being able to assign to the
left-hand side if we wrote:
distanceInMiles * 60 / speedInMPH = timeInMinutes; // BAD assignment
Where does the assignment go in the sequence of statements? distanceInMiles and speedInMPH must have received their values from the user before we can compute timeInMinutes. We cannot print the answer before we have computed it. So the assignment goes between the reading and printing. See the program below.
Now, modify the print statement to include the answer rather than question marks. In modifying that statement, remember to put the << before each item printed and spaces inside the quote marks of the string literals to space between words and numbers. See the modified program below.
The complete program
// Given a distance to travel in miles and the speed to travel
// in miles per hour, prints the number of minutes to travel.
// Beth Katz, September 2000
#include <iostream.h>
int main()
{
int distanceInMiles;
int speedInMPH;
int timeInMinutes;
cout << "Computes time to travel given distance and speed" << endl;
cout << "Enter distance in miles: ";
cin >> distanceInMiles;
cout << "Enter speed in miles per hour: ";
cin >> speedInMPH;
timeInMinutes = distanceInMiles * 60 / speedInMPH;
cout << "It takes " << timeInMinutes << " minutes to travel "
<< distanceInMiles << " miles at "
<< speedInMPH << " miles per hour" << endl;
return 0;
}
Not done yet
The program compiles cleanly. It seems to be indented reasonably. It contains the required comments. So we are done. Right? No.
Even if you've been careful in your development, you need to check whether your program meets the specification (problem description). Is the data read in the correct order? Are the units correct? Is the output printed in the correct format? Does the program use functions for repeated tasks? Are appropriate data and control structures used?
But even then, the program isn't necessarily correct. You should run several tests with normal and somewhat abnormal data. Create test data sets that include both the input and the expected output. Then, run your program using that input and check whether you get the expected output.
If you don't get the expected output, look over the program carefully.
We'll study some ways of debugging the program later. For now consider these
common logic errors:
- reading the data in incorrect order
- declaring variables with the wrong type
- losing calculation precision in integer division or using very small or very large numbers
- function parameter order switched
- incorrect sequencing of calculations
- printing the wrong variable in the output