Hands On C++: Project 12


Objectives

1. Practice defining two-dimensional vectors.
2. Practice using doubly-subscripted variables.
3. Practice building classes.

Introduction

Your instructor will tell you which one of the following projects to do.

Projects

12.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 an algorithm for constructing an n-by-n magic square, for any odd integer n:
   a. Place i = 1 in the middle of the top row.
   b. To place i+1, move up one row and one column to the right,
      unless:
      1) If the move takes you to row -1, column c:
          place i+1 in row n-1, column c.
      2) If the move takes you to row r, column n:
          place i+1 in row r, column 0.
      3) If the move takes you to an already filled square, OR
          if the move takes you to row -1, column n:
          place i+1 immediately below i.
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.

12.2. A certain automobile dealership sells ten different models of automobiles, and employs eight salespersons. 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.

12.3. In Chapter 12 of C++ An Introduction to Computing, a Matrix class is presented. Get this class fully operational, including function members to perform the addition, subtraction, multiplication, transpose, keyboard input, screen output, and file input and output operations for Matrix objects. To test your class, create a driven driver program that implements a menu-driven Matrix calculator.

12.4. Create a TicTacToeGame class that stores the 3-by-3 board, and function members 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: A hard copy of this grade sheet, attached to hard copies of

  1. all source files you created for this project; and
  2. the output from an execution of your executable program (using script).

Don't forget to clean up your directory when you are all finished...


Back to the Lab Exercise

Back to This Lab's Home Page


Copyright 1998 by Joel C. Adams. All rights reserved.