Skip to main content
Question Protected by Jamal
added 68 characters in body
Source Link

Write a program that prints an n x n\$n \times{} n\$ magic square (a square arrangement of the numbers 1, 2, ..., n2\$1, 2, \ldots, n^2\$ in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n\$n\$.

Store the magic square in a 2D array. Start by placing the numbers 1 in the middle of row 0\$0\$. Place each of the remaining numbers 2, 3, ...n2\$2, 3, \ldots, n^2\$ by moving up one row and over one column.

Any attempt to go outside the bounds of the array should "wrap around" to the opposite side of the array. For example, instead of storing the next number in row -1\$-1\$, we would store it in row n - 1\$n - 1\$ (the last row). Instead of storing the next number in column n\$n\$, we would store it in column 0\$0\$.

If a particular array element is already occupied, put the number directly below the previously stored number. If your compiler supports variable length arrays, declare the array to have n\$n\$ rows and n\$n\$ columns. If not, declare the array to have 99\$99\$ rows and 99\$99\$ columns.

Write a program that prints an n x n magic square (a square arrangement of the numbers 1, 2, ..., n2 in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n.

Store the magic square in a 2D array. Start by placing the numbers 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ...n2 by moving up one row and over one column.

Any attempt to go outside the bounds of the array should "wrap around" to the opposite side of the array. For example, instead of storing the next number in row -1, we would store it in row n - 1 (the last row). Instead of storing the next number in column n, we would store it in column 0.

If a particular array element is already occupied, put the number directly below the previously stored number. If your compiler supports variable length arrays, declare the array to have n rows and n columns. If not, declare the array to have 99 rows and 99 columns.

Write a program that prints an \$n \times{} n\$ magic square (a square arrangement of the numbers \$1, 2, \ldots, n^2\$ in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of \$n\$.

Store the magic square in a 2D array. Start by placing the numbers 1 in the middle of row \$0\$. Place each of the remaining numbers \$2, 3, \ldots, n^2\$ by moving up one row and over one column.

Any attempt to go outside the bounds of the array should "wrap around" to the opposite side of the array. For example, instead of storing the next number in row \$-1\$, we would store it in row \$n - 1\$ (the last row). Instead of storing the next number in column \$n\$, we would store it in column \$0\$.

If a particular array element is already occupied, put the number directly below the previously stored number. If your compiler supports variable length arrays, declare the array to have \$n\$ rows and \$n\$ columns. If not, declare the array to have \$99\$ rows and \$99\$ columns.

added 179 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Magic Square in C (Beginner)

StillI'm still teaching myself C out of KN King's C Programming: Modern Approach. Actually pretty excited I go this problem solved in under 2 hours haha. I asked this over on Stack Overflow and it was recommended I post it here.

Write a program that prints an n x n magic square (a square arrangement of the numebrsnumbers 1, 2, ..., n2 in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n.

Store the magic square in a 2d2D array. Start by placing the numnbernumbers 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ...n2 by moving up one row and over one column.

My answer: // KN King Chapter 8 // Programming Projects // Exercise 17 - Magic Square

#include// <stdio.h>KN King Chapter 8
// Programming Projects
// Exercise 17 - Magic Square

#include <stdio.h>

int main() {

    // Introductory message
    printf("This program creates a magic sqaure of a specified size.\n");
    printf("The size must be an odd number between 1 and 99.\n");

    // Get the users magic number and allocate to int n
    int n;
    printf("Enter size of magic square: ");
    scanf("%d", &n);

    // Create the array (not using VLA)
    int magic[99][99];
    int start = (n / 2); // The middle column
    int max = n * n; // The final number
    magic[0][start] = 1; // Place the number one in the middle of row 0

    // Loop to start placing numbers in the magic square
    int row;
    int col;
    int next_row;
    int next_col;
    int i;
    for (i = 2, row = 0, col = start; i < max + 1; i++) {
        if ((row - 1) < 0) { // If going up one will leave the top
            next_row = n - 1; // Go to the bottom row
        }
        else { next_row = row - 1; } // Otherwise go up one
        printf("In row: %d\n", row);

        if ((col + 1) > (n - 1)) { // If column will leave the side
            next_col = 0; // Wrap to first column
            printf("Column will leave side\n");                                   
        }                                                                         
        else { next_col = col + 1; } // Otherwise go over one                     
        printf("In col: %d\n", col);                                              
                                                                              
        if (magic[next_row][next_col] > 0) { // If that position is taken         
            if (row > (n - 1)) { // If going to row below leaves bottom           
                next_row = 0; // Go back to the top                               
            }                                                                     
            else {                                                                
                next_row = row + 1; // Go to the row below                        
                next_col = col; // But stay in same column                        
            }                                                                     
        }                                                                         
        row = next_row;                                                           
        col = next_col;                                                           
        printf("About to put %d in row %d, col %d\n", i, row, col);               
        magic[row][col] = i; // Put the current value in that position            
    }                                                                             
                                                                              
    // Now let's print the array                                                  
    int j;                                                                        
    for (i = 0; i < n; i++) {                                                     
        for (j = 0; j < n; j++) {                                                 
            printf("%4d", magic[i][j]);                                           
        }                                                                         
        printf("\n");                                                             
}         }                                                                    
return 0;         
    return 0;                                                        
}             
}

Thanks for looking.

Magic Square in C (Beginner)

Still teaching myself C out of KN King's C Programming: Modern Approach. Actually pretty excited I go this problem solved in under 2 hours haha. I asked this over on Stack Overflow and it was recommended I post it here.

Write a program that prints an n x n magic square (a square arrangement of the numebrs 1, 2, ..., n2 in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n.

Store the magic square in a 2d array. Start by placing the numnber 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ...n2 by moving up one row and over one column.

My answer: // KN King Chapter 8 // Programming Projects // Exercise 17 - Magic Square

#include <stdio.h>

int main() {

// Introductory message
printf("This program creates a magic sqaure of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");

// Get the users magic number and allocate to int n
int n;
printf("Enter size of magic square: ");
scanf("%d", &n);

// Create the array (not using VLA)
int magic[99][99];
int start = (n / 2); // The middle column
int max = n * n; // The final number
magic[0][start] = 1; // Place the number one in the middle of row 0

// Loop to start placing numbers in the magic square
int row;
int col;
int next_row;
int next_col;
int i;
for (i = 2, row = 0, col = start; i < max + 1; i++) {
    if ((row - 1) < 0) { // If going up one will leave the top
        next_row = n - 1; // Go to the bottom row
    }
    else { next_row = row - 1; } // Otherwise go up one
    printf("In row: %d\n", row);

    if ((col + 1) > (n - 1)) { // If column will leave the side
        next_col = 0; // Wrap to first column
        printf("Column will leave side\n");                                   
    }                                                                         
    else { next_col = col + 1; } // Otherwise go over one                     
    printf("In col: %d\n", col);                                              
                                                                              
    if (magic[next_row][next_col] > 0) { // If that position is taken         
        if (row > (n - 1)) { // If going to row below leaves bottom           
            next_row = 0; // Go back to the top                               
        }                                                                     
        else {                                                                
            next_row = row + 1; // Go to the row below                        
            next_col = col; // But stay in same column                        
        }                                                                     
    }                                                                         
    row = next_row;                                                           
    col = next_col;                                                           
    printf("About to put %d in row %d, col %d\n", i, row, col);               
    magic[row][col] = i; // Put the current value in that position            
}                                                                             
                                                                              
// Now let's print the array                                                  
int j;                                                                        
for (i = 0; i < n; i++) {                                                     
    for (j = 0; j < n; j++) {                                                 
        printf("%4d", magic[i][j]);                                           
    }                                                                         
    printf("\n");                                                             
}                                                                             
return 0;                                                                     
}             

Thanks for looking.

Magic Square in C

I'm still teaching myself C out of KN King's C Programming: Modern Approach. Actually pretty excited I go this problem solved in under 2 hours. I asked this over on Stack Overflow and it was recommended I post it here.

Write a program that prints an n x n magic square (a square arrangement of the numbers 1, 2, ..., n2 in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n.

Store the magic square in a 2D array. Start by placing the numbers 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ...n2 by moving up one row and over one column.

My answer:

// KN King Chapter 8
// Programming Projects
// Exercise 17 - Magic Square

#include <stdio.h>

int main() {

    // Introductory message
    printf("This program creates a magic sqaure of a specified size.\n");
    printf("The size must be an odd number between 1 and 99.\n");

    // Get the users magic number and allocate to int n
    int n;
    printf("Enter size of magic square: ");
    scanf("%d", &n);

    // Create the array (not using VLA)
    int magic[99][99];
    int start = (n / 2); // The middle column
    int max = n * n; // The final number
    magic[0][start] = 1; // Place the number one in the middle of row 0

    // Loop to start placing numbers in the magic square
    int row;
    int col;
    int next_row;
    int next_col;
    int i;
    for (i = 2, row = 0, col = start; i < max + 1; i++) {
        if ((row - 1) < 0) { // If going up one will leave the top
            next_row = n - 1; // Go to the bottom row
        }
        else { next_row = row - 1; } // Otherwise go up one
        printf("In row: %d\n", row);

        if ((col + 1) > (n - 1)) { // If column will leave the side
            next_col = 0; // Wrap to first column
            printf("Column will leave side\n");                                   
        }                                                                         
        else { next_col = col + 1; } // Otherwise go over one                     
        printf("In col: %d\n", col);                                              
                                                                              
        if (magic[next_row][next_col] > 0) { // If that position is taken         
            if (row > (n - 1)) { // If going to row below leaves bottom           
                next_row = 0; // Go back to the top                               
            }                                                                     
            else {                                                                
                next_row = row + 1; // Go to the row below                        
                next_col = col; // But stay in same column                        
            }                                                                     
        }                                                                         
        row = next_row;                                                           
        col = next_col;                                                           
        printf("About to put %d in row %d, col %d\n", i, row, col);               
        magic[row][col] = i; // Put the current value in that position            
    }                                                                             
                                                                              
    // Now let's print the array                                                  
    int j;                                                                        
    for (i = 0; i < n; i++) {                                                     
        for (j = 0; j < n; j++) {                                                 
            printf("%4d", magic[i][j]);                                           
        }                                                                         
        printf("\n");                                                             
    }                                                                              
    return 0;                                                                     
}
Tweeted twitter.com/#!/StackCodeReview/status/295664487338496000
Source Link
aussie_aj
  • 153
  • 1
  • 1
  • 4

Magic Square in C (Beginner)

Still teaching myself C out of KN King's C Programming: Modern Approach. Actually pretty excited I go this problem solved in under 2 hours haha. I asked this over on Stack Overflow and it was recommended I post it here.

The exercise is to print a magic square. My solution works but it feels incredibly convoluted. I'm wondering if it actually is? If anyone can help me reorganise the logic in this I'd appreciate it, or am I on the right track? Bear in mind this is Chapter 8, and we've only covered basics like loops, conditional statements, single and 2d arrays etc. I'm open to all constructive feedback.

The question:

Write a program that prints an n x n magic square (a square arrangement of the numebrs 1, 2, ..., n2 in which the sums of the rows, columns and diagonals are all the same). The user will specify the value of n.

Store the magic square in a 2d array. Start by placing the numnber 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ...n2 by moving up one row and over one column.

Any attempt to go outside the bounds of the array should "wrap around" to the opposite side of the array. For example, instead of storing the next number in row -1, we would store it in row n - 1 (the last row). Instead of storing the next number in column n, we would store it in column 0.

If a particular array element is already occupied, put the number directly below the previously stored number. If your compiler supports variable length arrays, declare the array to have n rows and n columns. If not, declare the array to have 99 rows and 99 columns.

My answer: // KN King Chapter 8 // Programming Projects // Exercise 17 - Magic Square

#include <stdio.h>

int main() {

// Introductory message
printf("This program creates a magic sqaure of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");

// Get the users magic number and allocate to int n
int n;
printf("Enter size of magic square: ");
scanf("%d", &n);

// Create the array (not using VLA)
int magic[99][99];
int start = (n / 2); // The middle column
int max = n * n; // The final number
magic[0][start] = 1; // Place the number one in the middle of row 0

// Loop to start placing numbers in the magic square
int row;
int col;
int next_row;
int next_col;
int i;
for (i = 2, row = 0, col = start; i < max + 1; i++) {
    if ((row - 1) < 0) { // If going up one will leave the top
        next_row = n - 1; // Go to the bottom row
    }
    else { next_row = row - 1; } // Otherwise go up one
    printf("In row: %d\n", row);

    if ((col + 1) > (n - 1)) { // If column will leave the side
        next_col = 0; // Wrap to first column
        printf("Column will leave side\n");                                   
    }                                                                         
    else { next_col = col + 1; } // Otherwise go over one                     
    printf("In col: %d\n", col);                                              
                                                                              
    if (magic[next_row][next_col] > 0) { // If that position is taken         
        if (row > (n - 1)) { // If going to row below leaves bottom           
            next_row = 0; // Go back to the top                               
        }                                                                     
        else {                                                                
            next_row = row + 1; // Go to the row below                        
            next_col = col; // But stay in same column                        
        }                                                                     
    }                                                                         
    row = next_row;                                                           
    col = next_col;                                                           
    printf("About to put %d in row %d, col %d\n", i, row, col);               
    magic[row][col] = i; // Put the current value in that position            
}                                                                             
                                                                              
// Now let's print the array                                                  
int j;                                                                        
for (i = 0; i < n; i++) {                                                     
    for (j = 0; j < n; j++) {                                                 
        printf("%4d", magic[i][j]);                                           
    }                                                                         
    printf("\n");                                                             
}                                                                             
return 0;                                                                     
}             

Thanks for looking.