Assignment 31: Matrix sums
due Wednesday 5/22
Write a program that prompts the user for an integer to store in each of the entries
of a 3-by-4 apmatrix (two-dimensional array), then prints out the matrix as a rectangular grid
with properly aligned columns (use setw, described on textbook page 64, to align
the columns). For example, the user might wish to enter the following matrix:
| 7
| 3
| 2
| 4
|
| 10
| 100
| 1000
| 10000
|
| 123
| 4567
| -89
| -10004
|
The user input dialogue should look like this:
Row 0, column 0: 7
Row 0, column 1: 3
Row 0, column 2: 2
Row 0, column 3: 4
Row 1, column 0: 10
etc.
After you've written a program that inputs numbers and outputs them as a grid,
enhance the program so that it prints the sum of each row at the end of the row,
and the sum of each column underneath the column.
To receive the 5 points in the Design and Efficiency scoring category,
your program must not calculate the sums by writing out all of the terms.
| bad: | M[0][0] + M[1][0] + M[2][0]
|
| good: | a loop that performs the same computation by adding one number at a time
|
(The fault of the bad approach is that it wouldn't be feasible to write out the
whole sum if the matrix had a large number of rows or columns.)
For your sample run, use the matrix shown on this page (7, 3, 2, etc.).
Extension
Revise the program so that it can be used for a matrix of any size specified by
the user when the program is run. Submit two sample runs, with the matrix specified above,
and a matrix of your choice with different numbers of rows and columns.