Chapter 9: More on Pointers

Example 9.1, Page number: 318

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

Example 9.2, Page number: 320

In [2]:
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[0] is 7f55de486b90 
The value of a[0][0] is 7f55de4db368 
The value of a[0][1] is 7f55de48f050 
The value of a[0][2] is 7f55de4db3f8 
The value of a[0][3] is 7f55de4c7908 

The value of a[1] is 7f55de477320 
The value of a[1][0] is 7f55de4ff3f8 
The value of a[1][1] is 7f55de477290 
The value of a[1][2] is 7f55de4773b0 
The value of a[1][3] is 7f55de477368 

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

Example 9.3, Page number: 326

In [1]:
# Function declaration, calculation and result
def readdata (a, num) :
	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)]
readdata (a, n)
print
printdata (a, n)
Enter the number of elements 3
Enter value in cell[3] 30
Enter value in cell[2] 20
Enter value in cell[1] 10

Value in cell[3] is 30 
Value in cell[2] is 20 
Value in cell[1] is 10 

Example 9.4, Page number: 328

In [2]:
# Function declaration, calculation and result
def readdata (a, 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 = int(raw_input('Enter the number of elements '))
a = [0 for i in xrange(n)]
readdata (a, n)
print
printdata (a, n)
Enter the number of elements 5
Enter value in cell[5] 1
Enter value in cell[4] 2
Enter value in cell[3] 3
Enter value in cell[2] 4
Enter value in cell[1] 5

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

Example 9.5, Page number: 330

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

def makearray (n) :
	n = int(raw_input('Enter the number of elements '))	
	return n
	
def readdata (a, 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)]
readdata (a, n)
print
printdata (a, n)
Enter the number of elements 4
Enter value in cell[4] 400
Enter value in cell[3] 300
Enter value in cell[2] 200
Enter value in cell[1] 100

Value in cell[4] is 400 
Value in cell[3] is 300 
Value in cell[2] is 200 
Value in cell[1] is 100 

Example 9.6, Page number: 335

In [4]:
# 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 [5]:
# Function declaration, calculation and result
def readdata (a, n) :
	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 = [0] * n
a = readdata (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 [0] 46
Enter data in cell [1] 75
Enter data in cell [2] 68
Enter data in cell [3] 34
Enter data in cell [4] 49

The sorted data is 
The data in cell [0] is 34 
The data in cell [1] is 46 
The data in cell [2] is 49 
The data in cell [3] is 68 
The data in cell [4] is 75 

Example 9.8, Page number: 341

In [6]:
# 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 [7]:
# 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 [8]:
# 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 [9]:
# Function declaration, calculation and result
def readdata (a, n) :
	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 = [0] * 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[1] zaheer
Enter string[2] ashita
Enter string[3] tamanna
Enter string[4] purti

The sorted data is 
The data in cell [1] is ashita 
The data in cell [2] is purti 
The data in cell [3] is tamanna 
The data in cell [4] is zaheer