Practice Exercise 10
Multi-Dimensional Array Exercises

Objective: To become familiar with multi-dimensional array handling.


Exercise 1:

Write a program that declares a 2 x 3 array with the following values:

        5        6        7
        8        9       10

Print the array row-by-row, so your output looks like this:

       row 0:   5    6    7
       row 1:   8    9   10

Then print the array column-by-column, so your output looks like this:

       col 0:    5     8
       col 1:    6     9
       col 2:    7    10


Demo

For more information on how to use modules rather than forms, read the Console Applications notes.

 



Exercise 2: (HARD!)

Write a program to multiply two 2-dimensional arrays, one by the other.  This is called matrix multiplication.  For example, if firstArray ( a 2 x 2 array) contains

        1        2
        3        4

and secondArray (a 2 x 3 array) appears as

        5        6        7
        8        9       10

then the product matrix is 

productMatrix(0,0) = 1 * 5 + 2 * 8
productMatrix(1,0) = 3 * 5 + 4 * 8

productMatrix(0,1) = 1 * 6 + 2 * 9
productMatrix(1,1) = 3 * 6 + 4 * 9

productMatrix(0,2) = 1 * 7 + 2 * 10
productMatrix(1,2) = 3 * 7 + 4 * 10

See http://www.mathwords.com/m/matrix_multiplication.htm

Matrix multiplication can be done only if the number of columns in the multiplicand (the first array) equals the number of rows in the multiplier (the second array).

The program should read in the two arrays (or declare them and initialize them in the program), test to see if the multiplication is possible, and then multiply them if it is.  The output will be a printout of the two arrays and will either output the product array or print a message saying that multiplication is not possible.

Hint:  In order to perform the multiplication you need a loop nested in a loop nested in a loop.  Yikes!


Demo

For more information on how to use modules rather than forms, read the Console Applications notes.