using visual studio C++ to create the code for magic squares.
Engr 123
Assignment 06
Assigned: March 13, 2017
Due: March 27, 2017
Reminder: This is a programming project, and work on this assignment should be done
individually. Assistance from other students is limited to questions about specific issues
as noted in the syllabus.
Magic Squares
A magic square is a 2D grid of integers in which the sum of the integers in any row,
column, or along the two major diagonals is the same. For example, Figure 1 shows a 3 x
3 magic square where the sum is 15.
8 1 6
3 5 7
4 9 2
Figure 1
A 3 x 3 magic square in which the sum of the rows, columns, or major diagonals is 15.
There are a number of different algorithms for creating your own magic squares. (See
Wikipedia at https://en.wikipedia.org/wiki/Magic_square). The algorithm we will use for
this assignment creates a magic square of n x n where n is odd.
Place a 1 in the top row center box.
Move one space up and one space right for the next number. If that space is
outside the grid wrap it around. If that space is occupied move one row down
from the last number.
Continue filling in numbers sequentially until all squares are filled in.
Here is an example of the algorithm for a 3 x 3 grid.
3
2
2
1
1
1
3
3
2
2
4
1,4
5
1
5
6
1
5
6
3
3
4
3
4
2
4
2
2
7
8
9
8
1
1
5
1 6
8
3 5 7
3
4,7 2
4
Figure 2
Creating a 3 x 3 magic square.
6
7
2
8
3
4
5
2
6
7
9
For this assignment create a GUI application that allows the user to enter the size of the
magic square. The size must be an odd integer in the range 1 to 25. If the user’s number
is not an odd integer in this range, you should issue the appropriate error message and ask
Engr 123
Spring, 2017
Dynamic Labels
In some cases when creating a GUI interface, the number of labels, textboxes, or other controls
may be dependent on the data in which case they need to be created dynamically as the program
runs. The code below is a button click event which creates 8 labels, assigns them properties, and
places them on the form for an application. Each label has a text property which is set to an
integer 0 to 7.
private void btnLabels_Click(object sender, EventArgs e)
{int i, n. 8;
Label[] dLabel – new Label[n];
for(i=0;i