What is an array?:
- collection of data items with that represent the same thing (e.g. test scores of a class, data measurements from an experiment, balances of bank accounts, etc.)
- it could be a collection of
int
's,float
's,char
's, etc. - you can think of arrays like PO Boxes
Why are arrays important?:
- arrays allow you to group data items that are related
- arrays allow you to add new data items to the group easily
- using arrays allow your code to be flexible when treating variables that represent similar things
How to picture an array?:
-
Honolulu Post Office
honolulu_po_box
PO Box 1 at Honolulu Post Officehonolulu_po_box[0]
PO Box 2 at Honolulu Post Officehonolulu_po_box[1]
PO Box 3 at Honolulu Post Officehonolulu_po_box[2]
Array Analogy Real World Programming World Honolulu Post Office honolulu_po_box
PO Box 1 at Honolulu Post Office honolulu_po_box[0]
PO Box 2 at Honolulu Post Office honolulu_po_box[1]
PO Box 3 at Honolulu Post Office honolulu_po_box[2]
Array Statement Analogy Real World Programming World My package is somewhere inside the Honolulu Post Office My data is stored in the honolulu_po_box
arrayMy parckage is in PO Box 1 at Honolulu Post Office My data is stored in the first array element of the honolulu_po_box
array,honolulu_po_box[0]
My parckage is in PO Box 2 at Honolulu Post Office My data is stored in the second array element of the honolulu_po_box
array,honolulu_po_box[1]
My parckage is in PO Box 3 at Honolulu Post Office My data is stored in the third array element of the honolulu_po_box
array,honolulu_po_box[2]
All the po boxes at the Honolulu Post Office can only hold 4 inch x 4 inch x 4 inch boxes The honolulu_po_box
array can only holdint
's,int honolulu_po_box[3]
The Honolulu Post Office has 3 PO Boxes available The honolulu_po_box
array has 3 array elements,int honolulu_po_box[3]