CMPSC 201
Ch. 11 Notes - We will only be studying sections 1-5, 7, & 8 of Ch. 11 in this class.
Objective #1: Use one-dimensional arrays.
- An array
is a list of variables that are referred to with the same variable name (identifier.)
- To declare an array before it is used in the body of your program, you must
use a statement like:
int scores[10];
which would declare an array of integers, which is named "scores".
scores can store up to 10 different integer values. The positions of the array
are identified by their index positions which run from 0 to 9 (not 1 to 10.)
Each one of the 10 variables in scores is called an element.
You could also store doubles in an array or any other primitive data type.
You simply have to declare the array appropriately. For example, the following
declaration would declare an array of doubles:
double sums[20];
- You can initialize the separate elements of an array when it is declared.
The following statement illustrates this,
int scores[5] = {65, 76, 45, 99, 100};
or simply
int scores[] = {65, 76, 45, 99, 100};
- Arrays can be used to efficiently perform tasks in conjunction with loops,
especially determinant for loops.
- The following code segment allows a user to input 5 test scores, storing
them in an array named userScores, and also computes the test average:
for (i = 0; i < 5 ; i++)
{
cin >> userScores[i];
sum += userScores[i];
}
testAverage = sum / 5.0;
- Be careful not to reference an element that doesn't exist within an array.
That is, do not to attempt to access an index
position that is not actually part of an array. The
result will probably be a run-time (not compile-time) error. If you initialize
the array, scores, to the size of 10 but then attempt to assign the fifteenth
index position of scores the value of 28, you will not experience a syntax
error. Rather, you will accidentally be overwriting another variable possibly.
For example, the following code would produce an error because the element
value[4] doesn't exist. Only elements value[0], value[1], value[2], &
value[3], exist as elements of the array value.
double value[4] = { 0, 1, 2, 3 };
cout << value[4];
Objective #2: Use multi-dimensional arrays.
- Arrays of more than one dimension can be used to arrange more complicated
sets of data. Two-dimensional arrays are used quite often. For example, a
spreadsheet can be thought of as 2-dimensional
array (sometimes called a table.) The same variable name (that
is, identifier) is used to access each element of the 2-dimensional array
but the proper index position subscripts must be used.
- To declare an array of integers called studentGrades to be a 2-dimensional
array with 3 rows and 4 columns, you would use the statement:
int studentGrades[3] [4];
I always think of RC Cola to remember that the first value refers to the
number of rows in the array and the second value refers to the number of columns.
- You can initialize the 2-dimensional array when you declare it by using
commas and braces appropriately. For example,
int studentGrades[3] [4] =
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
- Be careful though when assigning values
to a specific position within a 2-dimensional array. Keep in mind that just
like one-dimensional arrays, the subscript positions with regard to the rows
and the columns begin at 0, not 1. In the example above the value of
the variable studentGrades[0] [2] is 3.
- When you pass an array in C++, you are actually passing by address (which
our textbook calls "passing by simulated reference"). This means
that you are not passing by value but simply that you are passing the hexadecimal
memory address of the first element of the array. Any changes to the values
of the array's elements that occur in the function will affect the value of
the argument in the calling function. See our Ch.
11 Demo Program #1
as an example of passing a two-dimensional array.