Hands On C++: Project 13


The Projects

Your instructor will 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. Following the pattern in the lab exercise, first, design using OCD; then code your design in C++ using stepwise translation; finally, test your program thoroughly.

Project #13.1: A magic square is an n-by-n table in which the integers 1, 2, 3, ..., n^2 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-by-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.

Build a MagicSquare class as a vector of vectors. Given an odd integer n, its constructor should use the preceding algorithm to initialize the two-dimensional structure. Provide an output operator to display the magic square using the format shown above. Then write a driver program that inputs the value n and, using your MagicSquare class, constructs and outputs the corresponding magic square.

Project #13.2: 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
Use object-centered design to design and implement a SalesTable class and use it to solve this problem. In addition to an output operation that displays a SalesTable in a format like that shown above, its constructor should allow a SalesTable object to be initialized with data from a file. It should also provide operations to determine the total sales for a given salesperson, as well as the total sales for a particular model of automobile.

Project #13.3: In Chapter 13 of C++: An Introduction to Computing, a Matrix data type is presented. Get this data type fully operational, including functions (or methods if you define it as a class) to perform the addition, subtraction, multiplication, transpose, test equality, input, and output operations for Matrix objects. To test your class, create a driven driver program that implements a menu-driven Matrix calculator.

Project #13.4: Create a TicTacToeGame class that stores the 3-by-3 board, and methods to initialize the board, display the board, and take a turn. Add a driver program that uses your class to allow a person to play Tic-Tac-Toe against the computer.

Turn In

Turn the following things:

  1. This grade sheet.
  2. Your OCD.
  3. Your source program.
  4. The output from an execution of your program.


Lab Home Page | Lab Exercise
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.