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
votes
1 answer

Please, can some one tell me what's wrong with this code?

Please, can some one tell me what's wrong with this code? #include using namespace std; int main() { int a[3], i; for(i = 0; i <= 3; i++ ) { a[i] = 0; cout << "i = " << i << endl; } …
-1
votes
1 answer

Are these two loops equivalent: off-by-one

Are these two loops the same? For some reason the second loop is off-by-one and I cannot figure out why. while ( !b && ++n < WORD_COUNT ) b = mWords[n]; n++; while ( !b && n < WORD_COUNT ) { b = mWords[n]; n++; }
Megidd
  • 7,089
  • 6
  • 65
  • 142
-1
votes
5 answers

JAVA: Error reversing a string to check for palaindrome off-by-one i think

I am having a problem. I received an assignment to write pseudo code for a palindrome checking program. My problem is that while I received good marks on my pseudocode assignment, when I tried to write the code in java for my own edification, I was…
AllWillB1
  • 13
  • 3
-1
votes
1 answer

Quick Sort Error, Possibly an off by 1 C++

Trying to write a quick sort and have spent awhile debugging. The problem seems to be in the second recursive call, but I can't figure out where I'm off. Any pointer would be awesome. Thanks. void quickSort(vector &list, int left, int right){ …
Donovan King
  • 855
  • 1
  • 6
  • 18
-2
votes
2 answers

Abort trap in C without any understandable reason (for me ;))

The following code compiles without any problem with the command 'gcc -lm': /* The code was considerable shortened for a clearer understanding */ #include #include #include #include #include…
Benjamin
  • 29
  • 2
-5
votes
3 answers

Age computation always off by one

The task is to create a function. The function takes two arguments: current father's age (years) current age of his son (years) Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as…
Nikolai
  • 3
  • 1
  • 4
1 2 3
4