Project 11


The Projects

Your instructor may assign you one of the problems below. To solve your problem, write a program that reads the necessary information to compute and output the indicated values, as efficiently as possible.

Project #11.1: A magic square is an n × n table in which the integers 1, 2, 3, ..., n2 appear exactly once, and all column sums, row sums and diagonal sums are equal. For example, here is a 5-by-5 magic square in which all the rows, columns and diagonals add up to 65:
--------------------------
| 17 | 24 |  1 |  8 | 15 |
--------------------------
| 23 |  5 |  7 | 14 | 16 |
--------------------------
|  4 |  6 | 13 | 20 | 22 |
--------------------------
| 10 | 12 | 19 | 21 |  3 |
--------------------------
| 11 | 18 | 25 |  2 |  9 |
--------------------------
Here is a partial algorithm for constructing an n × n magic square, for any odd integer n:

  1. Set i to 1.
  2. Set r to be the first row; set c to be the middle column.
  3. Repeat:
    1. Place i in row r and column c.
    2. Move r up one row, and move c one column to the right.
    3. If the move takes you past the top,
          Reset r to be the last row.
    4. If the move takes you to off the right side of the grid,
          Set c to be the first column.
    5. If the move takes you to an already filled square
          Move r down two rows and c to the left one column.

The algorithm is missing two crucial steps: (1) when should the loop terminate? and (2) what happens when the algorithm tries to work its way up the main diagonal a second time? Both of these steps are fixed with one statement each, although the second problem will require a bit more thought —-consider the example above when i = 16 is placed into the square.

Write a program to construct magic squares using a two-dimensional array or a vector of vectors whose dimensions are specified by an odd integer entered by the user.

Project #11.2 (from Chap. 13 of Programming in C++ for Engineering and Science): A certain automobile dealership sells ten different models of automobiles, and employs eight salespeople. Each month, the sales are recorded in a table in entry [r][c] corresponds to the number of sales of model r by salesperson c. For example, a typical sales table might appear as follows:
0 0 2 0 5 6 3 0
5 1 9 0 0 2 3 2
0 0 0 1 0 0 0 0
1 1 1 0 2 2 2 1
5 3 2 0 0 2 5 5
2 2 1 0 1 1 0 0
3 2 5 0 1 2 0 4
3 0 7 1 3 5 2 4
0 2 6 1 0 5 2 1
4 0 2 0 3 2 1 0
Write a program that, given such a sales table, produces a monthly sales report, displaying the monthly sales and the totals for each model and salesperson in easy-to-read format:
Sales Person
  Model :   1   2   3   4   5   6   7   8   :  Totals
------------------------------------------------------
    1   :   0   0   2   0   5   6   3   0   :    16
    2   :   5   1   9   0   0   2   3   2   :    22
    3   :   0   0   0   1   0   0   0   0   :     1
    4   :   1   1   1   0   2   2   2   1   :    10
    5   :   5   3   2   0   0   2   5   5   :    22
    6   :   2   2   1   0   1   1   0   0   :     7
    7   :   3   2   5   0   1   2   0   4   :    17
    8   :   3   0   7   1   3   5   2   4   :    25
    9   :   0   2   6   1   0   5   2   1   :    17
   10   :   4   0   2   0   3   2   1   0   :    12
------------------------------------------------------
 Totals :  23  11  35   3  15  27  18  17

Project #11.3: Section 13.4 of Programming in C++ for Engineering and Science describes matrix operations — addition, subtraction, multiplication by a scalar, and multiplying two matrices. Write a program to perform these operations on matrices together with input and output operations, finding a matrix's transpose ("rotate" it 90o so rows become columns), and checking if two matrices are equal. Design the program so that it implements a menu-driven matrix calculator.

Project #11.4: Develop a program to play Tic-Tac-Toe against a user.


Back to the Lab Exercise

Back to This Lab's Home Page


Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)