An elephant in Cairo is a term used in
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
to describe a piece of data that matches the search criteria purposefully inserted at the end of a search space, in order to make sure the search algorithm terminates; it is a humorous example of a
sentinel value
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically ...
. The term derives from a humorous essay circulated on the
Internet
The Internet (or internet) is the Global network, global system of interconnected computer networks that uses the Internet protocol suite (TCP/IP) to communicate between networks and devices. It is a internetworking, network of networks ...
that was published in
''Byte'' magazine in September 1989, describing how various professions would go about hunting elephants.
Algorithm
When hunting elephants, the article describes programmers as following this algorithm:
:# Go to
Africa
Africa is the world's second-largest and second-most populous continent after Asia. At about 30.3 million km2 (11.7 million square miles) including adjacent islands, it covers 20% of Earth's land area and 6% of its total surfac ...
.
:# Start at the
Cape of Good Hope
The Cape of Good Hope ( ) is a rocky headland on the Atlantic Ocean, Atlantic coast of the Cape Peninsula in South Africa.
A List of common misconceptions#Geography, common misconception is that the Cape of Good Hope is the southern tip of Afri ...
.
[The ]Cape of Good Hope
The Cape of Good Hope ( ) is a rocky headland on the Atlantic Ocean, Atlantic coast of the Cape Peninsula in South Africa.
A List of common misconceptions#Geography, common misconception is that the Cape of Good Hope is the southern tip of Afri ...
has been traditionally believed to be Africa's southernmost point, but that is actually Cape Agulhas
Cape Agulhas (; , "Cape of Needles") is a rocky headland in Western Cape, South Africa. It is the geographic southern tip of Africa and the beginning of the traditional dividing line between the Atlantic and Indian oceans according to the In ...
''.
:# Work northward in an orderly manner, traversing the continent alternately east and west,
:# During each traverse pass:
:#:* Catch each animal seen.
:#:* Compare each animal caught to a known elephant.
:#:* Stop when a match is detected.
This algorithm has a bug, namely a
bounds checking
In computer programming, bounds checking is any method of detecting whether a variable is within some bounds before it is used. It is usually used to ensure that a number fits into a given type (range checking), or that a variable being used as ...
error: if no elephants are found, the programmer will continue northwards and end up in the Mediterranean sea, causing
abnormal termination by
drowning
Drowning is a type of Asphyxia, suffocation induced by the submersion of the mouth and nose in a liquid. Submersion injury refers to both drowning and near-miss incidents. Most instances of fatal drowning occur alone or in situations where othe ...
.
Thus experienced programmers modify the above algorithm by placing a known elephant in
Cairo
Cairo ( ; , ) is the Capital city, capital and largest city of Egypt and the Cairo Governorate, being home to more than 10 million people. It is also part of the List of urban agglomerations in Africa, largest urban agglomeration in Africa, L ...
to ensure that the algorithm will terminate.
The modified algorithm is therefore as follows:
:# Go to
Africa
Africa is the world's second-largest and second-most populous continent after Asia. At about 30.3 million km2 (11.7 million square miles) including adjacent islands, it covers 20% of Earth's land area and 6% of its total surfac ...
.
:# Put an elephant in
Cairo
Cairo ( ; , ) is the Capital city, capital and largest city of Egypt and the Cairo Governorate, being home to more than 10 million people. It is also part of the List of urban agglomerations in Africa, largest urban agglomeration in Africa, L ...
.
:# Start at the
Cape of Good Hope
The Cape of Good Hope ( ) is a rocky headland on the Atlantic Ocean, Atlantic coast of the Cape Peninsula in South Africa.
A List of common misconceptions#Geography, common misconception is that the Cape of Good Hope is the southern tip of Afri ...
.
:# Work northward in an orderly manner, traversing the continent alternately east and west,
:# During each traverse pass:
:#:* Catch each animal seen.
:#:* Compare each animal caught to a known elephant.
:#:* Stop when a match is detected.
:# If you are in Cairo, then there are no elephants in Africa (other than the one you placed there).
See also
*
Elephant test
Notes
{{reflist, group=notes
References
External links
List of the other elephant hunting techniques(PDF)
Computer humour
Metaphors referring to elephants