Topics:
Top-Down Design
Step-wise refinement
Algorithm
Programmer's Algorithm for attacking general problems
Pseudocode and Flowcharts
Top-Down Design
To be a good problem solver and programmer you need to use a systematic method for solving the problem at hand and designing your program. You should use a top-down structured approach. When solving a problem it is important to get the "big picture" first. A high-level task description should always proceed before details of any sub-problem are carried out.
Stepwise refinement
This is a programming design strategy based on the process of defining the major steps or tasks involved in solving a problem. These tasks are then refined and themselves divided into subtasks. This process of refinement and division continues until the refined tasks can be easily converted to code in a high-level programming language such as C++. Refined solutions are often expressed in pseudocode, an informal use of English statements which can be translated directly into a programming language such as C++.
Algorithm
An algorithm is a sequence of steps for solving a problem, i.e., a "recipe" for solving a problem. The number of steps must be finite and each step must be well-defined and implementable, say by a computer. Some commonly encountered algorithms include:
1. Recipe for baking a cake.
2. Algorithms for I/O (input and output)
3. Searching algorithms
4. Sorting algorithms
5. Algorithms for finding shortest paths in networks, etc.
The following are the steps or algorithm that you should follow when writing a program:
Programmer's Algorithm for attacking general problems.
1. Define the problem; have a thorough understanding, particularly of the
input and output.
2. Carefully plan the solution to the problem using Top-Down Design.
3. Generate pseudocode using stepwise refinement.
4. Code the program in C++.
5. Thoroughly test and debug the program.
6. Document the program, include sufficient comments. Cleanup the code for
others to read.
We illustrate this methodolgy with an example problem.
Problem. Write a program for determining the type of triangle given its three side lengths.
1. Defining the problem
Input: Float numbers A, B and C representing the three sides.
Output: One or more messages indicating the type of triangle input.
Processing: Employ conditional expressions and selection to print out
the corresponding triangle type.
2. Planning the solution using top-down design
Initial Algorithm
1. Get Data.
2. Test the data to determine triangle type
a. Test equality of edges to determine types: equilateral, isosceles, scalene
b. Test using Pythagorean Theorem for types: right, obtuse, acute
3. Output results.
First-Level of Refinement
1. Get Data.
1.1 Write a program description to user.
1.2 Prompt user to enter sides A, B, and C with C largest.
1.3 Input A, B, and C.
2. Calculate Triangle Type
2.a Use equality testing and multi-way selection to determine if the triangle
is equilateral, isosceles, or scalene.
pseudocode:
if all three sides equal
then
print "equilateral"
else if two sides are equal
then print "isosceles"
else
print "scalene"
2.b Use Pythagorean Theorem and multi-way selection to determine if triangle is a right, obtuse, or acute
pseudocode: if sides satisfy C^2 = A^2 + B^2
then print "right"
else if sides
satisfy C^2 > A^2 + B^2
then print "obtuse"
else print "acute"
3. Output results.
3.1 Results output in Step 2
In this simple example we need only one level of refinement. In more sophisticated programs many levels will be required.
Now we will code the program for testing triangle types
//*************************************************************************
//Programmer: Fred Annexstein
//Last update: 01/10/2001
//Program purpose: This program inputs the threes side lengths of a
//triangle and determines the type of triangle from the input.
//*************************************************************************
//PREPROCESSOR DIRECTIVES
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//VARIABLE DECLARATIONS
float A,B,C // Three side lengths
//DESCRIPTION OF PROGRAM
cout << "This program determines the type of triangle from the side lengths";
//INPUT TWO SIDES OF RIGHT TRIANGLE
cout << "\n\nEnter length of side A: ";
cin >> A;
cout << "\nEnter length of side B: ";
cin >> B;
cout << "\nEnter length of longest side C: ";
cin >> C;
//DETERMINE THE TRIANGLE TYPES BASED ON SIDE EQUALITY
if ((A==B) && (B==C))
cout<< “The triangle is an equilateral triangle.\n”;
else if ((A==B) || (A==C) || (B==C))
cout << “The triangle is isosceles.\n”;
else cout << “The triangle is scalene.\n”;
//DETERMINE THE TRIANGLE TYPES BASED ON PYTAGOREAN THEOREM
if ((A*A + B*B) == (C*C))
cout<< “the triangle is right triangle.\n”;
else if ((A*A + B*B) < (C*C))
cout << “The triangle is obtuse.\n”;
else cout << “The triangle is acute.\n”;
return 0;
} //END main()
Note the extensive use of commenting in this program. The two forward slashes // indicates that the line is a comment line to be ignored by the C++ compiler. It is not executable.
It is very important that your programs are well-commented, so that they are easy to read.
Avoid over-commenting and redundant comments, since these could actually make your program less readable.