Next: Verifying the correctness of
Up: Algorithms
Previous: Describing an Algorithm
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:
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);