Topical Information

This program should emphasize the use of 2D vectors.

Program Information

Allow the user to enter values for 2 matrices. You can set a maximum size, but the user gets to determine the actual size. Check for valid multiplication orders and compute the product(s) for display.

Please use good functional decomposition to make your development easier: 2D matrix entry, 2D matrix multiply, 2D matrix display, etc.

Place any matrix functions you develop into a matrix library.

Example Run

As an example the program interaction might look something like (the parts in blue are typed by the user):

$ matmult.out

                 Welcome to the Matrix Multiplying Program!!!

Please enter the size of your first matrix:  40x50

I'm sorry, that matrix is too tall for this program...please reduce the
height...

Please enter the size of your first matrix:  4x500

I'm sorry, that matrix is too broad for this program...please reduce the
width...

Please enter the size of your first matrix:  4x5

Thank you, now enter your matrix (row by row):

1 2 2 1 2
1 1 0 1 0
2 2 0 2 0
2 1 2 1 1

Now for the second matrix...

Please enter the size of your second matrix:  3x4

Thank you, now enter your matrix (row by row):

1 2 1 2
2 0 2 0
2 2 1 1

Thank you...checking multiplication validities...

Your second matrix can be multiplied by your first matrix.
No other orderings are valid...

When multiplying your 3x4 by your 4x5, you get the 3x5:

9 8 6 7 4
6 8 4 6 4
8 9 6 7 5

Thank you for using the MMP!!

Endeavor to have a burgeoning day!

$

Note: Recall that for matrix multiplication to be valid, the inner dimensions must match.

Thought Provoking Questions

  1. How can you test force them to enter a valid dimension for their matrix -- not letting the program continue without a usable size?

  2. How can you read in their matrix values row by row?

  3. How can you test whether each of the possible orderings is valid or not? (Hint: There are 4 orderings: f*f, f*s, s*f, s*s. Where f is the first matrix and s the second.)

  4. How can you be sure that if the two matrices fit that their products will also fit in your maximum dimensions? Is there something you should do with your maximum dimensions to make this work?

  5. How exactly does matrix multiplication work again? For each ____ of the left-hand matrix, I multiply element-wise by the ____ of the right-hand matrix -- summing the results and placing it in the answer matrix. Then I proceed through each such pair of ____ and ____. Hmm...Oh, yeah...

  6. How many loops are implied by the matrix multiplication traversal pattern? (Hint: There are at least 2 since matrices are 2D...)

  7. How can you print a 2D vector row by row?

  8. How many answer matrices do you need? There are four possible orderings, each of which would give its own answer. But do you need them all at once?

This assignment is (Level 3.5).

Options

Add (Level 2) to also search through the transposed matrices for possibly valid multiplications: ~f*~f, ~f*s, ~s*f, ~s*~s, ~s*~f, and ~f*~s. Recall that the transpose of a matrix is formed by flipping along the major diagonal. For example, a 3x5:

9 8 6 7 4
6 8 4 6 4
8 9 6 7 5

Would become a 5x3:

   9 6 8
   8 8 9
   6 4 6
   7 6 7
   4 4 5

More Thought Provoking Questions

  1. Do you have to create the transposed matrix or can you simply alter your traversal pattern?

  2. Does this transposition alter your decision about the maximum dimensions you chose before? Will all the products still fit?

Add (Level 1) to give the user the option of choosing between a pre-counted entry (as above) or an end-detected entry. (Hint: You can detect the end of a row by looking for the '\n' after the last row value. This need only be done for the first row. You can detect the end of the matrix by a blank row being entered -- one with no numbers -- immediately ending in a '\n'.)

Add (Level 1) to make the preceding option automatic instead of by user choice. In other words, you detect from their input whether they are entering the size first or using end-detected entry. (Hint: The size has an 'x' between the values where-as two matrix elements have only space.) (Warning: if their detected row is only one element long, there will be a '\n' instead of an 'x' and it will need to not only discover the entry method but set the row length.)