|
course.wilkes.edu/CS125Labs |
||||||
Lab 12: Projects
Objectives
1. Practice defining two-dimensional
IntroductionYour instructor will tell you which one of the following projects to do.
Projects12.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 by salesperson r of model 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 0Write 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
12.4.
Create a Turn In: A hard copy of this grade sheet, attached to hard copies of
Don't forget to clean up your directory when you are all finished...
|
Home Membership
|
|||||
|
Last update: Tuesday, October 10, 2000 at 2:17:53 PM. |
||||||