Next: Verifying the correctness of Up: Algorithms Previous: Describing an Algorithm


Statements required to describe algorithms

Very few statement types have been used in describing the above algorithm. In fact these few statement types are sufficient to describe all computer algorithms. In practice other forms of loop may be introduced but they can all be implemented using the while loop used in the previous section. The following basic concepts are required:

  1. The idea that statements are executed in the order they are written. Sequence.
  2. A construct that allows the grouping of several statements together so that they can be handled as if they were one statement by enclosing them in brackets. Compound Statement.
  3. A construct that allows the repetition of a statement or compound statement several times. Repetition.
  4. A construct that allows the selection of the next statement to execute depending on the value of a condition. Selection.
  5. The ability to assign a value to a quantity. Assignment.

All high-level programming languages have equivalents of such constructions and hence it would be very easy to translate the above algorithm into a computer program. In fact each statement of this algorithm can be written as a statement in the language C++.

If you compare the following program written in C++ with the algorithm you should be able to spot the similarities. Note that the program as written is not complete, it would require further declarations and definitions to be added before the compiler would accept it as syntactically correct. What is listed here are the executable statements of the program.

ins.open(infile);
total = 0;
count = 0;
while (!ins.eof())
  {
    ins >> number;
    total = total + number;
    count = count + 1;
  }
if (count == 0)
  {
    cout << "No input" << endl;
  }
else
  {
    average = total/count;
    cout << "Average is "
         << average
         << endl;
  }
ins.close(infile);



Next: Verifying the correctness of Up: Algorithms Previous: Describing an Algorithm