# Chapter 9: More on Pointers¶

## Example 9.1, Page number: 318¶

In :
from array import *

# Variable declaration
a = [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25]]

# Calculation and result
print ('The value of a is %x ' % id(a))

for i in range (0, 5) :
print ('The value of a[%d] is %x ' % (i, id(i)))

for i in range (0, 5) :
for j in range (0, 5) :
print ('The cell [%d][%d] i.e. %x has %d ' % (i, j, id(a[i][j]), a[i][j]))

The value of a is 7f55dc3e08c0
The value of a is 18861b0
The value of a is 1886198
The value of a is 1886180
The value of a is 1886168
The value of a is 1886150
The cell  i.e. 1886198 has 1
The cell  i.e. 1886180 has 2
The cell  i.e. 1886168 has 3
The cell  i.e. 1886150 has 4
The cell  i.e. 1886138 has 5
The cell  i.e. 1886120 has 6
The cell  i.e. 1886108 has 7
The cell  i.e. 18860f0 has 8
The cell  i.e. 18860d8 has 9
The cell  i.e. 18860c0 has 10
The cell  i.e. 18860a8 has 11
The cell  i.e. 1886090 has 12
The cell  i.e. 1886078 has 13
The cell  i.e. 1886060 has 14
The cell  i.e. 1886048 has 15
The cell  i.e. 1886030 has 16
The cell  i.e. 1886018 has 17
The cell  i.e. 1886000 has 18
The cell  i.e. 1885fe8 has 19
The cell  i.e. 1885fd0 has 20
The cell  i.e. 1885fb8 has 21
The cell  i.e. 1885fa0 has 22
The cell  i.e. 1885f88 has 23
The cell  i.e. 1885f70 has 24
The cell  i.e. 1885f58 has 25


## Example 9.2, Page number: 320¶

In :
from array import *

# Variable declaration
a = [[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]],
[[21,22,23,24,25], [26,27,28,29,30], [31,32,33,34,35], [36,37,38,39,40]]]

# Calculation and result
print ('The value of a is %x ' % id(a))

for i in range (0, 2) :
print ('The value of a[%d] is %x ' % (i, id(a[i])))
for j in range (0, 4) :
print ('The value of a[%d][%d] is %x ' % (i, j, id(a[i][j])))
print

for i in range (0, 2) :
for j in range (0, 4) :
for k in range (0, 5) :
print ('The cell [%d][%d][%d] i.e. %x has %d ' % (i, j, k, id(a[i][j][k]), a[i][j][k]))

The value of a is 7f55de4772d8
The value of a is 7f55de486b90
The value of a is 7f55de4db368
The value of a is 7f55de48f050
The value of a is 7f55de4db3f8
The value of a is 7f55de4c7908

The value of a is 7f55de477320
The value of a is 7f55de4ff3f8
The value of a is 7f55de477290
The value of a is 7f55de4773b0
The value of a is 7f55de477368

The cell  i.e. 1886198 has 1
The cell  i.e. 1886180 has 2
The cell  i.e. 1886168 has 3
The cell  i.e. 1886150 has 4
The cell  i.e. 1886138 has 5
The cell  i.e. 1886120 has 6
The cell  i.e. 1886108 has 7
The cell  i.e. 18860f0 has 8
The cell  i.e. 18860d8 has 9
The cell  i.e. 18860c0 has 10
The cell  i.e. 18860a8 has 11
The cell  i.e. 1886090 has 12
The cell  i.e. 1886078 has 13
The cell  i.e. 1886060 has 14
The cell  i.e. 1886048 has 15
The cell  i.e. 1886030 has 16
The cell  i.e. 1886018 has 17
The cell  i.e. 1886000 has 18
The cell  i.e. 1885fe8 has 19
The cell  i.e. 1885fd0 has 20
The cell  i.e. 1885fb8 has 21
The cell  i.e. 1885fa0 has 22
The cell  i.e. 1885f88 has 23
The cell  i.e. 1885f70 has 24
The cell  i.e. 1885f58 has 25
The cell  i.e. 1885f40 has 26
The cell  i.e. 1885f28 has 27
The cell  i.e. 1885f10 has 28
The cell  i.e. 1885ef8 has 29
The cell  i.e. 1885ee0 has 30
The cell  i.e. 1885ec8 has 31
The cell  i.e. 1885eb0 has 32
The cell  i.e. 1885e98 has 33
The cell  i.e. 1885e80 has 34
The cell  i.e. 1885e68 has 35
The cell  i.e. 1886618 has 36
The cell  i.e. 1886600 has 37
The cell  i.e. 18865e8 has 38
The cell  i.e. 18865d0 has 39
The cell  i.e. 18865b8 has 40


## Example 9.3, Page number: 326¶

In :
# Function declaration, calculation and result
while num > 0 :
a[num-1] = int(raw_input('Enter value in cell[%d] ' % num))
num = num - 1

def printdata (a, num) :
while num > 0 :
print ('Value in cell[%d] is %d ' % (num, a[num-1]))
num = num - 1

n = int(raw_input('Enter the number of elements '))
a = [0 for i in xrange(n)]
print
printdata (a, n)

Enter the number of elements 3
Enter value in cell 30
Enter value in cell 20
Enter value in cell 10

Value in cell is 30
Value in cell is 20
Value in cell is 10


## Example 9.4, Page number: 328¶

In :
# Function declaration, calculation and result
while n > 0 :
a[n-1] = int(raw_input('Enter value in cell[%d] ' % n))
n = n - 1

def printdata (a, n) :
while n > 0 :
print ('Value in cell[%d] is %d ' % (n, a[n-1]))
n = n - 1

n = int(raw_input('Enter the number of elements '))
a = [0 for i in xrange(n)]
print
printdata (a, n)

Enter the number of elements 5
Enter value in cell 1
Enter value in cell 2
Enter value in cell 3
Enter value in cell 4
Enter value in cell 5

Value in cell is 1
Value in cell is 2
Value in cell is 3
Value in cell is 4
Value in cell is 5


## Example 9.5, Page number: 330¶

In :
# Function declaration, calculation and result
global n
n = 0

def makearray (n) :
n = int(raw_input('Enter the number of elements '))
return n

while n > 0 :
a[n-1] = int(raw_input('Enter value in cell[%d] ' % n))
n = n - 1

def printdata (a, n) :
while n > 0 :
print ('Value in cell[%d] is %d ' % (n, a[n-1]))
n = n - 1

n = makearray (n)
a = [0 for i in xrange(n)]
print
printdata (a, n)

Enter the number of elements 4
Enter value in cell 400
Enter value in cell 300
Enter value in cell 200
Enter value in cell 100

Value in cell is 400
Value in cell is 300
Value in cell is 200
Value in cell is 100


## Example 9.6, Page number: 335¶

In :
# Variable declaration, calculation and result
string = raw_input('Enter the string: ')

rev_string = reversed(string)

if list(string) == list(rev_string) :
print ('%s is a PALINDROME ' % string)
else :
print ('%s is not a PALINDROME ' % string)

Enter the string: malayalam
malayalam is a PALINDROME


## Example 9.7, Page number: 337¶

In :
# Function declaration, calculation and result
for i in range (0, n) :
a[i] = int(raw_input('Enter data in cell [%d] ' % i))
return a

def bubble (a, n) :
for p in range (0, n) :
for i in range (0, n-1-p) :
if a[i] > a[i+1] :
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
return a

def printdata (a, n) :
for i in range (0, n) :
print ('The data in cell [%d] is %d ' % (i, a[i]))

n = int(raw_input('Enter the number of elements to be entered: '))
a =  * n
a = bubble (a, n)
print ('\nThe sorted data is ')
printdata (a, n)

Enter the number of elements to be entered: 5
Enter data in cell  46
Enter data in cell  75
Enter data in cell  68
Enter data in cell  34
Enter data in cell  49

The sorted data is
The data in cell  is 34
The data in cell  is 46
The data in cell  is 49
The data in cell  is 68
The data in cell  is 75


## Example 9.8, Page number: 341¶

In :
# Variable declaration, calculation and result
i = 6
c = 'a'

the_data = i
print ('the_data points to the integer value %d ' % the_data)

the_data = c
print ('the_data points to the character value %c ' % the_data)

the_data points to the integer value 6
the_data points to the character value a


## Example 9.9, Page number: 343¶

In :
# Variable declaration
marks = [[0 for x in xrange(5)] for x in xrange(5)]
avg = 0
percent = [0 for x in xrange(5)]

# Function declaration, calculation and result
def readncompute (marks, percent, avg) :
no_of_students = int(raw_input('How many students marks do you want to enter? '))
for i in range (0, no_of_students) :
print ('\nEnter marks of 5 subjects for roll no. %d ' % (i+1))
for j in range (0, 5) :
marks[i][j] = int(raw_input('Subject %d: ' % (j+1)))
percent[i] = percent[i] + marks[i][j]

percent[i] = percent[i]/5
avg = avg + percent[i]
return avg/no_of_students, no_of_students

def printdata (score, per, avg, no_of_students) :
for i in range (0, no_of_students) :
print ('\nMarks for roll no. %d are ' % (i+1))
for j in range (0, 5) :
print ('%d ' % score[i][j])
print ('Percentage for roll no. is %.2f ' % per[i])
if per[i] < avg :
print ('Below average')
else :
print ('Above average')

average, nos = readncompute(marks, percent, avg)
print ('\nThe average marks are %.2f' % average)
printdata (marks, percent, average, nos)

How many students marks do you want to enter? 2

Enter marks of 5 subjects for roll no. 1
Subject 1: 65
Subject 2: 76
Subject 3: 54
Subject 4: 83
Subject 5: 94

Enter marks of 5 subjects for roll no. 2
Subject 1: 84
Subject 2: 62
Subject 3: 94
Subject 4: 73
Subject 5: 62

The average marks are 74.00

Marks for roll no. 1 are
65
76
54
83
94
Percentage for roll no. is 74.00
Above average

Marks for roll no. 2 are
84
62
94
73
62
Percentage for roll no. is 75.00
Above average


## Example 9.10, Page number: 349¶

In :
# Variable declaration
marks = [[0 for x in xrange(5)] for x in xrange(5)]
avg = 0
percent = [0 for x in xrange(5)]

# Function declaration, calculation and result
def readncompute (marks, percent, avg) :
no_of_students = int(raw_input('How many students marks do you want to enter? '))
for i in range (0, no_of_students) :
print ('\nEnter marks of 5 subjects for roll no. %d ' % (i+1))
for j in range (0, 5) :
marks[i][j] = int(raw_input('Subject %d: ' % (j+1)))
percent[i] = percent[i] + marks[i][j]

percent[i] = percent[i]/5
avg = avg + percent[i]
return avg/no_of_students, no_of_students

def printdata (score, per, avg, no_of_students) :
for i in range (0, no_of_students) :
print ('\nMarks for roll no. %d are ' % (i+1))
for j in range (0, 5) :
print ('%d ' % score[i][j])
print ('Percentage for roll no. is %.2f ' % per[i])
if per[i] < avg :
print ('Below average')
else :
print ('Above average')

average, nos = readncompute(marks, percent, avg)
print ('\nThe average marks are %.2f' % average)
printdata (marks, percent, average, nos)

How many students marks do you want to enter? 2

Enter marks of 5 subjects for roll no. 1
Subject 1: 65
Subject 2: 76
Subject 3: 54
Subject 4: 32
Subject 5: 89

Enter marks of 5 subjects for roll no. 2
Subject 1: 76
Subject 2: 86
Subject 3: 43
Subject 4: 32
Subject 5: 21

The average marks are 57.00

Marks for roll no. 1 are
65
76
54
32
89
Percentage for roll no. is 63.00
Above average

Marks for roll no. 2 are
76
86
43
32
21
Percentage for roll no. is 51.00
Below average


## Example 9.11, Page number: 351¶

In :
# Function declaration, calculation and result
for i in range (0, n) :
a[i] = int(raw_input('Enter data in cell [%d] ' % i))
return a

def bubble (name) :
for p in range (0, 4) :
for i in range (0, 3-p) :
if name[i] > name[i+1] :
temp = name[i]
name[i] = name[i+1]
name[i+1] = temp
return name

def printdata (name) :
for i in range (0, 4) :
print ('The data in cell [%d] is %s ' % ((i+1), name[i]))

name =  * 4
for i in range (0, 4) :
name[i] = raw_input('Enter string[%d] ' % (i+1))

name = bubble (name)
print ('\nThe sorted data is ')
printdata (name)

Enter string zaheer
Enter string ashita
Enter string tamanna
Enter string purti

The sorted data is
The data in cell  is ashita
The data in cell  is purti
The data in cell  is tamanna
The data in cell  is zaheer