6.3 Algorithm Design


6.3 Algorithm Design

As discussed earlier, an algorithm describes the method of solution to a problem in a precise, clear, and complete form. Algorithm design is carried out in the design phase of the software life cycle, and it is the most challenging task in the entire process. Various kinds of notations are used to describe an algorithm, such as informal English, flowcharts, and pseudo-code.

6.3.1 General Solution

In designing and writing an algorithm, the most general solution is the one that should be used. The intent is to describe the solution for a group of very similar problems. Sometimes, this group is called a family of problems.

Consider the problem of calculating the area of a triangle. Given the three sides of a triangle, compute its area. In this case, the algorithm should describe not only the solution to a particular triangle, with sides 3, 7, and 12, but also a solution to any triangle. Therefore, the solution sought is the one for a family of very similar problems; this case is the area of a triangle.

6.3.2 Algorithm for the Area of a Triangle

Consider the design of a solution for the problem of calculating the area of a triangle, described previously. The problem is to design an algorithm to calculate the area of a triangle, using the values of its three sides x, y, and z. The algorithm in informal pseudo-code is:

  1. Read the value of side x from the input device (keyboard).

  2. Read the value of side y from the input device (keyboard).

  3. Read the value of side z from the input device (keyboard).

  4. Compute s = 0.5(x + y + z), as an intermediate result.

  5. Compute area =

  6. Print value of area to the output device (video screen).

Note that the algorithm is written in a very informal notation, and the value of the sides of a triangle must represent a well-defined triangle. This description of the algorithm is written in an informal pseudo-code notation.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net