170-181

## Array

• An array is a collection of data of same type.
• It can be used for list with like items. examples: test scores, temperatures, names, etc.
• avoids declaring multiple simple variables
• can manipulates 'list' as one entity
• Example, you can do something like
• Do this to i-th indexed variable
• display all elements of an array
• fill elements of array score from user
Example
 `#include using namespace std;// the program asks user to input 5 numbers// then output the averageint main() { // declare an array with 5 numbers // sum is the total sum of all the scores double score, sum = 0; cout << "Please input 5 numbers. " << "Seperate the numbers by spaces: \n"; int i; for(i = 0; i < 5; i++) { cin >> score[i]; //ask the user to input the i-th score sum+=score[i]; } // magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The scores are : \n"; for(i = 0; i < 5; i++) { cout << score[i] << " "; } cout << endl; cout << "The average is " << sum/5 <
Output
 `Please input 5 numbers. Seperate the numbers by spaces:92.25 33 45.75 88.8 77.77The scores are :92.25 33.00 45.75 88.80 77.77The average is 67.51`

 `score` `score` `score` `score` `score` 92.25 33 45.75 88.8 77.77
Explanation of the program
• `double score` declares an array of 5 doubles names scores
• `score[i]` is the i-th element.
Array consists of 2 parts
• name of the array
• size of the array
Syntax
 Declare an array Type arrayName[size]; size should be a number or a const variable. It shouldn't be other type of variables.
Example:
• `int year;`
`int` is called the base type of the array, `50` is the size. `year` is the name.
• You can declare arrays and regular variables together. example:
`double taxRate, income;`
The order doesn't matter.
• You can use constant to declare an array size.  `#include using namespace std;// the program asks user to input 5 numbers// then output the averageint main() { const int NUM_OF_STUDENTS = 5; // declare an array with NUM_OF_STUDENTS numbers // sum is the total sum of all the scores double score[NUM_OF_STUDENTS], sum = 0; cout << "Please input "<< NUM_OF_STUDENTS << " numbers. " << "Seperate the numbers by spaces: \n"; int i; for(i = 0; i < NUM_OF_STUDENTS; i++) { cin >> score[i]; //ask the user to input the i-th score sum+=score[i]; } // magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The scores are : \n"; for(i = 0; i < NUM_OF_STUDENTS; i++) { cout << score[i] << " "; } cout << endl; cout << "The average is " << sum/NUM_OF_STUDENTS <
• You can easily change the number of students by
`const int NUM_OF_STUDENTS = 100;`
• You can't use variables to declare an array size.
Compiling error  `#include using namespace std;int main() { int numOfStudents; cout << "Please enter the number of students: "; cin >> numOfStudents; double score[numOfStudents], sum = 0; return 0;}`
Access elements in an array
Individual parts called many things
• Indexed or subscripted variables
• Elements of the array
• Value in brackets called index or subscript, it is a number from 0 to size-1
• `score,score,score,score,score` represent the first, second, third, forth and fifth element in the array `score`
• Array indexes always start with zero and ends with the size-1
• you can use `score[i]` as variables. i.e. you can use it in assignments, arithmetic expression etc etc
• You can put any arithmetic expression inside the bracket, like `score[i]`, or `income[n+1]`.
Note the 2 uses of brakets []
• In declaration, specifies SIZE of array
• Anywhere else, specifies a subscript
Natural counting loop
Naturally works well ‘counting thru’ elements of an array.
 ` for(i = 0; i < NUM_OF_STUDENTS; i++) { cin >> score[i]; //ask the user to input the i-th score sum+=score[i]; }`
Major array pitfall
• Array indexes always start with zero!
• C++ will ‘let’ you go beyond range
• Unpredictable results
• Compiler will not detect these errors!
• Indexes range from 0 to size – 1 Example:
`double temperature; // 24 is array size`
Declares array of 24 double values called `temperature`
They are indexed as:
`temperature, temperature … temperature`
Common mistake
`temperature = 5;`
• Index 24 is ‘out of range’!
• No warning, possibly disastrous results
 ```#includeusing namespace std;int main(){ int temperature; int i; for(i = 0; i < 24; i++) { temperature[i] = i + 60; } cout << temperature << endl; return 0; } ```
Output on my PC
The output is a meaningless number  `1245120`

### Arrays in memory

Refer to the book, p178, Display 5.2

### Initializing array

Array can be initialized like this
 `int children = {2, 12, 1};`
It is equivalent to the following code
 `int children;children=2;children=12;children=1;`
• if you list fewer values than there are indexed variables, those values will be used to initalize the first few indexed variables.
• The remaining indexed variables will be initialized to a zero of the array base type.
If you initialize an array when it is declared, you can omit the size of the array, and the array will automatically be declared to have the minmum size needed for the initialization values.
 `int b[] = {5, 12, 11};`
is equivalent to
 `int b = {5, 12, 11};`
Example
 `#includeusing namespace std;int main(){ int array = {1,3,5}; int i; for(i = 0; i < 5; i++) { cout << array[i] << " "; } cout << endl; return 0;}`
Output
 1 3 5 0 0