# C++    Magic Square A magic square of order n is an arrangement of n2 numbers, usually distinct integers,…

C++

Magic Square

Don't use plagiarized sources. Get Your Custom Essay on
C++    Magic Square A magic square of order n is an arrangement of n2 numbers, usually distinct integers,…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!

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…

here we have written program to calculate magic square. in which following inputs are required

1. Press ‘C’ to continue or ‘Q’ to quit the repeatation.

2. Enter the number for which you want magic square

————————————————————————–

CODE:

// author: rajdeep-paliwal

#include<stdio.h>

#include<string.h>

#include<iostream.h>

#include<stdlib.h>

// we wrote a function to generate odd sized magic squares

void generateSquare(int n)

{

int magicSquare[15][15];

// firstly set all slots as 0

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

// Initialize position for 1

int i = n/2;

int j = n-1;

// here we put all values one by one in magic square

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

{

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

{

j = n-2;

i = 0;

}

else

{

// here we took 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 magic square

printf(“n The Magic Square for n=%d:n Sum of ”

“each row or column %d:nn”, n, n*(n*n+1)/2);

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

{

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

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

printf(“n”);

}

}

// main function to operate other function

int main()

{

int n = 7; // Works only when n is odd. set 7 as default.

char c;

while(1)

{

cout << “nn Press C to Continue or Q to Quit: “;

cin >> c;

if(c == ‘Q’)

{

break;

}

cout << “nn Please enter the number: “;

cin >> n;

if(n%2 == 0)

{

cout << ” Please enter odd values only. Try again!”;

}

else

{

cout << “n You have entered ” << n;

generateSquare (n);

}

}

return 0;

}

—————————————————————————————–

OUTPUT:

Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.