Computing.Net > Forums > Programming > Question about array off by 1 error

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Question about array off by 1 error

Reply to Message Icon

Name: jeremyzee
Date: January 9, 2007 at 10:17:05 Pacific
OS: WinXP Pro
CPU/Ram: Athlon 3500 / 1GB Mushkin
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: January 9, 2007 at 13:35:47 Pacific
Reply:

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


0

Response Number 2
Name: Guy
Date: January 9, 2007 at 17:32:39 Pacific
Reply:

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


0

Response Number 3
Name: Michael J (by mjdamato)
Date: January 10, 2007 at 02:13:19 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Question about array off by 1 error

Base to Hexadecimal www.computing.net/answers/programming/base-to-hexadecimal/7804.html

Question about Java Debug Interface www.computing.net/answers/programming/question-about-java-debug-interface/3072.html

Arrays in Flash www.computing.net/answers/programming/arrays-in-flash/15712.html