Back to DFS's Turing Page


From Problem to Program

The Problem

Given a triangle having a base of 10 and a height of 7, find its area.

The Algorithm

Determining what algorithm is needed to solve a problem is frequently an easy matter. In previous courses, you have had a number of exercises for which data was provided and you were asked to write a program which would perform relatively simple calculations and report the results. The required algorithm, the set of steps, could be as simple as: assign values to variables; use the variables in a formula; and print the result.

The Pseudocode

Your pseudocode should be concise and understandable. The following example may in fact be too verbose.

  1. Assign the dimensions of the triangle to appropriate variables.
  2. Calculate the area using the standard formula: A = bh/2.
  3. Print the result in a grammatical English sentence.

The Code

The last step in the process is to translate your pseudocode into the computer language being used. Certain things are required by the computer language, e.g., mathematical operators must be explicitly specified. The program you write, however, is not completely dictated by your pseudocode and the computer language. Variations in style are more than welcome. The following two illustrate this in a simple fashion, producing identical output. The first solution utilizes a separate put.. or put call for each item to be printed, e.g. text, variable, text, etc. (The two dots at the end of a put causes the cursor to remain on the output line.) The second combines several items into each of only two put statements.

var b: int %base
var h: int %height
var A: real %Area
%-----Assign values to variables-----
b := 10
h := 7
%-----Calculation-----
A := b * h / 2
%-----Print the result-----
put "The area of a triangle with a base of "..
put b..
put " units and"
put "a height of "..
put h..
put " units is "..
put A:0:1..
put " square units."

var b: int %base
var h: int %height
var A: real %Area
%-----Assign values to variables-----
b := 10
h := 7
%-----Calculation-----
A := b * h / 2
%-----Print the result-----
put "The area of a triangle with a base of ", b, " units and"
put "a height of ", h, " units is ", A:0:1, " square units."

© 1997-2004 DFStermole
Created: 23 Aug 97
Last Modified: 16 Feb 04