Team Emertxe
Advanced Pointers,
Arrays and Functions
Assignment 4
Assignment 4
Assignment 4
WAP to generate a n*n magic square.
Assignment 4
WAP to generate a n*n magic square.
Input:
Assignment 4
WAP to generate a n*n magic square.
Input: Read an integer ‘n’.
Assignment 4
WAP to generate a n*n magic square.
Input: Read an integer ‘n’.
Output:
Assignment 4
WAP to generate a n*n magic square.
Input: Read an integer ‘n’.
Output: Print magic square based on ‘n’ no.of rows and columns.
Assignment 4
What is magic square?
Assignment 4
What is magic square?
 It is an arrangement of numbers in a square grid.
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.
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
8 + 3 + 4 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
1 + 5 + 9 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
6 + 7 + 2 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
8 + 1 + 6 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
3 + 5 + 7 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
4 + 9 + 2 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
4 + 5 + 6 = 15
Assignment 4
Example :-
8 1 6
3 5 7
4 9 2
8 + 5 + 2 = 15
Assignment 4
Example :-
 Read ‘n’ from the user.
Assignment 4
Example :-
 Read ‘n’ from the user.
Lets say, ‘n’ = 3
Assignment 4
Example :-
 Read ‘n’ from the user.
Lets say, ‘n’ = 3
 Check ‘n’ is positive odd or not.
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.
Assignment 4
Example :-
Step1 : Start storing number from 0th row middle column.
Assignment 4
Example :-
Step1 : Start storing number from 0th row middle column.
1
0 1 2
0
1
2
row
col
Assignment 4
Example :-
Step2 : Move one row up and one column towards right.
n
0 1 2
0
1
2
row
col
Assignment 4
Example :-
Step2 : Move one row up and one column towards right.
n+1
n
0 1 2
0
1
2
row
col
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
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
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
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
Assignment 4
Example :-
Step2 : Move one row up and one column towards right.
1
0 1 2
0
1
2
row
col
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
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
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
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
Assignment 4
Example :-
 Follow step2 , move one row up and one column towards right.
1
2
0 1 2
0
1
2
row
col
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Assignment 4
Example :-
Step4 : Print the magic square array.
8 1 6
3 5 7
4 9 2
0 1 2
col
0
1
2
row
Sample execution:-
Assignment 4
Sample execution:-
Assignment 4
Sample execution:-
Assignment 4
Assignment 4
Pre-requisites:-
Assignment 4
Pre-requisites:-
 Loops
Assignment 4
Pre-requisites:-
 Loops
 Arrays
Assignment 4
Pre-requisites:-
 Loops
 Arrays
 Pointers
Assignment 4
Pre-requisites:-
 Loops
 Arrays
 Pointers
 Functions
Assignment 4
Pre-requisites:-
 Loops
 Arrays
 Pointers
 Functions
 Dynamic memory allocation
Assignment 4
Pre-requisites:-
 Loops
 Arrays
 Pointers
 Functions
 Dynamic memory allocation
Objective:-
Assignment 4
Pre-requisites:-
 Loops
 Arrays
 Pointers
 Functions
 Dynamic memory allocation
Objective:-
 To understand the concept of Pointers and 2D array.
Team Emertxe
Thank you

04_magic_square.pdf