|
|
|
|
|
Chapter 9 : Vectors and Arrays |
|
9.7 Arrays |
|
Recitation handout 7 |
|
Individual project handout |
|
|
|
|
|
|
|
|
|
|
|
25th, Feb : recitation 7 |
|
lab: individual project |
|
3rd, March: recitation 8 |
|
lab: array lab |
|
6th, March : individual project due |
|
10th, March: recitation 9 |
|
lab: no new lab |
|
16th, March: 8:00 Am Final Exam |
|
39 Snell Library |
|
|
|
|
|
|
|
|
Array |
|
How to declare an array |
|
How to initialize an array |
|
How to refer to individual elements in an array |
|
|
|
|
|
|
|
|
|
|
Frequently, we may have a set of values (data),
all of the same data type, that forms a logic group. |
|
One –dimensional array: a simple list containing
individual items of the same data type |
|
|
|
The items in the list can be declared as a
single unit and stored under a common variable name called the array name. |
|
|
|
|
Array variable declaration |
|
data_type
array_name[max_size]; |
|
|
|
Examples |
|
double temp[5]; // a floating-points
array |
|
char codes[4]; // an character array |
|
int grades[6]; // an integer array |
|
|
|
|
Good programming practice requires the number of
array |
|
items be defined as a constant before declaring
the array. |
|
Thus, the previous array declaration would be
declared using |
|
two statements, such as: |
|
const int NUMBER = 5; |
|
double temp[NUMBER]; |
|
|
|
const int ARRAYSIZE = 4; |
|
char codes[ARRAYSIZE]; |
|
|
|
const int MAXSIZE = 6; |
|
int grades[MAXSIZE]; |
|
|
|
|
In these declaration statements, each array is
allocated |
|
sufficient memory to hold the number of data items given in the
declaration statement. |
|
Each item in an array is called an element. The
individual |
|
elements are stored sequentially in the array. |
|
|
|
|
|
|
|
|
|
Any individual element can be accessed by giving
the name of the array and the element’s position. |
|
This position is called the element’s index or
subscript value. |
|
For a single-dimensional array, the first
element has an index of 0 |
|
The index or subscript value gives the position
of the element in the array. |
|
temp[0] is read as “ temp sub zero” |
|
temp[1] is read as “ temp sub 1” |
|
|
|
|
The subscript contained within brackets need not
to be an constant integer |
|
Any expression that evaluates to an integer may
be used as a subscripts. |
|
|
|
Examples: |
|
int i = 2; |
|
temp[i]; // the same as temp[2] |
|
temp[ i * 2]; // the same as temp[4] |
|
|
|
|
|
|
|
One cannot change the size of a given array |
|
|
|
Some C++ complier do not check the value of the
indexing being used (called a bounds check) |
|
Compile time : no error messages |
|
Run time: crash or error |
|
Visual C++ complier does the bound check, but
only give you some warning messages, not error messages. |
|
Example |
|
int temp[5]; // declare an integer array
with 5 elements |
|
temp[2] = 5; // valid |
|
temp[5] = 3; // wrong, out of bound |
|
|
|
|
|
|
|
|
|
|
Array can be declared and initialized at the
same time. |
|
Examples |
|
int grades[6] = { 98, 87, 63, 74, 86, 76}; |
|
Size is 6 |
|
char codes[4] = { ‘F’, ‘S’, ‘C’, ’D’}; |
|
Size is 4 |
|
double temp[4] ={ 10.9, 54.8, 34.5, 42.1}; |
|
Size is 4 |
|
|
|
|
|
|
|
|
|
If the number of initializers is less than the
declared number of elements listed in square brackets, the initializers are
applied starting with array element 0. |
|
For example, |
|
int grades[4] = { 98, 87 }; |
|
grade[0] will be 98 |
|
grade[1] will be 87 |
|
The other array elements will be initialized to
0. |
|
grade[2] will be 0 |
|
grade[3] will be 0 |
|
|
|
|
|
|
|
A unique feature of initializers is that the
size of an array may be omitted when initializing values are included in
the declaration statement. |
|
And the actual size of the array will depend on
how many elements in the { }. |
|
int grades[] = {98, 87, 63, 74, 86, 76}; |
|
Actual size will be 6 |
|
The complier will reserves enough storage space
for 6 elements. |
|
|
|
|
|
|
|
|
|
|
If I want to declare a character array and
initialize it, there are normally three ways: |
|
char codes[6] = {‘s’, ’a’, ’m’, ’p’, ’l’, ’e’} |
|
Size is 6 |
|
char codes[]
= {‘s’, ’a’, ’m’, ’p’, ’l’, ’e’} |
|
Size is 6 |
|
char codes[] = “sample”; //no braces or
commas |
|
Size is 7 |
|
We can use the string “sample” to initialize the
codes array. This declaration created an array named codes having 7
elements. The last one is \0, also called null character. The null
character is automatically appended to all strings by the C++ compiler. |
|
|
|
|
|
|
char codes[] ={'s','a','m','p','l','e'}; |
|
cout
<< codes[5] << endl; // valid |
|
cout
<< codes[6] << endl; |
|
// warning C4700: local variable 'codes'
used without |
|
// having been initialized |
|
// In other words, codes[6] is out of
bound |
|
char codes1[] ="sample"; |
|
cout
<< codes1[5] << endl; // valid |
|
cout
<< codes1[6] << endl; // valid |
|
|
|