0

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"?

Samuel Muldoon
  • 331
  • 2
  • 6
  • 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](https://cseducators.stackexchange.com/a/7669/9426), 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. – ggorlen May 31 '23 at 20:07
  • 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? – ggorlen May 31 '23 at 20:12

4 Answers4

4

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.

ctrl-alt-delor
  • 10,635
  • 4
  • 24
  • 54
2

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.

Steve
  • 356
  • 1
  • 6
1

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.
yeputons
  • 111
  • 2
0

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.

Samuel Muldoon
  • 331
  • 2
  • 6