0

Alright so I thought my code was working and it seems to do so, it allows the user to choose the size of a magic square where the number one is the start point and starts in the center of the first row. The pattern goes along this line....go up one and over one, if you go up above the first row.....move back to the last row or if you run off the end of the right side of the column than go back to the start of the column...in a magic square if your not familiar with it, all sides are equal when the numbers on that side or diagonal are counted.

An odd number must be entered for this to be written out as a magic square (example: 3x3, 5x5, 7x7, etc..) the problem is it works until I enter 11x11....when done, it comes back around and when the program runs into a slot that has already been filled it is supposed to enter the next number below the last one that was entered into the array...but when 11x11 is entered it overwrites the 1 with a 13 which breaks the cycle and ruins the pattern....I would appreciate it if someone helped me with this, I think maybe the problem has to do with the equation I use to choose the starting point. This works all the way up to 11x11, every odd number entered after that seems to overwrite the starting point.

    // Chapter 8 Programming Project #17

    #include <stdio.h>

    #define N_squared (N * N)
    #define MOVE (--row, ++column)
    #define RW_SIZE ((int) (sizeof(magic_square) / sizeof(magic_square[0])))

    void create_magic_square(int N, int magic_square[N][N], int ROW_SIZE);
    void print_magic_square(int N, int magic_square[N][N], int ROW_SIZE);

    int main(void)
    {
        int N, row, column;

        printf("This program creates a magic square of a specified size\n");
        printf("The size must be an odd number between 1 and 99.\n");
        printf("Enter size of magic square: ");
        scanf("%d", &N);
        int magic_square[N][N];
        for (row = 0; row < N; row++) {
            for (column = 0; column < N; column++) {
                magic_square[row][column] = 0;
            }
        }
        // Create magic square
        create_magic_square(N, magic_square, RW_SIZE);
        // Print magic square
        print_magic_square(N, magic_square, RW_SIZE);

        return 0;
    }

    void create_magic_square(int N, int magic_square[N][N], int ROW_SIZE)
    {
        printf("Size of N*N = %d\nSize of ROW_SIZE = %d\n", N_squared, ROW_SIZE);
        // Here I iterate through the numbers, rows, and columns
        int i = 1, row = 0;
        int column = (((ROW_SIZE + 1) / 2) - 1);

        while (i != N_squared + 1){
            // if new position is empty place next number
            if (magic_square[row][column] == 0) {
                magic_square[row][column] = i;
                i++;
            // If new position is filled then move back and down
            } else if (row + 2 < ROW_SIZE &&
                       column - 1 >= 0) {
                row += 2;
                column -= 1;
            } else if (row + 2 > ROW_SIZE - 1 &&
                       column - 1 < 0) {
                row = 1;
                column = ROW_SIZE - 1;
            }
            // If current position has been set then move
            if (magic_square[row][column] != 0)
                MOVE;
            // If row runs off the board reset
            if (row < 0)
                row = ROW_SIZE - 1;
            // if column runs off the board reset
            if (column > ROW_SIZE - 1)
                column = 0;
        }
    }

    void print_magic_square(int N, int magic_square[N][N], int ROW_SIZE)
    {
        int row, column;
        printf("\n");
        for (row = 0; row < ROW_SIZE; row++) {
            for (column = 0; column < ROW_SIZE; column++) {
                if (N > 9)
                    printf(" %3d ", magic_square[row][column]);
                else
                    printf(" %2d ", magic_square[row][column]);
            }
            printf("\n\n");
        }
    }
Matt
  • 74,352
  • 26
  • 153
  • 180
John Conner
  • 233
  • 1
  • 4
  • 18
  • Thanks for all the help lol....really appreciate all these comments ^^ lol XD – John Conner Apr 21 '14 at 06:21
  • Obviously I'll just have to figure this one out myself, must be to hard for anyone else to figure out....I'll do it after I have ran through the rest of these programming projects and tomorrow I'll do it and post it up for whoever else gets stuck with a similar problem, and no this isn't schoolwork, this is me learning to program for personal reasons. – John Conner Apr 21 '14 at 09:06

0 Answers0