A magic square is a square where all rows columns and diagon

A “magic square” is a square where all rows, columns and diagonals add up to the same value. (See: http://en.wikipedia.org/wiki/Magic_square). The method we will use to create a magic square is under the section called “Method for constructing a magic square of odd order” (An example is provided in this section). A formula for this method is provided below (Note: this only works for odd sized grids):

Step 1: Insert the value 1 anywhere in the grid.

Step 2: (a)If the square up and to the right is already filled with a number, then put the next integer (counting up) in the square directly below the current position. (b) Otherwise, go one square up and to the right and put in the next integer (counting up). If you go off the grid, wrap around to the bottom and/or left.

Step 3: Go to step 2 until the whole grid is filled.

Part 1:: Write a method to check and see if the square to the up and right is currently occupied (as described in Step 2). This should return true if the square is already filled in with a number, and otherwise return false if the square is blank. (Note: you may need to provide arguments to this method, depending on how you wrote your code.)

Part 2:: Write code to actually fill in the grid (Step 2 and 3) and then display the grid. You may start your initial value 1 (Step 1) anywhere you like. The size of your grid can either be defined in your code, or you can use Scanner to ask the user for the size they want.

Part 3:: Rewrite the portion of your magic square code where you filled in the array (Step 2, 3 and 4) using only two lines of code. One line is a (for) loop statement and the other is an assignment statement. You may not use methods in either line of code. You may not simply condense your code to fit on one line (you can only use 2 semicolons for the for loop and one semicolon for the assignment statement). Your array must follow the formula explained before part 1 (numbers must increase going up and to the right).

Solution

This is to create an odd magic square here:

include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class magicSquare
{
public:
    magicSquare()

{ square = 0; }
    ~magicSquare()

{ if( square )

delete [] square; }

    void create_square( int p )
    {
        if( square ) delete [] square;
        if( !( p & 1 ) ) p++; sz = p;
        square = new int[sz * sz];
        memset( square, 0, sz * sz * sizeof( int ) );
        filltheSquare();
    }

    void displaysquare()
    {
        cout << \"This is an Odd Magic Square: \" << sz << \" x \" << sz << \"\ \";
        cout << \"The Magic Sum is: \" << magicNumber() << \"\ \ \";
        ostringstream cvr; cvr << sz * sz;
        int length = cvr.str().size();

        for( int y = 0; y < sz; y++ )
        {
            int yy = y * sz;
           for( int x = 0; x < sz; x++ )
               cout << setw( length + 2 ) << square[yy + x];

            cout << \"\ \";
        }
        cout << \"\ \ \";
    }

private:
    void filltheSquare()
    {
        int sx = sz / 2, sy = 0, m = 0;
        while( m < sz * sz )
        {
            if( !square[sx + sy * sz] )
            {
               square[sx + sy * sz]= m + 1;
               increment( sx ); decrement( sy );
               m++;
            }
            else
            {
               decrement( sx ); increment( sy ); increment( sy );
            }
        }
    }

    int magicNumber()
    { return sz * ( ( sz * sz ) + 1 ) / 2; }

    void increment( int& o )
    { if( ++o == sz ) o = 0; }

    void decrement( int& o )
    { if( --o < 0 ) o = sz - 1; }

    bool checkPos( int x, int y )
    { return( isInside( x ) && isInside( y ) && !square[sz * y + x] ); }

    bool isInside( int s )
    { return ( s < sz && s > -1 ); }

    int* squar;
    int sz;
};

int main( int argc, char* argv[] )
{
    magicSquare n;
  n.create_square( 5 );
    n.displaysquare();
    return 0;
}

A “magic square” is a square where all rows, columns and diagonals add up to the same value. (See: http://en.wikipedia.org/wiki/Magic_square). The method we wil
A “magic square” is a square where all rows, columns and diagonals add up to the same value. (See: http://en.wikipedia.org/wiki/Magic_square). The method we wil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site