Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Write a program to construct an n x n magic square for any odd value of n. The problem will require a double indexed

C++

Write a program to construct an n x n magic square for any odd value of n. The problem will require a double indexed array and integer pair to keep track of the current position on the grid. You should also have a method for testing that the matrix created is correct by adding the columns and rows to ensure they all have the same sum.

A magic square is an n x n matrix in which each of the integers 1, 2, 3, ...n2 appears exactly once and all column sums, row sums, and diagonal sums are equal.

The following is a procedure for constructing a n x n magic square for any odd integer n. Place 1 in the middle of the top row. Then, after integer k has been placed, move up one row and one column to the right to place the next integer k+1, unless one of the following occurs:

- If a move takes you above the top row in the jth column, move to the bottom of the jth column and place k+1 there.

- If a move takes you outside to the right of the square in the ith row, place k+1 in the ith row at the left side.

-If a move takes you to an already filled square or if you move out of the square at the upper right hand corner, place k+1 immediately below k.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago