/** * Assignment 3.b. Spiral Matrices * * Compute and print spirals. A spiral consists of consecutive integers * starting at a given value arranged in a counter-clockwise spiral that * grows outwards from the center of a square matrix made from a 2-d array. * * Author: Ron Mak * Department of Computer Engineering * San Jose State University */ #include #include using namespace std; const int MAX_SIZE = 101; // maximum size of the square matrix const int MAX_START = 50; // maximum starting number void do_spiral(const int n, const int start); void make_spiral(const int n, const int start, int matrix[][MAX_SIZE]); void print_spiral(const int n, const int matrix[][MAX_SIZE]); /** * The main: Generate and print spirals of various sizes. */ int main() { do_spiral(1, 1); do_spiral(5, 1); do_spiral(9, 11); do_spiral(12, 13); do_spiral(15, 17); } /** * Generate and print a spiral of size n with a given starting number. * @param n the size of the square matrix (must be odd and 1 <= n <= MAX_SIZE). * @param start the starting number (1 <= start <= MAX_START). */ void do_spiral(int n, int start) { cout << "Spiral of size " << n << " starting at " << start << endl; // Check that 1 <= n <= MAX_SIZE. if ((n < 1) || (n > MAX_SIZE)) { cout << "***** ERROR: Size " << n << " < 1 or > " << MAX_SIZE << endl << endl; return; } // Make sure that n is odd. if (n%2 != 1) { cout << "***** Error: Size " << n << " must be odd." << endl << endl; return; } // Check that 1 <= start <= MAX_START. if ((start < 1) || (start > MAX_START)) { cout << "***** Error: Starting value " << start << " < 1 or > " << MAX_START << endl << endl; return; } int matrix[MAX_SIZE][MAX_SIZE]; // 2-d spiral matrix make_spiral(n, start, matrix); print_spiral(n, matrix); } /** * Make a spiral in a square matrix of size n. * Start the spiral with a given starting number. * @param n the size of the prime spiral. * @param start the starting number of the spiral. * @param matrix the square matrix that contains the prime spiral. */ void make_spiral(const int n, const int start, int matrix[][MAX_SIZE]) { enum Direction {UP, DOWN, LEFT, RIGHT}; // which way to go int row = n/2; // starting row in the middle int col = row; // starting column in the middle int dr = 0; // row change in a direction int dc = 1; // column change in a direction int steps = 1; // how many steps to take in each direction int stop = n*n + start - 1; // stopping number Direction direction = RIGHT; // starting direction // Loop up to the stopping number. int number = start; while (number <= stop) { // Set values along the current direction step times. for (int j = steps; j > 0; j--) { matrix[row][col] = number++; // Move to the next position in the current direction. row += dr; col += dc; } // Change direction. Increase the number of steps // if changing direction to the left or to the right. switch (direction) { case RIGHT: { direction = UP; dr = -1; dc = 0; break; } case DOWN: { direction = RIGHT; dr = 0; dc = 1; steps++; break; } case LEFT: { direction = DOWN; dr = 1; dc = 0; break; } case UP: { direction = LEFT; dr = 0; dc = -1; steps++; break; } } } } /** * Print a spiral. * @param n the size square matrix. * @param matrix the matrix that contains the spiral. */ void print_spiral(const int n, const int matrix[][MAX_SIZE]) { cout << endl; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { cout << setw(4) << matrix[row][col]; } cout << endl; } cout << endl; }