Arrays
Our first look at structured data.
Simple types: int, char, float, bool.
Composite types: more complex arrangements of simple types.
Recall assignment 7 with 3 scores
float score1, score2, score3; //to declare
...
cin >> score1 >> score2 >> score3 //to read
...
sum = score1 + score2 + score3 //to add
...
cout << score1 << score2 << score3 //to read
...
??? //put in increasing order
What if there were 5 scores? 10?
We need a way to handle pieces of related data.
Arrays:
a structured data type consisting of a collection of memory cells for
storing a list of values that are all the same type.
Later, records: a data structure for storing related elements of different types.
Examples:
- Sayings at the bottom of the page
- Sudoku
Declaration:
int score[5];
0 1
  2
3 4
To access the elements:
score[3] = 7;
cout << score[2];
Out of bound indices:
score[5]= 8: // uses the next space
Arrays as paramters:
Passing by value would require copying over the entire array.
Too expensive.
Default: arrays are passed by reference.
Therefore omit the &.
13.2 Shortcomings of Arrays
1. No range checking
2. No good way to dynamically resize an array
3. Inconsistent syntax
One approach to handle this: a built-in class called vector
(Sections 13.3-13.8).
We will not follow this. See class examples for alternate material.
Comment: an array subscript cannot be a character.