Guidelines for Preparing Algorithm

What is an algorithm?

To those who aren't math-inclined, algorithms can sound scary. But they don't have to be . Algorithms are part of life, Any systematic, step-by-step set of instructions for solving a problem is an algorithm. Examples would include the instructions for programming a VCR, a 1040 income tax form or a cake recipe. Every computer program is an algorithm that tells the computer how to do something.

The term algorithm is after the name of a famous ancient Persian Mathematician called Alkharazmi. Within programmers community algorithm is used to design programs. There are many tools, however, available to prepare program design. The one that we will be using in our programming courses is called pseudocode.

Pseudocode     

A pseudocode is a compromise between a formal programming language and a natural (or human) language. The purpose of using a pseudocode in developing a program design is to provide a means of expressing the design which exhibits a degree of structure, but which avoids the exactness required by formal programming languages.

In a professional program development environment, especially those stressing group programming efforts (Which is virtually all such environments), pseudocode tend to become highly formalized themselves. These formalized pseudocode are often referred to as design or program specification language.

In our programming classes, we will be using a more informal pseudocode for expressing program designs. Our Pseudocde will be a blend of C++/Java and English language. C++/Java will be used to impart structure to the decomposition of the program into functions and to illustrate the flow of control within and between these routines. English will be used to express simple actions (or steps) within a function, steps which would be unnecessarily cumbersome to express using C++/Java.

The elements of the our pseudocode which will be drawn from C++/Java are listed below:

  • Constants, defined data types and variables

  • Functions (declarations and calls)

  • Control structures ({...}, if, else, while, do-while, for etc.)

The intend in using C++/Java as the basis of our pseudocode is to produce designs, the English portion of which can be "filled in" to produce a C++/Java program. Thus, it is no coincidence that your designs will look like somewhat imprecise C++/Java programs. See Deitel & Deitel pages 70-75 for an example of the evolution of a program from pseudocode design to C++ implementation. Similarly see Lewis and Loftus pages 109-111 for example of the evolution of a program from pseudocode design to Java implementation.

Algorithm layout

Algorithm items appear in the following order

    A description of the program
    Pre: A list of all constants and input variables to the program
   
Post: A list of all outputs from the program
    A list of all auxiliary variables that you use in your program
   
A list of all functions that you use in your program.

    Algorithm for main program

pre:

post:

pseudocode

    Algorithm for all functions

pre:

post

pseudocode

A Simple Example (This example does not have function)

Problem

Mr Programmer has kept track of several tankfuls of gasoline by recording miles driven
and gallons used for each tankful. Develop a  program that will input the miles
driven and gallons used for each tankful. The program should calculate and display the
miles per gallon obtained for each thankful. The program should terminate when the user enters n. The program should calculate and display the miles per gallon obtained for each thankful. After processing all information the program should calculate and display the average miles per gallon obtained for all tankful.

Pre: (Input Variables): gallons, miles of type float
Post: (Output Variables): miles_per_gallon, average of type float
Auxiliary Variables: total_gallons, total_miles of type float, yes_no of type charcater

First Refinement

Initialize total_gallons and total_miles to zero.
Initialize yes_no to 'y'
Input the gallons used and the miles driven, and calculate and print the
miles/gallons ratio for each tank of gas.
Add miles to total_miles and gallons to total_gallons
Calculate and display the average and total_miles/total_gallons.

Second Refinement

Initialize total_gallons to zero
Initialize total_miles to zero
Initialize yes_no to 'y'

while (yes_no is equal to 'y')
{

Input the gallons used for the current tank
Input the miles driven for the current tank
Add gallons to the running total in total_gallons
Add miles to the running total in total_miles
Calculate and print the ratio miles_per_galon = miles/gallons in a separate
line
Prompt the user to enter 'y' for continue and 'n' for ending the program

}
calculate average = total_miles/total_gallons
write appropriate message to display average



Comments? E-mail me at aghafarian@ngcsu.edu

09/02/2005