Chapter 3
by Forest J. Handford
This chapter covers arrays and matrices
In this chapter we will make a program that multiplies matrices. Matrices, what the hell are they? Matrices is the plural of matrix. When I say matrix I'm not talking about the kick-ass movie with Keanu Reaves. A matrix is a math term. The Computer Science version of the term is a two-dimensional array. This isn't astro-physics. An array is a set of data stored as cells, like a spreadsheet. A one-dimensional array has one subscript. A subscript is an address or position within an array. To get or assign values to a cell in an array you need to write the arrays name followed by "[", then the subscript and finally "]". The lowest possible subscript for C and C++ arrays is zero. Unfortunately C and C++ forces us to declare array sizes with constants. This makes them somewhat inflexible. Consider the following:
int IntegerArray[5];
The above code declared a six celled one-dimensional array. Valid subscripts for the above array are zero through five. Each cell can hold an integer value. Consider the following code to loop through the above array:
for ( int Subscript = 0; Subscript < 5; Subscript++)
{
IntegerArray[Subscript] = 1;
}
The above code will set every cell to equal zero. The following table shows how the values are stored:
| Subscript | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| Value | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
A Two-dimensional array has two subscripts, one for each dimension, and an n-dimensional array has n subscripts, one for each dimension. Consider the following:
char CharArray[4][4]; CharArray[0][0] = 0; CharArray[0][1] = 1; CharArray[0][2] = 2; CharArray[1][0] = 1; CharArray[2][0] = 2; CharArray[1][1] = 2; CharArray[1][2] = 5; CharArray[2][1] = 3; CharArray[2][2] = 9; CharArray[4][4] = 8;
A cell that isn't given a value will have what-ever value was last stored in that part of RAM. Here is an example of what the values for the array would be:
| Subscripts | 0 | 1 | 2 | 3 | 4 |
| 0 | 0 | 1 | 2 | Unknown | Unknown |
| 1 | 1 | 2 | 5 | Unknown | Unknown |
| 2 | 2 | 3 | 9 | Unknown | Unknown |
| 3 | Unknown | Unknown | Unknown | Unknown | Unknown |
| 4 | Unknown | Unknown | Unknown | Unknown | 8 |
You can think of a 5 dimensional array as having cells defining where a book is in a library. The first subscript defines the country of the library. The second subscript defines the state or province of the library. The third subscript defines the town of the library. The fourth subscript defines the library within the town. The fifth subscript defines the book within the library.
Now we need to look at matrix multiplication. Let's look at two matrices. The first matrix will be A. The A matrix will hold the price of my stocks as of 10/31/99. The B matrix will hold the number of shares I have for each stock.
| Stock | Price |
| AMD | 19.25 |
| Coke | 57.00 |
| MEDITECH | 23.05 |
| Stock | AMD | Coke | MEDITECH |
| Shares | 50 | 8 | 30 |
To get my total stock value for 10/31/99 we must multiply the matrices as follows:
19.25 * 50 + 57 * 8 + 23.05 * 30 = 2110
Now let's do a vector multiplication using matrices. The first matrix will be a ships current location. The second matrix will define the velocity of the ship:
| Position | Miles from the center of the game's current level |
| X | 34 |
| Y | 2 |
| Z | 45 |
| Direction | X | Y | Z |
| Miles per second | 1 | 5 | 2 |
To find out where the ship will be in 1 second we do the following:
X = 34 * 1 = 34
Y = 2 * 5 = 10
Z = 45 * 2 = 90
Now let's multiply a larger matrix. We will use my portfolio and Warren Buffet's portfolio:
| My Stock | Birkshire Hathaway | Coke | AMD |
| The number of shares I have | 0 |
8 |
50 |
| A guess at the number of shares Warren has | 1,000,000 |
1,000,000 | 0 |
| My Stock | Price |
| Birkshire Hathaway | 63,900 |
| Coke | 57 |
| AMD | 19 |
Multiply and we get the following matrix:
| Equation | Portfolio total | |
| Me | 0 * 63,900 + 8 * 57 + 50 * 19 | 1,406.00 |
| Warren | 1,000,000 * 63,900 + 1,000,000 * 57 + 0 * 19 | 63,957,000,000.00 |
As you can see I'm giving Warren a run for his money! Two notable rules of matrices are:
| If you are sick enough to be interested in matrices and want to learn more check out College Mathematics. by S.T. Tang. |
The last thing we need to cover is the getchar function. This function is from stdio.h. It will get 1 character from the keyboard. To use it you need to first clear the input stream by calling it and then call it again to get what ever value the user enters. Consider the following:
char YorN
printf("Exit (Yes or No)? ");
getchar();
YorN = getchar();
Unless the prompt confuses the user, or maybe a cat jumps on the keyboard, YorN will equal 'Y', 'y', 'N' or 'n'. Now we are ready for our matrix multiplication program. This was also a program for an employment quiz given by Tiburon.
NOTE: The following program can only handle whole numbers between 37,000 and -37,000. You can work to figure how to change the program to make it multiply the above matrices.

[ Code and Program ]
That is all for this chapter.