6.12. Arrays

Do not go outside of either physical or logical array bounds [Array bound: 1-20 points]

Do not use an array index that is outside of the array bounds. If only a prefix or suffix of an array has relevant information in it, only look at the relevant information. For example, if you have created an array of physical size 100, but you have read information into the first 30 slots, then do not do a loop over all 100 slots just to see those 30 values.

Use a named constant for a fixed array size [Array size: 1-2 points]

If you need to choose an arbitrary fixed array size, the size must be a named constant. For example, somewhere near the beginning of the program, define
  const int maxNumberOfWidgets = 200;
Then, when you create an array of widgets, say
  widget widgetArray[maxNumberOfWidgets];
Never write 200 where you should use maxNumberOfWidgets. The idea is that you should be able to change 200 to 500 in the definition of maxNumberOfWidgets and have the program work with the new constant, without changing the program in any other places.

Do not overwork a constant. If you need an array of bolts, do not make its size be maxNumberOfWidgets. Create another constant, such as maxNumberOfBolts.

Only use a constant array size when that seems necessary. If you know that you need an array of size n, then create an array of size n, not an array of size maxNumberOfWidgets.


An array is too large [Array too large: 1-2 points]

Do not allocate an array that is much larger than you need, unless you have a good reason for doing that. (If you allocate extra room because you expect the number of things stored in the array to grow, then that is a good reason.)

An array is unnecessarily passed by reference [Reference array: 1 point]

If you pass an array by value, it is really being passed by pointer. So you have a pointer to the array, and you can change the array.

Only pass an array by reference if you intend to make the pointer point to a different array.