Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Magic Square A magic square of order n is an arrangement of n 2 numbers, usually distinct integers, in a square, such that the

C++

Magic Square

A magic square of order n is an arrangement of n2 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.

Write a C++ program that can create and display magic squares of any size, which will be entered at the runtime. The size could be varying from 3 to 15.

Allow your program to run continuously as often as the user wishes to test with the other sizes. Before ending the program, your name as the programmer must be displayed.

Note: with the same size n entered at repeated runs, there should be different squares displayed; That meant if the same square was always displayed for the same input size, it is wrong.

For references: http://en.wikipedia.org/wiki/Magic_square

Sample

~-~-~-~-~-~-~-~-~-~-~-~-~

Magic Square Generator

~-~-~-~-~-~-~-~-~-~-~-~-~

Enter desired size of magic square (3 - 15) or 0 to end: gj

ERROR! Invalid Value!

Enter desired size of magic square (3 - 15) or 0 to end: 5

With size 5, the Magic Number is 65

-------------------------

| 2| 22| 10| 19| 12|

-------------------------

| 25| 1| 17| 16| 6|

-------------------------

| 5| 20| 21| 8| 11|

-------------------------

| 24| 7| 3| 18| 13|

-------------------------

| 9| 15| 14| 4| 23|

-------------------------

Press key to continue...

Enter desired size of magic square (3 - 15) or 0 to end: 15

With size 15, the Magic Number is 1695

---------------------------------------------------------------------

| 11| 42|119| 14| 47| 16|152| 41|221|213| 83|210|224|206| 96|

---------------------------------------------------------------------

|192| 79| 73|216| 80| 36|135|201| 81| 43| 94|208|134| 3|120|

---------------------------------------------------------------------

|109|185| 22|222| 53|167| 12| 46| 61|165|181| 62| 75|155|180|

---------------------------------------------------------------------

|186| 44|102| 48|223|127|108|153|144| 38| 98|168| 64| 19|173|

---------------------------------------------------------------------

|169|145|191|197| 8|189|124|122| 92| 52| 99| 57| 35|195| 20|

---------------------------------------------------------------------

|115| 45|103|184| 29|214| 74| 65| 87|176|163| 68|170| 30|172|

---------------------------------------------------------------------

| 24|143|150|198|211|154| 50|100| 49| 67| 37|182| 33|166|131|

---------------------------------------------------------------------

|101|142|117|218| 85|118| 54|146|179| 27| 69| 23|136|203| 77|

---------------------------------------------------------------------

| 91|161|113|125|156| 58|202| 60| 32|162|121|188| 59|160| 7|

---------------------------------------------------------------------

|159| 66| 71|148|139| 2|157| 84| 90|217|111| 78|187|110| 76|

---------------------------------------------------------------------

|171|220| 39| 5| 88|177| 1|112|215| 25|147|128|133| 51|183|

---------------------------------------------------------------------

|196| 40| 26| 10|116|200|225|164| 89|194|104|105| 31|132| 63|

---------------------------------------------------------------------

| 13|174|158| 72|190| 93| 86| 56|199| 70|107| 55|193|123|106|

---------------------------------------------------------------------

Press key to continue...

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions