Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make a flow chart and pseudocode a function called oddMyMagic that inputs a scal

ID: 3805330 • Letter: M

Question

Make a flow chart and pseudocode a function called oddMyMagic that inputs a scalar n and outputs a n-by-n magic square array. A magic square is an arrangement of the numbers 1 to n^2 in an n-by-n array with each number occuring exactly once such that the sum of the elements of any row, any column, or either of the diagonals is the same.

For example if you type in

A = oddMyMagic(3)

a 3-by-3 matrix is created where the sum of all squares in every direction is 15.

Problem 3: Make a flow chart and pseudocode then code the corresponding function called oddMyMagic that inputs a scalar n and outputs an n- n magic square array. A magic square is an arrangement of the numbers 1 to n? in an n-by-n array with each number occuring exactly once such that the sum of the elements of any row, any column, or either of the diagonals is the same. The figure below shows a magic square for n 3. Per Wikipedia there exists a quick, elegant 2 7 6 15 9 5 1 15 4 3 81 15 15 15 15 15 15 Figure 2: A 3-by-3 magic square in which any row, column, or diagonal sums to 15. method to construct the magic square when n is odd. In the central column of the first row, place the number 1. After that, the fundamental movement for filling the square is diagonally up and right, one step at a time. If a filled square is encountered, one moves vertically down one square instead, then continues as before. When an "up-and-to-the-right" move would leave the square, it is wrapped around to the last row and first column, respectively. This is demonstrated in the figure below step 3 step 4 step 22 step 1 3 21 2 Move up and to the right. Moving up and to the right Place 1 in the first row, Move up and to the right. In this case, up is off the board in this case, right is off the board encounters a filled space. center column the movement is wrapped Move down instead. the movement is wrapped around to the left. around to the bottom step 9 step 8 step 5 step 7 step 65 816 38 1 6 3 5 7 3 5 7 479 2 4 2 4 2 4 2 Move up and to the right. Move up and to the right Moving up and to the right Move up and to the right. In this case, right is off the board In this case, up is off the board encounters a filled space The movement is wrapped (after wrapping around). The movement is wrapped around to the bottom. Move down instead. around to the left Figure 3: The Siamese method for solving the magic square when n is odd.

Explanation / Answer

Magic Square

A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2.

The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the following value:
M = n(n^2+1)/2

For normal magic squares of order n = 3, 4, 5, …, the magic constants are: 15, 34, 65, 111, 175, 260, …

In this post, we will discuss how programmatically we can generate a magic square of size n. Before we go further, consider the below examples:

Three conditions hold:

1. The position of next number is calculated by decrementing row number of previous number by 1, and incrementing the column number of previous number by 1. At any time, if the calculated row position becomes -1, it will wrap around to n-1. Similarly, if the calculated column position becomes n, it will wrap around to 0.

2. If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1.

3. If the calculated row position is -1 & calculated column position is n, the new position would be: (0, n-2).

Based on the above approach, following is the working code:

#include<stdio.h>

#include<string.h>

// A function to generate odd sized <a href="#">magic square</a>s

void generateSquare(int n)

{

    int magicSquare[n][n];

    // set all slots as 0

    memset(magicSquare, 0, sizeof(magicSquare));

    // Initialize position for 1

    int i = n/2;

    int j = n-1;

    // One by one put all values in <a href="#">magic square</a>

    for (int num=1; num <= n*n; )

    {

        if (i==-1 && j==n) //3rd condition

        {

            j = n-2;

            i = 0;

        }

        else

        {

            //1st condition helper if next number goes to out of square's right side

            if (j == n)

                j = 0;

            //1st condition helper if next number is goes to out of square's upper side

            if (i < 0)

                i=n-1;

        }

        if (magicSquare[i][j]) //2nd condition

        {

            j -= 2;

            i++;

            continue;

        }

        else

            magicSquare[i][j] = num++; //set number

        j++; i--; //1st condition

    }

    // print <a href="#">magic square</a>

    printf("The <a href="#">Magic Square</a> for n=%d: Sum of each row or column %d: ",

            n, n*(n*n+1)/2);

    for(i=0; i<n; i++)

    {

        for(j=0; j<n; j++)

            printf("%3d ", magicSquare[i][j]);

        printf(" ");

    }

}

// Driver program to test above function

int main()

{

    int n = 7; // Works only when n is odd

    generateSquare (n);

    return 0;

}

Run on IDE

Output:

#include<stdio.h>

#include<string.h>

// A function to generate odd sized <a href="#">magic square</a>s

void generateSquare(int n)

{

    int magicSquare[n][n];

    // set all slots as 0

    memset(magicSquare, 0, sizeof(magicSquare));

    // Initialize position for 1

    int i = n/2;

    int j = n-1;

    // One by one put all values in <a href="#">magic square</a>

    for (int num=1; num <= n*n; )

    {

        if (i==-1 && j==n) //3rd condition

        {

            j = n-2;

            i = 0;

        }

        else

        {

            //1st condition helper if next number goes to out of square's right side

            if (j == n)

                j = 0;

            //1st condition helper if next number is goes to out of square's upper side

            if (i < 0)

                i=n-1;

        }

        if (magicSquare[i][j]) //2nd condition

        {

            j -= 2;

            i++;

            continue;

        }

        else

            magicSquare[i][j] = num++; //set number

        j++; i--; //1st condition

    }

    // print <a href="#">magic square</a>

    printf("The <a href="#">Magic Square</a> for n=%d: Sum of each row or column %d: ",

            n, n*(n*n+1)/2);

    for(i=0; i<n; i++)

    {

        for(j=0; j<n; j++)

            printf("%3d ", magicSquare[i][j]);

        printf(" ");

    }

}

// Driver program to test above function

int main()

{

    int n = 7; // Works only when n is odd

    generateSquare (n);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote