Erin's CS stuff

CS stuff!

Arrays Quick Guide

Application Example Notes
A declaration statement creates a composite variable to store a group of values. declaration
  • needs a data type, indentifier, and size between square brackets
  • the size between the square brackets must be of type int
  • best practices include using a macro for the size
A single array element is used to access individual values. element
  • needs a indentifier and index between square brackets
  • the index between the square brackets must be of type int and represents an offset from the beginning of the array
  • this expression should not include a data type
all array elements must be accessed one at a time elements
  • needs an for loop to repeat accessing each element
  • the index will usually start at 0 and continue until size - 1
Variable length arrays use an int variable as the size during declaration.

For example, the size can be determined during runtime if the user enters the number of items before the array needs to be declared
variable length array
  • as long as the size variable stores a valid int value, it can be used as the size in the array declaration but the order of these statements is very important!
  • the variable used as the size during declaration can also be used to control any for loops used to access all the elements in the array
Arrays of an arbitrarily large size can be used when we can't determine the number of objects before storing them in the array. arbitrarily large
  • the size during array declaration is considered the capacity
  • the actual number of useful values must also be saved in another variable and should be used to control any subsequent for loops