Writing pseudocode to be language independent (original) (raw)
The rule means roughly that the statements and constructs should be clear and not be directly expressed in any actual programming language. For example, imagine we want to present a silly toy example for an algorithm that determines if the value 5 is present in an array. Perhaps you have never seen C++, but I still give you the following description:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 1, 3, 5, 7 };
auto iter = std::find(v.begin(), v.end(), 5);
if(iter != v.end())
std::cout << "found 5\n";
}
You might be confused and say "what include?", "what std::?", "what vector, find, auto, cout, <<, what is all this garbage?". And you would be absolutely right. My use of a concrete programming language is cluttering the presentation and obscuring what I really want to get across. So here's one possible pseudocode implementation of the above, in the spirit of the rule you mention:
A[1..4] = [1,3,5,7]
for i = 1 to 4:
if A[i] == 5:
print "found 5"
The logic behind the algorithm should be much clearer from this description to someone who has never seen C++ or some specific programming language I might have used.