10. Assignment 4
What is magic square?
It is an arrangement of numbers in a square grid.
11. Assignment 4
What is magic square?
It is an arrangement of numbers in a square grid.
Sum of each elements in row wise, column wise and diagonal
wise should be equal.
22. Assignment 4
Example :-
Read ‘n’ from the user.
Lets say, ‘n’ = 3
Check ‘n’ is positive odd or not.
23. Assignment 4
Example :-
Read ‘n’ from the user.
Lets say, ‘n’ = 3
Check ‘n’ is positive odd or not.
Allocate memory dynamically for ‘n’ x ‘n’ matrix.
28. Assignment 4
Example :-
Step2 : Move one row up and one column towards right.
2.1 : If you are in topmost row, jump to bottom.
n
0 1 2
0
1
2
row
col
29. Assignment 4
Example :-
Step2 : Move one row up and one column towards right.
2.1 : If you are in topmost row, jump to bottom.
n
n+1
0 1 2
0
1
2
row
col
30. Assignment 4
Example :-
Step2 : Move one row up and one column towards right
2.1 : If you are in topmost row, jump to bottom
2.2 : If you are in rightmost column, jump to 0th column
n
0 1 2
0
1
2
row
col
31. Assignment 4
Example :-
Step2 : Move one row up and one column towards right
2.1 : If you are in topmost row, jump to bottom
2.2 : If you are in rightmost column, jump to 0th column
n+1
n
0 1 2
0
1
2
row
col
33. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1 n
n+1
Step 2.1
0 1 2
0
1
2
row
col
34. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1
0 1 2
0
1
2
row
col
35. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1
0 1 2
0
1
2
row
col
36. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1
2
0 1 2
0
1
2
row
col
37. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
1
2
0 1 2
0
1
2
row
col
38. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1
2
0 1 2
0
1
2
row
col
39. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1
2
n+1
n
Step 2.2
0 1 2
0
1
2
row
col
40. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1
2
0 1 2
0
1
2
row
col
41. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1
3
2
0 1 2
0
1
2
row
col
42. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
1
3
2
0 1 2
0
1
2
row
col
43. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3
1
3
2
0 1 2
0
1
2
row
col
44. Assignment 4
Example :-
Step3 : If element already present , move one row down and insert.
Then repeat step 2
m
n
0 1 2
0
1
2
row
col
45. Assignment 4
Example :-
Step3 : If element already present , move one row down and insert.
Then repeat step 2
m
n
n+1
0 1 2
0
1
2
row
col
46. Assignment 4
Example :-
Follow step2, move one row up and one column towards right.
1
3
2
0 1 2
0
1
2
row
col
47. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1
3
2
0 1 2
0
1
2
row
col
48. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1
3
4 2
0 1 2
0
1
2
row
col
49. Assignment 4
Example :-
Follow step2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1
3
4 2
0 1 2
0
1
2
row
col
50. Assignment 4
Example :-
Follow step 3 ,move one row down and insert. Then repeat step2.
1
3
4 2
0 1 2
0
1
2
row
col
51. Assignment 4
Example :-
Follow step 3 ,move one row down and insert. Then repeat step2.
1
3
4 2
0 1 2
0
1
2
row
col
52. Assignment 4
Example :-
Follow step 3 ,move one row down and insert. Then repeat step2.
1
3 5
4 2
0 1 2
0
1
2
row
col
53. Assignment 4
Example :-
Follow step 3 ,move one row down and insert. Then repeat step2.
1
3 5
4 2
0 1 2
0
1
2
row
col
54. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
1
3 5
4 2
0 1 2
0
1
2
row
col
55. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
1
3 5
4 2
0 1 2
0
1
2
row
col
56. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
1 6
3 5
4 2
0 1 2
0
1
2
row
col
57. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
1 6
3 5
4 2
0 1 2
0
1
2
row
col
58. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1 6
3 5
4 2
0 1 2
0
1
2
row
col
59. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1 6
3 5
4 2
0 1 2
0
1
2
row
col
60. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
1 6
3 5
4 2
0 1 2
0
1
2
row
col
61. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1 6
3 5
4 2
0 1 2
0
1
2
row
col
62. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1 6
3 5 7
4 2
0 1 2
0
1
2
row
col
63. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here already value is filled. If element present move to
step3.
1 6
3 5 7
4 2
0 1 2
0
1
2
row
col
64. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1 6
3 5 7
4 2
0 1 2
0
1
2
row
col
65. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here exceeds the limit so move to 0th column in the same
row
1 6
3 5 7
4 2
0 1 2
0
1
2
row
col
66. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
1 6
3 5 7
4 2
0 1 2
0
1
2
row
col
67. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
8 1 6
3 5 7
4 2
0 1 2
col
0
1
2
row
68. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
8 1 6
3 5 7
4 2
0 1 2
col
0
1
2
row
69. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
8 1 6
3 5 7
4 2
0 1 2
col
0
1
2
row
70. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
8 1 6
3 5 7
4 2
0 1 2
col
0
1
2
row
71. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
8 1 6
3 5 7
4 2
0 1 2
col
0
1
2
row
72. Assignment 4
Example :-
Follow step 2 , move one row up and one column towards right.
Here if move up then it exceeds the limit. So move to last
row and 1 column towards right
8 1 6
3 5 7
4 9 2
0 1 2
col
0
1
2
row