Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
We had a test in my AP Computer Science class, and the teacher and myself were debating on a question which I felt there were two equally right answers; however, she only felt one of them was correct. I was wondering if someone with fairly extensive programming experience could either point me to a source which infers my answer is correct, or at least puts their own input on the topic (whether it be for or against what I believe).
The question was, pertaining to J# arrays: "The off-by-one error associated with arrays arises because: "
The two answers up for debate are:
"The first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far""The last array element ends at length - 1 and loops may go one too far"
It seems to me like the two answers go hand-in-hand, and the second one is, not in any stretch of the imagination, wrong. I understand how arrays work and what the error is, and it seems to me as both answers are correct.
I know this seems like a pointless debate for an AP programming class, and we both agree on that. However, she still refuses to give me the points for the test, which is a large portion of the grade. This should have been an automatic correct question, but the ambiguity of the answers made it hard.
Thank you very much for any input on the topic.

Well, I don't know scratch about J#, but the issue you describe is relevant with many different programming languages.
The second answer is only partially correct because it describes the problem that can occure at the end of the array.
The first answer decribes both the problem that can occure at the beginning of an array as well as the problem that can occur at the end of an array.
It is not uncommon for a test to have two right answers for a question with one being more correct (or fully answering the question). Unless the parameters of the test were to give partial credit for partially correct answers, I would have to side with the instructor.
Plus, the "key" (no pun intended) to the array problem is that arrays start at 0. The fact that the last array element has an index of length-1 is just a byproduct of the 0 index.
Michael J

There are *very few* languages that start array indices at 1.
FORTRAN if I remember right. I cannot really think of another one.
Certainly not C, C++, Java, Ruby, .......... etc.
I think these days Basic allows you to say which base you are using.
And *almost* all Assembler indexing schemes will assume 0 is the start ('tho I do know of at least one exception to this part).
So .... any programmer who thinks counting starts at 1 is pretty inexperienced in my opinion.
As far as checking against going past the end of an array, clearly one can use either:
for(int i = 0;i < somearray.length;i++)
or
for(int i = 0;i <= somearray.length-1;i++)Or if you are going backwards through it (a pretty common task), either:
for(int i = somearray.length-1;i >= 0;i--)
or
for(int i = somearray.length-1;i > -1;i--)
I have seen variations on this, where inside the loop you do not use:
somearray[i]
but instead one of:
somearray[i+1]
somearray[i-1]....
:-)
You can tell your teacher that you talked to an old man with over 30 years programming experience, and he thinks it is a rookie mistake, not much worth discussing.
If you make it many times, you do not get past the rookie part.
Guy

Guy, I think you missed the point of the OPs post.
The question was not wether indicies start at 0 or how to program for them. The question was about two possible answers that were on an exam. The first answer was the one the instructor stated was correct and the 2nd one was the one the OP felt was also correct.
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |