(Homework Solution): C++    Magic Square A magic square of order n is an arrangement of n2 numbers, usually distinct integers,

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…

Expert Answer

answers

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:

DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0, Program: :NTURBOC3 BIN>TC C NTURB0C3 Projects MAGICS 1.CPP Press C to Continue or Q to Quit: C Please enter the number 9 You have entered 9 The Magic Square for n-9 Sum of each row or columnm 369: 35 25 15 5 76 66 56 46 45 24 14 4 75 65 55 54 44 34 13 3 74 64 63 53 4333 23 2 73 72 62 52 42 32 22 12 81 71 61 51 41 31 21 11 1 70 60 50 40 30 20 10 9 80 59 49 39 29 19 18 8 79 69 48 38 28 27 1?7 7868 58 37 36 26 16 6 77 67 57 47 Press C to Continue or Q to Quit:

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

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 many cases quite ahead of 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.

24/7 Customer Support

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?

Follow these simple steps to get your paper done

Place your order

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

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

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.

Essays

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.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents