Warning: These libraries are deprecated. You may try to use them, but chances are they will not work properly.

Welcome to lesson 7. Here you will learn the AP class apmatrix. What is apmatrix, you ask? It is basically a grid of items. It is kinda like a spreadsheet, but all of the cells must contain the same data type. On the computer, the matrix is read using the format matrix_name[row][column], where row is the horizontal (left to right) number and column is the verticle (top to bottom) number. It is quite simple, once you get the hang of it. It goes like this:
Count from here
0 1 2
0 0 x 0 0 x 1 0 x 2
1 1 x 0 1 x 1 1 x 2
2 2 x 0 2 x 1 2 x 2

So if you want to get the value from the top left cell, just do this: variable_name[0][0]. You want to get the bottom right one? just do variable_name[2][2]. 2 cells down and 1 cell to the right? Start counting from the top left corner. Go down two, which takes you to row 1, and go to the right 1, which takes you to column 0. variable_name[1][0]! Basically, just put the number of spaces down - 1 in the first set of brackets, and the number of spaces to the right - 1 in the second set of brackets. OK, now that you know what a matrix is, lets see how you use it.

apmatrix<int> mymatrix;

This creates an apmatrix named mymatrix which can only hold integer values in it's cells.

apmatrix<apstring> mymatrix(5,2);

This creates an apmatrix with 5 rows and 2 columns, whose cells can only hold apstrings.

apmatrix<double> mymatrix(4,2,1);

This creates an apmatrix with 4 rows, 2 columns, where each cell contains the value 1.0 (it is a double, which stores the number with a decimal).

This class is nearly identical to apvector, except it requires two brackets. You can use the brackets to change the value of the given cell, like variable_name[0][0] = value_of_the_same_data_type, or vise versa. You can also set an entire apmatrix equal to another entire apmatrix. This will re-make the matrix on the right of the equal sign to where it has the same number of rows, columns, and values in each cell regardless of what is in the matrix, with 1 exception. The data types of the matrices must match up.

Alrighty, now for this class's member functions. For these, assume that this declaration has already been made.

apmatrix<int> mymatrix(5,3,0).  


int i = mymatrix.numrows();

This gives i the number of rows in the matrix, which is 5.

i = mymatrix.numcols();

This gives i the number of columns in the matrix, which is 3.

mymatrix.resize(4,2);

This resizes the matrix to 4 rows and 2 columns. Notice that the matrix becomes smaller. When this happens, it cuts off the appropriate number of rows starting from the bottom, and the appropriate number of columns starting from the right. So here, the 5th (or 4th if you are using the index) row would be cut off and the 3rd(second if you are indexing) column would be cut off.

Pretty much every function that counts the length of something does not use indexing, while it's bracket ([]) notation does use indexing. So whenever you want to access the last item in the given class, use x.length()-1 or x.numrows()-1 or x.numcols()-1 inside the brackets. Without the -1, you would get an illegal vector index error, and the program would stop. Note that the length() function isn't part of ampatrix.

And that pretty much does it for the class apmatrix! Make sure you know this class well, because you will be using it a lot in future lessons!