Questions tagged [off-by-one]

A class of errors commonly made by programmers characterized by accessing arrays at an index, whose value is greater or lesser than the intended value by 1

An off-by-one error (OBOE) is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few. Usually this problem arises when a programmer fails to take into account that a sequence starts at zero rather than one (as with array indices in many languages), or makes mistakes such as using "is less than or equal to" where "is less than" should have been used in a comparison. This can also occur in a mathematical context.

[source]

51 questions
1
vote
2 answers

Avoiding Python Off-by-One Error in RLE Algorithm

EDIT: there's more wrong with this than just an off-by-one error, it seems. I've got an off-by-one error in the following simple algorithm which is supposed to display the count of letters in a string, along the lines of run-length encoding. I can…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
1
vote
1 answer

How is this gcc-generated strlen() mips loop not off-by-one?

Here is the source code for a very basic strlen() implementation. #include #include extern uintptr_t lx_syscall3(uintptr_t a, uintptr_t b, uintptr_t c, uintptr_t nr); static void lx_sys_exit(uintptr_t code) { …
Hypertable
  • 123
  • 8
1
vote
1 answer

star wars api => IndexError: list index out of range error

I am working with a star wars API from http://swapi.co/api/. I can connect to it just fine and my project is coming along fine. However, I am running into the following error message: IndexError: list index out of range error. Looking at other stack…
ravenUSMC
  • 495
  • 5
  • 23
1
vote
2 answers

First Byte's Bit Off-By-One after Steganography

Currently working on a Steganography project where, given a message in bytes and the number of bits to modify per byte, hide a message in an arbitrary byte array. In the first decoded byte of the resulting message, the value has it's first…
MrPublic
  • 520
  • 5
  • 16
1
vote
0 answers

Practice avoiding off-by-one errors?

I feel that I am quite bad at reasoning about boundary conditions in loops. I make a lot of mistakes and usually have off by one errors. Is there any way/site where i can practice problems related to corner cases/boundary conditions?
whatisinaname
  • 333
  • 4
  • 15
1
vote
1 answer

Loop not processing the last character of a string

Basically, the (Vigenere) decryption works perfectly except for not including the final letter for the decryption. For instance, the decryption for m_text yields 48 letters instead of 49. I even tried to manipulate the loop but it doesn't work out…
Devan
  • 13
  • 4
1
vote
2 answers

Off by one error in while loop with eof()

I have an off-by-one error in the following piece of C++ code and I can not figure it out. Can anybody please help. I have the code and its output below. Best. double* alloc_Array=new double[m_int_NumChann*m_int_NumSamples]; int…
Megidd
  • 7,089
  • 6
  • 65
  • 142
0
votes
0 answers

Google Apps Script code is targeting 1 row below where it needs to, therefore deleting rows that it shouldn't. How to fix this?

Code is pasted below. Summary: Search for the searchKey (PUT) in column P on “NWSS Sidecar” sheet When found, get the corresponding sample ID# from column E If the sample ID# is not paired up, (doesn’t have a PUT entry in the directly adjacent row…
0
votes
2 answers

Why does this reverse loop give me an error in Google Apps Script?

This reverse loop is giving me the error TypeError: Cannot read property '0' of undefined and I can't find out why. Here's the code part: function formatBoqPipework() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const boqPipeworkSheet =…
onit
  • 2,275
  • 11
  • 25
0
votes
1 answer

ascending order check recursively

What I try to do is to check a given array is ordered in an ascending manner by divide-and-conquer approach. I wonder what the logic behind the additional return case (a⌊n/2⌋−1 ≤ a⌊n/2⌋) is to reach to the final result. I attempted to solve the…
Hax
  • 133
  • 9
0
votes
2 answers

Count how many times an array element is larger than the subsequent element (off-by-one error)

I'm programming in C. I have to make a function called count , that counts how many times is larger than the subsequent element in the same array. For example, if we had a main code looking like this: int main() { int a1[] = { 5 }; int a2[]…
Carl
  • 149
  • 1
  • 7
0
votes
3 answers

Weird Error: Abort trap while handling character array in C

I want to store the binary value of each character in a string and store it in an array. But when i start messing around with functions like memset, i have no control over the debugging. #include #include int main() { char…
Maverickgugu
  • 767
  • 4
  • 13
  • 26
0
votes
1 answer

Freezing rows in Sheets with Google Apps throws type error

I'm trying to use GAS to freeze the top row of each sheet. It works, freezes the desired rows, but returns an error: "TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows") According to Google documentation, the…
0
votes
1 answer

How can I effectively troubleshoot off-by-one bug in C?

I've written a program to reverse a char array and also reverse words within that array. The program almost works as intended but I believe this is an off-by-one error. I've tried messing with the math involving the loop counters but have not been…
the_endian
  • 2,259
  • 1
  • 24
  • 49
0
votes
1 answer

How to fix off-by-one issue in for-loop

I'm given some variable values which I read from a file and I perform a running total calculation. My goal is to find out how many total calculations I've done. I can get the number right by subtracting 1 from my counter at the end but I would like…
Cornel
  • 180
  • 2
  • 4
  • 13