0
$\begingroup$

A lot of teaching examples of off-by-one errors which educators provide to their students involve for-loops.

For example, we might have the following for-loop:

// Array `A` contains `50` names indexed from `0` to `49`  
//
//     0 ... Orquil Bolouri
//     1 ... Manuel Tobellon
//     2 ... Kabatha Dowling
//     3 ... Lornton Krowley

Toni Toson
// `numel(A)` returns the number of elements in array `A`   
// `numel(A)` represents the number `50` in this context 

for k from 0 to numel(A) {
    first_name = get_first_name(A); 
    print(first_name);  
}    

What are examples of off-by-one Error in the "Real World"?

$\endgroup$
2
  • $\begingroup$ You've asked a variety of "how do I teach X using a real-world example?" questions here. Why are you so sure you need to use so-called "real-world examples" to teach these concepts? Do we need to have a separate thread for collecting lists of "real-world" examples for each and every CS concept? As discussed at length in this thread, I suggest distinguishing "physical world" from "practical computing". Many physical world examples are not very practical metaphors in computing, even if students are familiar with them IRL. $\endgroup$
    – ggorlen
    Commented May 31, 2023 at 20:07
  • $\begingroup$ Also, since off-by-one errors generally manifest in loops and arrays, what is your motivation for needing an example outside of this domain, which represents the vast majority of the off-by-one errors students will encounter in programming? $\endgroup$
    – ggorlen
    Commented May 31, 2023 at 20:12

4 Answers 4

5
$\begingroup$

Probably the most common out by one error is the fence-post error.

In a linear fence, the number of posts is one more than the number of panels. It is important to know whether you are counting fence-posts or panels. And, to know which quantity you want to know. Then you can adjust.

You can build a lesson around this concept, before bringing it back to programming.

$\endgroup$
2
$\begingroup$

I'm unclear if "real world" means a programming or non-programming context, but an infamous real-world example of this was following the introduction of the Julian calendar, by Julius Caesar in 46 BC.

There was widespread misunderstanding about the then-new 4-year cycle of the leap day, and it was instead observed every 3 years in the pattern of 1-2-3-leap-2-3-leap-2-3-leap.

This practice today is called "inclusive counting", where the last year of one cycle is also considered to be first year of the next cycle, and was a more common practice at the time.

$\endgroup$
2
$\begingroup$

Basically anything involving intervals, discrete and continuous.

What is the distance between 1 and 10? 10 - 1 = 9, but:

  • If these are kilometer posts, it's 9 kilometers along the road.
  • If these are buildings, there are 8 buildings between buildings 1 and 10.
$\endgroup$
1
  • $\begingroup$ So there are b-a+1 elements in integer interval [a,b]; there are b-a-1 elements in integer interval (a,b); there are b-a elements in integer interval [a,b). Programming language python decided that basic language constructs would always be expressed using intervals of form [a,b) because it makes it easier to avoid off-by-one errors. For instance, an array a has "length" len(a), meaning that its number of elements is len(a), and its elements are indexed by integer interval [0, len(a)); and if you write for i in range(0, len(a)) you are iterating on this interval. $\endgroup$
    – Stef
    Commented Mar 18 at 13:30
0
$\begingroup$

This is a community-editable wiki. Feel free to edit it.


Example of Three Languages and Only Two Compilers

Suppose that someone is writing code for a text-adventure video-game where they want to translate a large number of simple sentences from natural language into python, with an intermediate language in between.

You might assume that there are 3 compilers for 3 languages, but there are one 2 compilers.

  1. A language of short simple (not complex or compound) alpha-numeric German sentences.

    • kombinieren Mehl, Wasser, Basilikum und Knoblauch
  2. A middle language of highly structured German verbs and nouns with order of operations specified with parentheses, square-brackets, curly-braces, etc...

    • kombinieren(Mehl, Wasser, Basilikum, Knoblauch)
  3. python.


Example of Shelves for a Closet Full of Shoes

Imagine a large-household where rooms are given to guests or rented out to tenants.

The number of shelves is different than the number of shelf-spaces on which shoes can be placed.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.