In my answer to the question Determine missing number in data stream I have a comment which indicates C++ is not a valid form of pseudo-code. Lacking a formal definition of what pseudo-code actually is I don't see how specified languages can be excluded.

C++ is defined in terms of a series of objects and operations working on an abstract machine. The meaning of the code is unambiguous. Indeed, the syntax of C, on which C++ is based, is often used as the basis of pseudo-code, so it can't be entirely incorrect to use such syntax.

The argument was made that somebody who doesn't know C++ wouldn't understand the answer. Is this not true of absolutely any language, even pseudo code? If I choose to answer in nothing but a series of set operations, one would have to be well versed in those to understand. If I chose to answer with any formal language you'd also have to be versed in that to understand. That is, regardless of how I answer the question, somebody would have to understand that language to be able to understand the answer.


4 Answers 4


The main argument, imho, is clutter: a good answer exposes the relevant knowledge in a clear way. You have posted 50 lines of code; the actual algorithm consists of five lines. That's 90% irrelevant stuff there. You even have to scroll to find the actual algorithm. All in all, that is clearly a bad way to present an algorithm (note: not "program").

Note that the essential five lines would be completely fine as (part of an) answer even though they are C++; there is nothing special going on there (the pointer arithmetic can be ignored safely). Maybe a note of what ^ does is in order, but that is mostly clear from context, even if the reader has never used a C-ish language.

Also, your original answer did not contain any justification for why the algorithm should work. "I tested it for some inputs" may be a valid answer on SO but not here (in most cases). A formal proof is not needed or warranted in many cases, but there should be some conclusive correctness argument. You have since edited that in, so that makes your answer a lot better.

Of course you can post whatever you like. The commenters try to help you to improve your answer; if you decide to ignore them, that is your choice.

By the way, I approve of algorithms that have been implemented and tested. After all, most formal proofs are high-level (see Tsuyoshi's answer) so some develish details may be wrong; bugs like this can be found in almost every textbook. Many a runtime analysis has failed because of some innocent-looking line of pseudo code. However, an answer is usually not the place to do that. Maybe post your implementation and test code on pastebin or Github and link there; that would certainly add value to an otherwise good answer.

  • 3
    $\begingroup$ I agree. 50 lines of code is way to much to express such a simple (yet subtle) idea. Boo C++. $\endgroup$ May 26, 2012 at 15:46

In the particular case to which you linked, honestly speaking, I do not see the point of posting the C++ code. HdM’s comment is clear enough and it is much easier to understand than the C++ code.

As a means to explain an algorithm in general, pseudocode is sometimes more useful than (say) C++ because we can avoid specifying unimportant details. For example, if the order of processing vertices in a graph does not matter, we can write

For each vertex v, …

instead of

For vertices v=1,…,n, …

and the former is better because it clarifies that order does not matter. Specifying unimportant details obscures the essence of the algorithm, and doing so just to use an existing programming language is probably not a good thing if our purpose is to communicate an algorithm.

(This example was taken from JɛffE’s answer to a question on cstheory.stackexchange.com.)

  • $\begingroup$ I'm not disagreeing that pseudo can sometimes be more useful, or sometimes clearer. But this wouldn't make C++ an invalid pseudo-code, simply less preferable at times. $\endgroup$ May 26, 2012 at 11:59
  • 2
    $\begingroup$ @edA-qa mort-ora-y: I am not interested in whether C++ is “valid” or “invalid” as pseudocode. (I do not know what “valid” or “invalid” means at all, but I do not care.) I am just saying that if the goal is to communicate an algorithm, using actual programming languages is probably not a good choice. $\endgroup$ May 26, 2012 at 12:02
  • $\begingroup$ @TsuyoshiIto: That said, some languages (and coding styles) do lend themselves to communication. C++ (maybe with micro-optimisations!) certainly is not. $\endgroup$
    – Raphael
    May 26, 2012 at 12:27

An answer to an algorithms' question should be understandable without knowledge about a particular programming language. If your code is clear enough that a person who doesn't know anything about that language can understand it then it is fine, otherwise it can be considered as not-answering-the-question.

Moreover an answer to a algorithms' question should not just give some code (even in pseudo-code), it should explain why the algorithm works (if it is not clear). Just posting the code of a program even when it seems to work is not helpful, I think authors here are asking for algorithms to understand them, not merely program code that seems to work on some inputs without any explanation on why it is solving the problem. That might be a suitable answer on a programming Q&A, it is probably not a good answer on a CS Q&A.

(If someone submits a code for a algorithms course question without any explanation about what each part is doing and how it solves the problem then they probably will get zero even if the program seems to work on a bunch of inputs.)


Nothing wrong with using C++ to communicate an algorithm. Other possibilities exist, sure, but in your answer, use whatever notation you like (within reason, I guess).

  • 1
    $\begingroup$ I strongly disagree. Algorithm descriptions should be as free of unnecessary detail as possible. The compiler might need it, but for communicating the underlying idea, it's just a distraction. $\endgroup$
    – JeffE
    May 29, 2012 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .