Chapter 8: Arrays

Average Marks, Page number: 272

In [2]:
#Variable declaration
s = 0
marks = [] # array declaration

#for i in range(0,30):
 #   marks.append(int(raw_input("Enter marks: " ))) # store data in array
marks = [89,85,57,25,90,45,87,48,98,12,39,66,75,30,87,100,5,78,56,99,84,0,39,79,93,61,87,45,90,56]   
print "Enter marks: "
for i in range(0,30):
    print marks[i]

for i in range(0,30):
    s = s + marks[i] # read data from array

#Calculation
avg = s / 30 #Average formula

#Result
print "Average marks = ", avg 
Enter marks: 
89
85
57
25
90
45
87
48
98
12
39
66
75
30
87
100
5
78
56
99
84
0
39
79
93
61
87
45
90
56
Average marks =  63

Call By Value, Page number: 277

In [3]:
#Funcion definition
def display(m):
    print m 
    
#Variable declaration
marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array

for i in range(0,7):
    display(marks[i]) #function call 
55
65
75
56
78
78
90

Call By Reference , Page number: 278

In [7]:
#Funcion definition
def display(n):
     print n #return
    
#Variable declaration
marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array

for i in range(0,7):
    display(marks[i]) #function call
55
65
75
56
78
78
90

Pointer Arithmetic, Page number: 279

In [8]:
#Variable declaration
i = 3
j = 1.5
k = 'c'

print "Value of i = ", i 
print  "Value of j = ", j 
print  "Value of k = ", k 

#addresses of the variables
x = id(i)
y = id(j)
z = id(k)

print  "Original address in x = ", x 
print  "Original address in y = ", y 
print  "Original address in z = ", z 

x += 2
y += 4
z += 1

print  "New address in x = ", x 
print "New address in y = ", y 
print "New address in z = ", z 
Value of i =  3
Value of j =  1.5
Value of k =  c
Original address in x =  32529512
Original address in y =  105587440
Original address in z =  32744192
New address in x =  32529514
New address in y =  105587444
New address in z =  32744193

Pointer Subtraction , Page number: 281

In [1]:
#Variable declaration
arr = [ 10, 20, 30, 45, 67, 56, 74 ]
i = id(arr[1]) #address 
j = id(arr[5]) #Address

#Result
print  j - i, arr[5] - arr[1] 
1128 36

Pointer Comparison, Page number: 282

In [10]:
#Variable declaration
arr = [ 10, 20, 36, 72, 45, 36 ]
j = id(arr[ 4 ])
k = id( arr[0 + 4] )

#Result 
if j == k : #comparison
    print  "The two pointers point to the same location" 
else:
    print  "The two pointers do not point to the same location" 
The two pointers point to the same location

Memory Locations, Page number: 283

In [11]:
#Variable declaration
num = [ 24, 34, 12, 44, 56, 17]

#Result
for i in range(0,6):
    print  "element no. ", i 
    print  "address = ", id(num[i])  
element no.  0
address =  32529008
element no.  1
address =  32528768
element no.  2
address =  32529296
element no.  3
address =  32530520
element no.  4
address =  32530232
element no.  5
address =  32529176

Accessing Array Elements , Page number: 284

In [12]:
#Variable declaration
num = [ 24, 34, 12, 44, 56, 17]

#Result
for i in range(0,6):
    print  "address = ", id(num[i]) 
    print  "element =  ", num[i] 
address =  32529008
element =   24
address =  32528768
element =   34
address =  32529296
element =   12
address =  32530520
element =   44
address =  32530232
element =   56
address =  32529176
element =   17

Accessing Array Elements Using Pointers, Page number: 284

In [13]:
#Variable declaration
num = [ 24, 34, 12, 44, 56, 17]
j = id(num[0]) # assign address of zeroth element

#Result
for i in range(0,6):
    print "address = ", j 
    print "element = ", num[i] 
    j = id(num[(i+1)%6]) # increment pointer to point to next location 
address =  32529008
element =  24
address =  32528768
element =  34
address =  32529296
element =  12
address =  32530520
element =  44
address =  32530232
element =  56
address =  32529176
element =  17

Passing An Array to a Function , Page number: 286

In [14]:
#Variable declaration
num = [ 24, 34, 12, 44, 56, 17 ]

#Function definition:
def display ( j,n ):
    for i in range(0,n):
        print "element = ", j
        j = num[(i+1)%n] #increment pointer to point to next element 

display ( num[0], 6 ) #function call
element =  24
element =  34
element =  12
element =  44
element =  56
element =  17

Accessing Array Elements in Different Ways, Page number: 288

In [15]:
#Variable declaration
num = [ 24, 34, 12, 44, 56, 17]

for i in range(0,6):
    print "address = ", id(num[i])
    print "element = ", num[i], num[(0+ i)]
    print num[(i+0)], num[i] 
address =  32529008
element =  24 24
24 24
address =  32528768
element =  34 34
34 34
address =  32529296
element =  12 12
12 12
address =  32530520
element =  44 44
44 44
address =  32530232
element =  56 56
56 56
address =  32529176
element =  17 17
17 17

Two Dimensional Array , Page number: 289

In [19]:
#Variable declaration
stud = [] #array

#for i in range(0,4):
 #   stud.append((raw_input("Enter roll no and marks: ").split())) # storing data in the 2D array

stud = ["1 38","2 78","3 93","4 48"]
for i in range (0,4):
    print "Enter roll no and marks: "
    print stud[i]
    
#Result
for i in range(0,4):
    print stud[i][0],stud[i][1:] 
Enter roll no and marks: 
1 38
Enter roll no and marks: 
2 78
Enter roll no and marks: 
3 93
Enter roll no and marks: 
4 48
1  38
2  78
3  93
4  48

2-D Array Demo , Page number: 293

In [21]:
#Variable declaration
s = [ [1234, 56 ], [ 1212, 33], [ 1434, 80 ], [ 1312, 78 ]]

#Result
for i in range(0,4):
    print  "Address of %d th 1-D array = %u"%( i, id(s[i]) )
Address of 0 th 1-D array = 134002248
Address of 1 th 1-D array = 134654472
Address of 2 th 1-D array = 134791816
Address of 3 th 1-D array = 134792008

Accessing 2-D Array Elements, Page number: 295

In [22]:
#Variable declaration
s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ] ]

for i in range(0,4):
    for j in range(0,2):
        print  s[i][j] 
1234
56
1212
33
1434
80
1312
78

Pointer To An Array , Page number: 296

In [23]:
#Variable declaration
s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ]]

#Result
for i in range(0,4):
    p = s[i]
    pint = p
    print "\n" 
    for j in range(0,2):
        print pint[j]  

1234
56


1212
33


1434
80


1312
78

Passing 2-D Array To a Function , Page number: 297

In [25]:
#Variable declaration
a = [[1, 2, 3, 4] , [5, 6, 7, 8] , [9, 0, 1, 6 ]]

#Function definitions
def display ( q, row, col ):
    for i in range(0,row):
        for j in range(0,col):
            print q [ (i * col)%3][j]
        print "\n" 
    print "\n" 


def show ( q, row, col ):
    for i in range(0,row):
        p = q[i]
        for j in range(0,col):
            print  p[j]
        print "\n" 
    print  "\n" 

def Print ( q, row, col ):
    for i in range(0,row):
        for j in range(0,col):
            print  q[i][j]
        print "\n" 
    print  "\n" 
    
#function calls
display ( a, 3, 4 ) 
show ( a, 3, 4 ) 
Print ( a, 3, 4 )
1
2
3
4


5
6
7
8


9
0
1
6




1
2
3
4


5
6
7
8


9
0
1
6




1
2
3
4


5
6
7
8


9
0
1
6




Array of Pointers , Page number: 300

In [26]:
from ctypes import *

#Variable declaration
arr = [] # array of integer pointers 
i = c_int(31)
j = c_int(5)
k = c_int(19)
l = c_int(71)
arr.append(pointer(i))
arr.append(pointer(j))
arr.append(pointer(k))
arr.append(pointer(l))

for m in range(0,4):
    print  arr[m].contents 
c_long(31)
c_long(5)
c_long(19)
c_long(71)

Address Array , Page number: 301

In [29]:
#Variable declaration
a = [ 0, 1, 2, 3, 4 ]
p = [ a[0], a[1], a[2], a[3], a[4] ]

#Result
print  p, id(p), id(id(p ) )
[0, 1, 2, 3, 4] 134794120 134005648
In [ ]: