-1
$\begingroup$

Arrays start from 0 then 1,2... But why are indexes positive, why are indexes not negative?

$\endgroup$

closed as off-topic by Gypsy Spellweaver, ncmathsadist, ItamarG3, Ben I. Oct 28 '18 at 18:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Computer Science education, within the scope defined in the help center." – Gypsy Spellweaver, ncmathsadist, ItamarG3, Ben I.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ In some languages you can index of lots of thing, including a range of negatives or an enumeration type. $\endgroup$ – Buffy Oct 27 '18 at 0:44
  • $\begingroup$ Okk but I try to gave negavtive index in c++ then its show error.is this an other way to gave negative index ?? $\endgroup$ – Iram Shah Oct 27 '18 at 3:35
  • $\begingroup$ What are you trying to achieve with the negative index? $\endgroup$ – OBu Oct 27 '18 at 18:43
  • $\begingroup$ I am a student and all teachers tell us only about positive index in array. So there is a question in my mind that is it possible to gave negative index if yes the. How is it possible $\endgroup$ – Iram Shah Oct 27 '18 at 20:21
  • 1
    $\begingroup$ Hi @IramShah, welcome to Computer Science Educators! I'm afraid that I had to close this question, as it is off-topic for the site. CSEd is mostly for teachers, and it is about how to teach CS concepts. So a question about classroom practice would be topical here. However, I see that you already received a good answer, so I hope that you also got the help that you need! $\endgroup$ – Ben I. Oct 28 '18 at 18:22
3
$\begingroup$

You are starting with a base memory adress and you are adding an offset having the size of one array element for each following element. This makes adress calculation very easy (base adress + n * size of data structure).

Negative indexes are not allowed because you would refer to the memory adress in front of your base adress, which means you might destroy other variables content.

$\endgroup$
  • $\begingroup$ Re, "...this makes address calculation very easy..." That is a reason why arrays can be more efficiently implemented if the range of allowed indices is contiguous, but it does not explain why that range should not include any negative numbers. $\endgroup$ – Solomon Slow Oct 29 '18 at 19:51
  • $\begingroup$ Re, "...you might destroy other variable's content." That is a reason why you should not use out-of-bounds array indices, but it does not explain why negative numbers should be out-of-bounds. $\endgroup$ – Solomon Slow Oct 29 '18 at 19:51
  • $\begingroup$ There is no strong reason why negative numbers typically are not allowed. Probably the best reason is, Many languages, for simplicity's sake, only allow the programmer to specify the size of an array, rather than allowing the programmer to specify a starting and ending index. If you want to choose one starting index that will make everybody happy, there are not very many reasonable choices. Some programmers will grumble if you choose 0. Some will grumble if you choose 1. Everybody will grumble (and, nobody will use your language) if you choose any other number. $\endgroup$ – Solomon Slow Oct 29 '18 at 20:01
  • $\begingroup$ Consider "The principle of least surprise". Or, minimum grumbling. Maybe negative indices would refer to imaginary data? Probably appropriate for quantum computing. $\endgroup$ – Scott Rowe Oct 31 '18 at 12:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.