//Susan J. Cheslog //Discrete Structures Project #include #include const int MAX = 20; const int EMPTY = 0; //============================================================== void main () { int size; int entry; int row; int col; int lastEntry; int newRow; int newCol; int Grid[MAX][MAX]; ofstream outdata; ifstream indata; indata.open("sizes.txt"); outdata.open("results.txt"); indata >> size; while(indata) { outdata << endl << endl; if (size < 1 || size > MAX || size % 2 == 0) outdata << size << " is not a valid size."; else { for (row = 0; row < size; row ++) for (col = 0; col < size; col++) Grid[row][col] = EMPTY; lastEntry = size * size; row = 0; col = size/2; Grid[row][col] = 1; for (entry = 2; entry <= lastEntry; entry++) { newRow = ((row - 1 + size) % (size)); newCol = ((col + 1) % (size)); if (Grid[newRow][newCol] == EMPTY) { row = newRow; col = newCol; } else row = (row + 1) % size; Grid[row][col] = entry; } for (row=0; row < size; row++) { for (col = 0; col < size; col++) outdata << setw(4) << Grid[row][col]; outdata << endl; } } indata >> size; } }