CSCI 1110 - C++ Lecture Notes
Chapter 11 - One-Dimensional Arrays
Structured Data
Types
-
Simple data types
consist of a single value. Examples of simple C++ data types are char,
int, float, double, etc.
-
A Structured
Data Type is a collection of components whose
organization is characterized by the method used to access individual components.
-
The allowable operations on a structured
data type include the storage and retrieval of individual components.
One-Dimensional
Arrays
-
If we wanted to read in a list of 1000
values and print them in reverse order, we could write a program of this
form:
// Reverse list program
#include <iostream.h>
int main()
{
int value0;
int value1;
. . .
int value999;
cin >>
value0;
cin >>
value1;
. . .
cin >>
value999;
cout <<
value999;
cout <<
value998;
. . .
cout <<
value0;
return
0;
}
This program is over 3000 lines long and contains 3000
variables which have the same name (value)
with a numeric suffix.
-
A one-dimensional array provides a mechanism for keeping
track of a number of values which share the same name. Individual items
in the array can be referenced by an integeral value called an index.
-
All members of an array must be of the same data type.
-
To declare an array variable, specify the data type of members
of the array, the name of the array, and the number of elements enclosed
in square brackets:
int value[1000];
This reserves memory for a 1000 element array of ints
that is called value.
-
To reference individual members of an array, use the name
of the array followed by the index of the element enclosed in square brackets.
The index can be an integral constant, variable, or expression.
cout << value[0]; // output
first value
value[999] = value[0];
value[i] = value[j];
value[i] = value[i - 1];
-
Reverse program done with arrays:
// Reverse list program
#include <iostream.h>
int main()
{
int value[1000];
int number;
for (number
= 0; number < 1000; number++)
cin >> value[i];
for (number
= 999; number >= 0; number--)
cout << value[i];
return
0;
}
-
A one-dimensional array
is a structured collection of components, all of the same type, that is
given a single name. Each component (array element) is accessed by an integral
index that indicates the component's position within the collection.
-
C++ does not check to make sure that the index expression
is valid. An out-of-bounds array index is
either less than zero or greater than the array size minus one.
float alpha[100];
int i;
for (i = 0; i < 100; i++)
// valid
alpha[i]
= 0.0;
for (i = 0; i <= 99; i++)
// valid
alpha[i]
= 0.0;
for (i = 0; i <= 100; i++)
// not valid
alpha[i]
= 0.0;
for (i = 99; i >= 0; i--)
// not valid
alpha[i]
= alpha[i + 1];
Initializing
Arrays in Delcarations
-
To initialize the elements of an array when it is declared,
follow the array declaration with the assignment operator followed by a
list of data values seperated by commas and enclosed in braces:
int age[5] = {23, 10, 45, 14,
92};
char name[4] = {'T', 'o', 'm',
'\0'};
float temp[3] = {98.6, 1.4E2,
-27.8};
-
C++ will determine the size of a array automatically if you
specify it initial values:
int age[] = {23, 10, 45, 14,
92};
char name[] = {'T', 'o', 'm',
'\0'};
float temp[] = {98.6, 1.4E2, -27.8};
-
An array of characters can also be initialized by using a
string of characters cnclosed in double quotes:
char name[] = "Tom";
-
Note that name
contains four characters, including the '\0'
character in position name[3].
Aggregate
Array Operations
-
An Aggregate Operation is an
operation on a data structure as a whole, as opposed to an operation on
an individual component of the data structure.
-
Except for a few operations on arrays of characters (strings),
which we will cover in Chapter 12, there aren't any aggregate array operations.
-
To determine if two arrays are equal, you have to test all
members of the arrays individually:
int age[5] = {23, 10, 45, 14,
92};
int year[5] = {23, 10, 45, 16,
92};
bool equal = true;
for (int i = 0; equal &&
i < 5; i++)
equal
= age[i] == year[i];
-
Examples:
// compute array sum as the
sum of arrays
// age and year
int age[5] = {23, 10, 45, 14, 92};
int year[5] = {23, 10, 45, 16,
92};
int sum[5];
for (int i = 0; i < 5; i++)
sum[i]
= age[i] + year[i];
// -----------------------------------------
// compute total as the sum of
the members
// of array temp
float temp[3] = {98.6, 1.4E2,
-27.8};
float total = 0.0;
for (i = 0; i < 3; i++)
total
+= temp[i];
// -----------------------------------------
// output the number of meteors
sighted
// each night during an observation
// and the total for the week
enum Day {SUN, MON, TUE, WED,
THU, FRI, SAT};
int meteors[] = {4, 8, 43, 32,
17, 12, 8};
int total = 0;
for (Day i = SUN; i <= SAT;
i = Day(i + 1))
{
cout <<
"Meteors during ";
switch
(i)
{
case SUN : cout << "Sunday";
break;
case MON : cout << "Monday";
break;
case TUE : cout << "Tuesday";
break;
case WED : cout << "Wednesday";
break;
case THU : cout << "Thursday";
break;
case FRI : cout << "Friday";
break;
case SAT : cout << "Saturday";
break;
}
cout <<
"'s observation was "
<< meteors[i] << "." << endl;
total
+= meteors[i];
}
cout << "Total for the week
was "
<< total << "." << endl;
Passing
Arrays as Parameters
-
By default, arrays are always passed to functions by reference,
not by value.
-
When any variable is passed to a function by reference, C++
actually sends the address of the variable to the function. The address
that is passed to the function for arrays is the base
address of the array.
-
The Base Address of and array
is the memory address of the first element of the array.
-
Example of a function with an array parameter:
void ZeroArray(float arr[],
int nElements)
{
for (int
i = 0; i < nElements; i++)
arr[i]
= 0.0;
}
-
Note that in the function heading, the size of the array
arr is not specified.
However, the number of elements in the array is passed to the function
as a value parameter. This allows the function to be used with arrays of
any size:
float velocity[30];
float warp[9000];
. . .
ZeroArray(velocity, 30);
ZeroArray(warp, 9000);
-
One of the reasons for using value parameters is that it
prevents a function from changing the value of the variable. Since arrays
are always passed by reference, C++ has another way to ensure that a function
cannot change the values of the array:
void CopyArray(const int source[],
int destination[],
int size)
{
for (int
i = 0; i < size; i++)
destination[i]
= source[i];
}
When the C++ reserved word const
is used with a prarmeter, the function cannot alter that parameter.
Case Study - Frequency of
All Characters
-
On pages 629-635 of the text, there
is a case study for a program that reads a text data file and produces
a frequency count for all the printable characters in that file.
-
The design of the program is discussed
in the text.
-
The program can be viewed at CountAll
program.
Updated November 19, 1998 by Tom Irby.
Copyright 1998 by Thomas C. Irby