Advertisement
juanjo12x

Siames_Method_Magic_Square

Mar 31st, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. // KN King Chapter 8
  2. // Programming Projects
  3. // Exercise 17 - Magic Square
  4.  
  5. #include <stdio.h>
  6.  
  7. int main() {
  8.  
  9.     // Introductory message
  10.     printf("This program creates a magic sqaure of a specified size.\n");
  11.     printf("The size must be an odd number between 1 and 99.\n");
  12.  
  13.     // Get the users magic number and allocate to int n
  14.     int n;
  15.     printf("Enter size of magic square: ");
  16.     scanf("%d", &n);
  17.  
  18.     // Create the array (not using VLA)
  19.     int magic[99][99];
  20.     int start = (n / 2); // The middle column
  21.     int max = n * n; // The final number
  22.     magic[0][start] = 1; // Place the number one in the middle of row 0
  23.  
  24.     // Loop to start placing numbers in the magic square
  25.     int row;
  26.     int col;
  27.     int next_row;
  28.     int next_col;
  29.     int i;
  30.     for (i = 2, row = 0, col = start; i < max + 1; i++) {
  31.         if ((row - 1) < 0) { // If going up one will leave the top
  32.             next_row = n - 1; // Go to the bottom row
  33.         }
  34.         else { next_row = row - 1; } // Otherwise go up one
  35.         printf("In row: %d\n", row);
  36.  
  37.         if ((col + 1) > (n - 1)) { // If column will leave the side
  38.             next_col = 0; // Wrap to first column
  39.             printf("Column will leave side\n");                                  
  40.         }                                                                        
  41.         else { next_col = col + 1; } // Otherwise go over one                    
  42.         printf("In col: %d\n", col);                                              
  43.  
  44.         if (magic[next_row][next_col] > 0) { // If that position is taken        
  45.             if (row > (n - 1)) { // If going to row below leaves bottom          
  46.                 next_row = 0; // Go back to the top                              
  47.             }                                                                    
  48.             else {                                                                
  49.                 next_row = row + 1; // Go to the row below                        
  50.                 next_col = col; // But stay in same column                        
  51.             }                                                                    
  52.         }                                                                        
  53.         row = next_row;                                                          
  54.         col = next_col;                                                          
  55.         printf("About to put %d in row %d, col %d\n", i, row, col);              
  56.         magic[row][col] = i; // Put the current value in that position            
  57.     }                                                                            
  58.  
  59.     // Now let's print the array                                                  
  60.     int j;                                                                        
  61.     for (i = 0; i < n; i++) {                                                    
  62.         for (j = 0; j < n; j++) {                                                
  63.             printf("%4d", magic[i][j]);                                          
  64.         }                                                                        
  65.         printf("\n");                                                            
  66.     }
  67.   return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement