0% found this document useful (0 votes)
7 views87 pages

Arrays Safe

Uploaded by

M. Murali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views87 pages

Arrays Safe

Uploaded by

M. Murali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 87

Pillayar Koil street

House House House House House House House House House House

#1 #2 #3 #4 #5 #6 #7 #8 #9 #10
Data types
• An atomic data type

– variables of that type can store only one value at a time

• E.g., char, int, float

• A Molecular data type

– one in which each data item is a collection of data items

• .e.g. Array
Arrays
• Array
– a collection of a fixed number of components
wherein all of the components have the same
behavior, so obvious the same data type
• The general form :
dataType arrayName[intExp];
where intExp is any expression that evaluates
to a positive integer
Declaring an array
• The statement

int num_arr[ 5];


// declares an array num_arr made of 5 components
of the type int
(i.e., tells compiler to reserve 5 elements)
• The components are num_arr[0], num_arr [1],
num_arr [2], num_arr [3], num_arr [4]
Arrays – Few Examples

• Vowels { 'a', 'e', 'i', 'o','u'}


• Days in the months {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31}
• Days of the week {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",“ Saturday"}
•Months of the year
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OC
T","NOV","DEC"}
Accessing Array Components
• The general form (syntax) of
accessing an array element
arrayName[indexExp]
where indexExp, called index, is any expression whose value
is a positive integer
• Index value specifies the position of
the component in the array
• The [ ] is an operator
• The array index always starts at 0
[ ] - operator
• It is a Binary operator
• Binary operator – requires two operands
• One operand is an address and other
operand is an integer i.e. address[integer]
• As we can write in two ways, (a + b) as well
as (b + a) , which will compute to the same
value,
address[integer] and integer[address]
will compute to the same result.
Address Reference

65510 num_arr [0]

65512 num_arr [1]

65514 num_arr [2]

65516 num_arr [3]

65518 num_arr [4]


ctr
0 ( 0 + 1) * 10 = 10
Address Reference

65510 10 num_arr[0]

65512 num_arr [1]

65514 num_arr [2]

65516 num_arr [3]

65518 num_arr [4]

for (ctr = 0; ctr < 5; ctr = ctr + 1)


num_arr[ctr] = (ctr + 1) * 10
ctr
1 ( 1 + 1) * 10 = 20
Address Reference
num_arr[0]
65510 10
65512 20 num_arr [1]

65514 num_arr [2]

65516 num_arr [3]

65518 num_arr [4]

for (ctr = 0; ctr < 5; ctr = ctr + 1)


num_arr[ctr] = (ctr + 1) * 10
ctr
2 ( 2 + 1) * 10 = 30
Address Reference

65510 10 num_arr[0]

65512 20 num_arr [1]

65514 30 num_arr [2]

65516 num_arr [3]

65518 num_arr [4]

for (ctr = 0; ctr < 5; ctr = ctr + 1)


num_arr[ctr] = (ctr + 1) * 10
ctr
3 ( 3 + 1) * 10 = 40
Address Reference

65510 10 num_arr[0]

65512 20 num_arr [1]

65514 30 num_arr [2]

65516 40 num_arr [3]

65518 num_arr [4]

for (ctr = 0; ctr < 5; ctr = ctr + 1)


num_arr[ctr] = (ctr + 1) * 10
ctr
4 ( 4 + 1) * 10 = 50
Address Reference

65510 10 num_arr[0]

65512 20 num_arr [1]

65514 30 num_arr [2]

65516 40 num_arr [3]

65518 50 num_arr [4]

for (ctr = 0; ctr < 5; ctr = ctr + 1)


num_arr[ctr] = (ctr + 1) * 10
[] - revisited
• Name of the array yields an address i.e. Num will
give us 65510
• [] operator works like this – base address +
(index * size of individual element) i.e.
num_arr[3] will be treated as 65510 + (3 x 2) =
65516
• num_arr[3] is the same as 3[num_arr]
• num_arr[3] can be also written as *(num_arr+3) ,
which can obviously written as *(3+num_arr)
16
Array Initialization

• As with simple variables ,Arrays can be initialized


while they are being declared
• No need to specify the size of the array
• Size of array is determined by the number of initial
values in the braces
• For example:
• double sales[] = {12.25, 32.50, 16.90,
23,45.68};
Partial Initialization
• The statement int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0]= 5, list[1]=6, and list[2]=3
• The statement int list[25]= {4, 7};
declares list to be an array of 25 components
The first two components are initialized to 4 and 7
respectively
All other components are initialized to 0
Partial Initialization: 1
• The statement int list[10] = {0};
declares list to be an array of 10 components
and initializes all components to zero
• The statement int list[10] = {8, 5, 12};
initializes list[0] to 8, list[1] to 5, list[2] to 12
all other components are initialized to 0
F I N D M I N D \0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
F I N D \0 I N D \0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 8 F I N D M I N D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 5 F I N D M I N D
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
‘C’ Strings (Character Arrays): 4
• The statement
char name[16] = “My College";

declares a string variable name of


length 16 and stores “My College" in it
• The statement
char name[] = “My College";

declares a string variable name of


length 11 and stores “My College" in it
0 1 2 3 4

0 1 2 3 4 5
1 11 12 13 14 15
2 21 22 23 24 25
3 31 32 33 34 35
4 41 42 43 44 45
5 51 52 53 54 55

int num_arr [6] [5] ;


Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
0 1 2 3 4 5 6 7
0 0 1 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0
2 0 0 0 0 0 1 0 0
3 0 0 0 0 0 0 0 1
4 0 0 1 0 0 0 0 0
5 1 0 0 0 0 0 0 0
6 0 0 0 0 0 0 1 0
7 0 0 0 0 1 0 0 0
0 1 2 3 4 5 6 7
0 0 1 0 0 0 0 0 0
0 1
1 0 0 0 1 0 0 0 0 1 3
2 0 0 0 0 0 1 0 0 2 5
3 0 0 0 0 0 0 0 1 3 7
4 0 0 1 0 0 0 0 0 4 2
5 1 0 0 0 0 0 0 0 5 0
6 0 0 0 0 0 0 1 0 6 6
0 0 0 0 1 0 0 0
7 4
7
0 1
1 3
2 5
3 7
4 2
5 0
6
6
7
4
queen_position
0 -1
1 -1
2 -1
3 -1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
1 -1
2 -1
3 -1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
2 -1
3 -1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
3 -1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 3
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 3
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 3
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 7
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 7
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
Q 4 7
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
3 -1
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
4 -1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
Q 4 1
5 -1
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
Q 4 1
Q 5 3
6
-1
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
Q 4 1
Q 5 3
Q 6
5
7
-1
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
Q 4 1
Q 5 3
Q 6
5
7
-1
• Draw the Chess Board
• Place Queen in all 64 locations
• Remove Queens from all 64 locations
• How to check safety of the queens
• Backtrack
• Continue to record all the solutions
• Remove the duplicate solutions
Draw Chess Board
• ⌐−−−−−−−, every box has 7 horizontal lines to
accommodate SPQUEENSP
• Number of columns required is (8*7) + 9 = 65
• Every box will be 3 lines in height
• Number of Columns required is (8*2) + 9 = 25
• Let us draw the box now
Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen


Place Queens – 64 locations
• Calculate the row and col positions and place Queen
on all places of the board
• Use for loop (0 → 7) inside another for loop (0 → 7)
• Place the text “Queen”
• Note down all the row , col positioning
• Convert the code into a function
• void place_queen(int row, int col)
• “QUEEN” need to placed in 9,3..17,3..25,3……
9,6..17,6..25,6…….
Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen

Queen Queen Queen Queen Queen Queen Queen Queen


Remove Queens – 64 locations
• Calculate the row and col positions and place Queen
on all places of the board
• Use for loop (0 → 7) inside another for loop (0 → 7)
• Place the text “ ”
• Note down all the row , col positioning
• Convert the code into a function
• void remove_queen(int row, int col)
• Erasing need to happen in 9,3..17,3..25,3……
9,6..17,6..25,6…….
Left Diagonal UP
Right Diagonal

Q
Left Diagonal
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7

IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7

3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7

To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7

3 3 Queen

2 2 5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7

1 1
0 0 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7

-1 -1
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
UP
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7

IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7

3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7

To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7

3 4 Queen

2 4 5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7

1 4
0 4 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7

-1 4
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
Right Diagonal
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7

IS SAFE
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7
Row Col
4 4
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7

3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7

To Be checked
Row Col 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7

3 5 Queen

2 6 5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7

1 7
0 8 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7

-
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
queen_position
Q 0 0
Q 1 2
Q 2 4
Q 3 6
4 -1
5 -1
6
-1
7
-1
Backtrack
• The next column to check for availability is the
last placed column + 1
• Update the array by placing -1
• Remove the QUEEN from the screen
• BACKTRACKING MIGHT REQUIRE AN
IMMEDIATE BACKTRACKING, SO PLACE THE
BACKTRACKING CODE TWICE….NOT MORE
THAN TWO TIMES BACKTRACKING CAN HAPPEN
Save the Solution

• Save it in a file or a Linked


List or in another two
dimensional array 
Check for Duplication

• 90 180 , 270 rotation


0, 0 0

will yield the same result


• Mirror image /
Reflection _

You might also like