//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #5B" #define REVISION (0) #define TITLE "Magic Square" #define SUBTITLE "Problem 7.19" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "05b0soln.txt" //========================================================================= // PROBLEM: // // Write a program to generate a 5x5 Magic Square using the following // set of rules: // // 1) Place 1 in the middle of the top row. // 2) Place successive numbers in the square up and to the left of the // present square subject to the following special cases: // Wrap around to the bottom or right side as necessary. // If the square is already occupied, drop to the square directly // below the present square instead. // If the present square is the top left corner, drop to the // square directly below it. // SOLUTION: // // TOP LEVEL DECOMPOSITION: // 1) TASK: Draw the empty boxes for the Magic Square // 2) TASK: Generate the Magic Square // 3) TASK: Display the Magic Sqaure // // The display for each element of the Magic Square will be as follows: // // +----+ The vertical pitch is 2 // | nn | The horizontal pitch is 5 // +----+ The top left corner is (-2,-1) relative to the number // // 1) TASK: Draw the empty boxes for the Magic Square // 1.1) SET: row = 0 // 1.2) WHILE: (row < SIZE) SIZE = 5 in this case // 1.2.1) SET: col = 0 // 1.2.2) WHILE: (col < size) // 1.2.2.1) TASK: Place cursor at position where number will be printed // 1.2.2.1.1) SET: r = ROW_PITCH*row + FIRST_ROW // 1.2.2.1.2) SET: c = COL_PITCH*col + FIRST_COL // 1.2.2.2) TASK: Draw character box around the center // 1.2.2.2.1) SET: cursor to (c-2, r-1) // 1.2.2.2.2) PUT: "+----+" // 1.2.2.2.3) SET: cursor to (c-2, r) // 1.2.2.2.4) PUT: "| |" // 1.2.2.2.5) SET: cursor to (c-2, r+1) // 1.2.2.2.6) PUT: "+----+" // 1.2.2.3) TASK: Initial the box contents to zero // 1.2.2.3.1) SET: array[row][col] = 0 // 2) TASK: Generate the Magic Square // 2.1) TASK: Set cursor to middle box of top row // 2.2) SET: n = 1 // 2.3) LOOP: // 2.3.1) TASK: Set present box equal to n // 2.3.1.1) SET: array[row][col] = n // 2.3.2) TASK: Determine box to move to // 2.3.2.1) IF: (row = 0) AND (col = 0) // present box is corner box // 2.3.2.1.1) TASK: Impose special case for corner // 2.3.2.1.1.1) SET: row = 1 (col is already 0) // 2.3.2.2) ELSE: // 2.3.2.2.1) TASK: Determine the "preferred" next box // 2.3.2.2.1.1) SET: nextrow = row-1 // 2.3.2.2.1.2) SET: nextcol = col-1 // 2.3.2.2.2) TASK: Take care of any wrap-around issues // 2.3.2.2.2.1) IF: nextrow < 0 // 2.3.2.2.2.1.1) SET: nextrow = SIZE - 1 // 2.3.2.2.2.1) IF: nextcol < 0 // 2.3.2.2.2.1.1) SET: nextcol = SIZE - 1 // 2.3.2.2.3) IF: array[nextrow][nextcol] != 0 // desired cell already filled // 2.3.2.2.3.1) TASK: Move to cell directly below present cell // 2.3.2.2.3.1.1) SET: row++ (col is already set) // 2.3.2.2.3) ELSE: // preferred cell is the cell to use // 2.3.2.2.3.1) SET: row = nextrow // 2.3.2.2.3.2) SET: col = nextcol // 2.3.3) TASK: Increment n // 2.3.3.1) SET: n++ // 2.4) WHILE: n < SIZE*SIZE // 3) TASK: Display the Magic Sqaure // 3.1) SET: row = 0 // 3.2) WHILE: (row < SIZE) SIZE = 5 in this case // 3.2.1) SET: col = 0 // 3.2.2) WHILE: (col < size) // 3.2.2.1) TASK: Place cursor at position where number will be printed // 3.2.2.1.1) SET: r = ROW_PITCH*row + FIRST_ROW // 3.2.2.1.2) SET: c = COL_PITCH*col + FIRST_COL // 3.2.2.2) TASK: Print the number // 3.2.2.2.1) PUT: array[row][col]