Chapter Two
Chapter Two
Chapter two
2. Arrays
An array is a sequence of objects all of which have the same type. The objects are called
the elements of the array and are numbered consecutively 0, 1, 2, 3, ... . These numbers are called
index values or subscripts of the array. The term “subscript” is used because as a mathematical
sequence, an array would be written with subscripts: a 0, a1, a2, …. The subscripts locate the
element’s position within the array, thereby giving direct access into the array.
If the name of the array is a, then a[0] is the name of the element that is in position 0, a[1]
is the name of the element that is in position 1, etc. In general, the ith element is in position i–1.
So if the array has n elements, their names are a[0], a[1], a[2], …, a[n-1].
Virtually all useful programs use arrays. If several objects of the same type are to be used
in the same way, it is usually simpler to encapsulate them into an array.
Arrays are usually processed using for loops. Look at the following C++ program.
int main(){
double a[3];
a[0] = 11.11;
a[1] = 33.33;
a[2] = 55.55;
}
The first line declares a to be an array of 3 elements of type double. The next three lines assign
values to those elements. Look how an array is accessed using for loop.
1|Page
Chapter two Data Structures and Algorithm Analysis
int main(){
const int SIZE=5; // defines the size N for 5 elements
double a[SIZE]; // declares the array’s elements as type
double
cout << "Enter " << SIZE << " numbers:\t";
for (int i=0; i<SIZE; i++)
cin >> a[i];
cout << "In reverse order: ";
for (int i=SIZE-1; i>=0; i--)
cout << "\t" << a[i];
}
The first line defines the symbolic constant SIZE to be 5 elements. The second line
declares a to be an array of 5 elements of type double. Then the first for loop reads 5 values into
the array, and the second for loop prints them in reverse order.
This program initializes the array a and then prints its values:
int main(){
float a[] = { 22.2,44.4,66.6 };
int size = sizeof(a)/sizeof(float);
for (int i=0; i<size; i++)
cout << "\ta[" << i << "]="<< a[i] << endl;
}
a[0] = 22.2
a[1] = 44.4
a[2] = 66.6
the complete array occupies 12 bytes in memory. Therefore, the value of size is computed to be
12/4 = 3.
An array can be “zeroed out” by declaring it with an initializer list together with an
explicit size value, like this:
This array is declared to have 7 elements of type float; then its initializer list initializes
the first 3 elements with the given values and the remaining 4 elements with the value 0.
In addition to the predefined types such as int and char, C++ allows you to define your
own special data types. This can be done in several ways, the most powerful of which use classes
as described in Chapter 11. We consider here a much simpler kind of user-defined type.
An enumeration type is an integral type that is defined by the user with the syntax
Here enum is a C++ keyword, typename stands for an identifier that names the type being
defined, and enumerator-list stands for a list of names for integer constants. For example, the
following defines the enumeration type Semester, specifying the three possible values that a
variable of that type can have
and we can use those variables and those type values as we would with predefined types:
s1 = SPRING;
s2 = FALL;
if (s1 == s2) cout << "Same semester." << endl;
The actual values defined in the enumerator-list are called enumerators. In fact, they are
ordinary integer constants. For example, the enumerators FALL, SPRING, and SUMMER that
are defined for the Semester type above could have been defined like this:
3|Page
Chapter two Data Structures and Algorithm Analysis
The values 0, 1, … are assigned automatically when the type is defined. These default values can
be overridden in the enumerator-list:
If integer values are assigned to only some of the enumerators, then the ones that follow are
given consecutive values. For example,
Since enumerators are simply integer constants, it is legal to have several different enumerators
with the same value:
int answer;
cin >> answer;
:
if (answer == YES) cout << "You said it was o.k." << endl;
to work as expected. If the value of the variable answer is 1, then the condition will be true and
the output will occur. Note that since the integer value 1 always means “true” in a condition, this
selection statement could also be written
int main()
{ enum Day { SUN,MON,TUE,WED,THU,FRI,SAT };
float high[SAT+1] = {88.3,95.0,91.2,89.9,91.4,92.5,86.7};
for (int day = SUN; day <= SAT; day++)
cout << "The high temperature for day " << day
<< " was " << high[day] << endl;
}
4|Page
Chapter two Data Structures and Algorithm Analysis
Enumeration types are one way for programmers to define their own types. For example,
enum Color { RED,ORANGE,YELLOW,GREEN,BLUE,VIOLET };
defines the type Color which can then be used to declare variables like this:
Here, shirt is a variable whose value can be any one of the 6 values of the type Color and is
initialized to have the value BLUE, car is an array of 4 such Color type variables indexed from 0
to 3, and wavelength is an array of 6 float type variables indexed from RED to VIOLET.
Test run
int main(){
enum Color { RED,ORANGE,YELLOW,GREEN,BLUE,VIOLET };
Color shirt = VIOLET;
Color car[] = { GREEN,RED,BLUE,RED };
float wavelength[VIOLET+1] = {420,480,530,570,600,620};
cout<<"\nshirt = "<<shirt;
cout<<"\nwavelength[1] = "<<wavelength[5];
}
Output
Shirt = 5
Wavelength[1] = 620
C++ also provides a way to rename existing types. The keyword typedef declares a new name
(i.e., a synonym or alias) for a specified type. The syntax is typedef type alias; where type is the
given type and alias is the new name. For example, if you are used to programming in Pascal,
you might want to use these type aliases:
5|Page
Chapter two Data Structures and Algorithm Analysis
It shows that the number of elements in an array is not part of its type. Look at the following
example.
void main(){
typedef int IntegerArray[];
IntegerArray a = {1,2,3};
Output
1,2,3
In the above example the first line make an alias IntegerArray for int data type. Now this alias
can declare an array of type int just because of the [] after its name.
6|Page