Wyo C++ Ch. 15 Notes
Note that you are expected to be familiar with built-in
C++ multi-dimensional arrays and their syntax. However, you must use the apmatrix
class and apmatrix objects for all programming assignments in this course.
Objective #1: Be familiar with built-in C++ two-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 a two-dimensional array. The same variable name ( identifier) is used
to access each element of the two-dimensional array but two subscripts must
be used. Two-dimensional arrays are also called tables or matrices.
- To declare a built-in C++ two-dimensional array of integers called studentGrades
with 3 rows and 4 columns, you would use the statement:
int studentGrades[3][4];
where the 3 represents the number of rows in the two-dimensional array
and the 4 represents the number of columns.
- You can initialize the built-in C++ two-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}
};
or with the less readable
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 studentGrades[0][2] is 3.
- A built-in C++ multi-dimensional array of any number of dimensions can be
used. For example, a five dimensional array could be declared with:
int studentTestScores[20][3][2][5][2];
although it is hard to visualize and it would rarely be necessary to use something
like this in a real-life programming situation.
- A multi-dimensional array can only contain data of one data type. Unlike
a spreadsheet (like MS Excel) you cannot organize integers and floating-point
values (i.e. doubles) in the same two dimensional array. (You could however
use parallel arrays to relate such sets of data.)
Objective #2: Use the apmatrix class.
- You should learn to use apmatrix objects rather than built-in C++
multi-dimensional arrays. The apmatrix class gives a programmer the same advantages
over built-in C++ two-dimensional arrays that the apvector class gives over
built-in C++ one-dimensional arrays.
- The apmatrix class
is similar to the apvector class because an apmatrix is really just an "apvector
of apvectors."
- Review the apmatrix
class to familiarize yourself with its copy constructors, operator overloads,
member functions, and private member variables.
- constructors
- default constructor - A 0 by 0 apmatrix is instantiated. This apmatrix
will have to be resized later since it is illegal to access an element
that doesn't exist. Example:
apmatrix <apstring> aMatrix;
would instantiate an apmatrix named aMatrix of apstring elements.
- Another constructor in the apmatrix class allows the programmer
to instantiate an apmatrix object with a specified number of rows
and columns. For example,
apmatrix <int> aMatrix(3, 4);
where the apmatrix contains int elements and has 3 rows with 4 columns.
However, garbage values would initially be stored in each element
of the matrix.
- Another constructor allows the programmer to instantiate an apmatrix
object with a certain number of rows and columns and a fill value
initializing each element of the matrix. For example,
apmatrix <int> aMatrix(3, 4, 0);
instantiates the matrix named aMatrix, where the matrix contains int
elements and has 3 rows with 4 columns. But each integer element is
initialized to the value 0. The third argument is the "fill value".
- copy constructor - Allows an apmatrix to be copied in case a programmer
needs to pass an apmatrix by value. However, you should usually pass
apmatrix objects by reference rather than by value. Passing a large
apmatrix by value consumes a lot of memory.
- member functions
- accessors
- numrows - returns the number of rows of an apmatrix object.
For example,
cout << aMatrix.numrows( );
would display the number of rows currently found in the apmatrix
aMatrix.
- numcols - returns the number of columns of an apmatrix
object.
- modifiers
- resize - allows a programmer to dynamically resize an
apmatrix object with a different number of rows and/or columns.
When a matrix is resized, data (values) will be lost however if
the new number of rows and/or columns is smaller than the original
number of rows. Example:
aMatrix.resize(2, 3);
will change aMatrix so that it now has 2 rows and 3 columns. If
a matrix is instantiated with the default constructor, it must
always be resized in order to accomodate elements later in a program.
- overloaded operators
- = - The overloaded
assignment operator allows the programmer to assign the complete
contents of one apmatrix to another one. For example, the statement
aMatrix = yourMatrix;
assigns yourMatrix to aMatrix, overwriting all of the elements
of aMatrix. Note that aMatrix will be resized to the size of yourMatrix
even if it was initially smaller or larger than yourMatrix.
- [] - The overloaded
square brackets operator allows the programmer to directly access
a single element of an apmatrix. Example,
num = aMatrix[2][3];
would assign the element found in the 2nd row and 3rd column of
aMatrix to the variable num. If there are less than 2 rows or
less than 3 columns in aMatrix, the apmatrix class generates a
run-time error such as
Illegal matrix index: 2 max index = 1
The fact that this kind of error is handled by the apmatrix class
and does not lead to nastier run-time or logic errors is one reason
why the matrix objects are "safer" than built-in C++
multi-dimensional arrays. See the [] definition in apmatrix.cpp
to understand how the developers of the apmatrix close implemented
this example of bounds-checking (or range-checking.)
- private member variables
- myRows - the number of rows in the apmatrix object.
- myCols - the number of columns in the apmatrix object.
- myMatrix - the elements of the matrix object stored as an
apvector of apvectors. The template class apvector must be included
within apmatrix.h in order for this member variable to be used. Of
course, you should also remember that an apvector is really a built-in
C++ array if you get technical.
- When you pass a matrix, you should always pass it by address rather than
value. It is very inefficient to pass any kind of array by value since every
element must be passed rather than just the memory address of the first element.
See this example for a program which passes a
matrix by address rather than passing it by value.