C++ - Array PDF
C++ - Array PDF
INTRODUCTION
• An array enables the programmer to use a
single variable to store many values.
• The values are stored at consecutive indexes,
starting with zero and then incrementing by
one for each additional element of the array.
• Using 1 array variable to store many values
has many advantages over having to declare
separate variables that can hold only 1 value
each.
1
2/9/2011
an array of variables begins with the data type • The purpose of the size declarator is to tell the
followed by a variable name and ending with a computer how much memory to reserve.
semicolon.
2
2/9/2011
3
2/9/2011
4
2/9/2011
5
2/9/2011
INITIALIZATION INITIALIZATION
– Explicit array sizing • Explicit Array Sizing
• In which the square brackets contains a numerical int testScore[3] = {74, 87,92};
constant that explicitly specifies the size of the array. float milesPerGallon[4] = {44.4, 22.3, 11.6, 33.3};
– Implicit array sizing char grades[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’};
string days[7] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”,
• In which the square brackets are empty and the size of
“Thursday”, “Friday”, “Saturday”};
the array is indicated implicitly by the number of
elements on the right side of the assignment operator.