Chapter 4 Array,Pointer and Structure

Program Source Code 4.1, page no: 100

In [1]:
SIZE=6
sales=[]     #array declared
print 'Please input sales figure for',SIZE,'days' #user input
for i in range(SIZE):
    x=input()
    sales.append(x)
total=0
for j in range(SIZE):
    total+=sales[j]
average=0                       
average=total/SIZE
print 'Average Sales=',average
Please input sales figure for 6 days
352.44
867.70
781.32
867.35
746.21
189.45
Average Sales= 634.078333333

Program Source Code 4.2, page no: 102

In [9]:
b=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]  #2-dimensional array declared

for i in range(len(b)):       #for loop
 for j in range(len(b[i])):
   print 'b[',i,']',
   print '[',j,']=',b[i][j]
b[ 0 ] [ 0 ]= 1
b[ 0 ] [ 1 ]= 2
b[ 0 ] [ 2 ]= 3
b[ 0 ] [ 3 ]= 4
b[ 1 ] [ 0 ]= 5
b[ 1 ] [ 1 ]= 6
b[ 1 ] [ 2 ]= 7
b[ 1 ] [ 3 ]= 8
b[ 2 ] [ 0 ]= 9
b[ 2 ] [ 1 ]= 10
b[ 2 ] [ 2 ]= 11
b[ 2 ] [ 3 ]= 12

Program Source Code 4.3, page no: 104

In [17]:
DISTRICTS=4
MONTHS=3
sales=[[0] * (DISTRICTS +1)for i in range(MONTHS+1)]
for d in range(DISTRICTS):
    for m in range(MONTHS):
        print 'Enter sales for district ',d+1,
        print 'months',m+1
        sales[d][m]=input()
print '                                 Month'
print '                    1                 2                 3'                         
for d in range(DISTRICTS):
    print ''
    print 'District',d+1,
    for m in range(MONTHS):
        print '       ',sales[d][m],
Enter sales for district  1 months 1
3964.23
Enter sales for district  1 months 2
4135.87
Enter sales for district  1 months 3
4397.98
Enter sales for district  2 months 1
867.75
Enter sales for district  2 months 2
923.59
Enter sales for district  2 months 3
1037.01
Enter sales for district  3 months 1
12.77
Enter sales for district  3 months 2
378.32
Enter sales for district  3 months 3
798.22
Enter sales for district  4 months 1
2983.53
Enter sales for district  4 months 2
3903.73
Enter sales for district  4 months 3
9494.98
                                 Month
                    1                 2                 3

District 1         3964.23         4135.87         4397.98 
District 2         867.75         923.59         1037.01 
District 3         12.77         378.32         798.22 
District 4         2983.53         3903.73         9494.98

Program Source Code 4.4, page no: 107

In [2]:
DISRICTS = 4
MONTHS = 3

sales = [[1432.07,234.6,654.01],
         [327,13838.3,12589.9],
         [9328.34,934,4492.3],
         [12838.3,2332.63,32.93]]
        
print '\n'
print '                             month'
print '                 1           2           3',

for d in range(DISRICTS):
    print '\nDistrict',d+1,
    for m in range(MONTHS):
        print '%10.2f' %(sales[d][m]),

                             month
                 1           2           3 
District 1    1432.07     234.60     654.01 
District 2     327.00   13838.30   12589.90 
District 3    9328.34     934.00    4492.30 
District 4   12838.30    2332.63      32.93

Program Source Code 4.5, page no: 109

In [1]:
Var1=11
Var2=22.0
Var3=33
print 'main starts at '
print 'Var1 of type int is located at ',(id(Var1)),
print 'having value= ',Var1
print 'Var1 of type double is located at ',(id(Var2)),
print 'having value= ',Var2
print 'Var1 of type int is located at ',(id(Var3)),
print 'having value= ',Var3
main starts at 
Var1 of type int is located at  31356456 having value=  11
Var1 of type double is located at  47703456 having value=  22.0
Var1 of type int is located at  31355928 having value=  33

Program Source Code 4.6, page no: 114

In [5]:
from ctypes import c_int,pointer
i=c_int(6)
j=c_int(7)

ptr=pointer(i)
ptr[0]=10    #*ptr=10

ptr=pointer(j)
ptr[0]=20   #*ptr=20

print 'i=',i.value,'j=',j.value
i= 10 j= 20

Program Source Code 4.6a, page no: 115

In [13]:
from ctypes import c_int,pointer

#variable declaration
i=c_int(6)
j=c_int(7)


ptri=pointer(i)   #*ptri=&i
ptrj=pointer(j)
ptri[0]=10
ptrj[0]=ptri[0]
ptrj=ptri
ptrj[0]=20
print 'i=',i.value,'j=',j.value
i= 20 j= 10

Program Source Code 4.7, page no: 117

In [14]:
from ctypes import c_int,pointer
i=c_int(6)
ptri=pointer(i)    
pptri=pointer(ptri)     #**pptri

print 'i=',i.value
print '*ptri=',ptri[0]
print '**pptri=',pptri[0][0]
i= 6
*ptri= 6
**pptri= 6

Program Source Code 4.8, page no: 121

In [11]:
from ctypes import  POINTER,c_char,c_short,pointer
iarr=[65,66,67,68]      #array declared
ptri=iarr


ptrc=POINTER(c_char)
ptrs=POINTER(c_short)

  #type case int to char
for i in range(4):                 #for loop
    print 'ptri[',i,']=',ptri[i]       #getting the pointer value
print ''
for i in range(4):
    ptrc=pointer(c_char(chr(ptri[i]))) 
    print 'ptrc[',i,']=',ptrc[0]        #getting the pointer value
print ''

for i in range(4):
    ptrc=pointer(c_short(iarr[i])) 
    print 'ptrs[',i,']=',ptrc[0]            #getting the pointer value
print ''
ptri[ 0 ]= 65
ptri[ 1 ]= 66
ptri[ 2 ]= 67
ptri[ 3 ]= 68

ptrc[ 0 ]= A
ptrc[ 1 ]= B
ptrc[ 2 ]= C
ptrc[ 3 ]= D

ptrs[ 0 ]= 65
ptrs[ 1 ]= 66
ptrs[ 2 ]= 67
ptrs[ 3 ]= 68

Program Source Code 4.9, page no: 123

In [7]:
DateLabel=['Date of birth','Date of anniversary']    #array defined
class DATE:       #class date
    day=None
    month=None
    year=None
    
DateOfBirth=DATE
DateOfBirth.day=07
DateOfBirth.month=12
DateOfBirth.year=1966

print 'Date of Birth',':',         #printing the characteristics
print DateOfBirth.day,'/',
print DateOfBirth.month,'/',
print DateOfBirth.year

DatesOfAnniversary=DATE
DatesOfAnniversary.day=11
DatesOfAnniversary.month=07
DatesOfAnniversary.year=1993

print 'Date Of Anniversary',':',     #printing the characteristics
print DatesOfAnniversary.day,'/',
print DatesOfAnniversary.month,'/',
print DatesOfAnniversary.year
Date of Birth : 7 / 12 / 1966
Date Of Anniversary : 11 / 7 / 1993

Program Source Code 4.10, page no: 124

In [2]:
DateLabel=['Date of birth','Date of anniversary']    #array declared
class DATE:
    day=None
    month=None
    year=None
    
Dates=[DATE() for j in range(2)]
Dates[0].day=7
Dates[0].month=12
Dates[0].year=1966
Dates[1].day=11
Dates[1].month=7
Dates[1].year=1993

for i in range(2):
    print DateLabel[i],':',Dates[i].day,'/',Dates[i].month,'/',Dates[i].year    #printing the characteristics
 Date of birth : 7 / 12 / 1966
Date of anniversary : 11 / 7 / 1993

Program Source Code 4.11, page no: 125

In [6]:
DateLabel=['Date of birth','Date of anniversary']     #array declared

class DATE:            #class date
    day=None
    month=None
    year=None

DateOfBirth=DATE
DatesOfAnniversary=DATE

pDates1=DATE()


pDates1.day=07
pDates1.month=12
pDates1.year=1966

print DateLabel[0],':',      #printing the characteristics
print pDates1.day,'/',
print pDates1.month,'/',
print pDates1.year

pDates2=DATE()
pDates2.day=11
pDates2.month=07
pDates2.year=1993

print DateLabel[1],':',   #printing the characteristics
print pDates2.day,'/',
print pDates2.month,'/',
print pDates2.year
Date of birth : 7 / 12 / 1966
Date of anniversary : 11 / 7 / 1993

Program Source Code 4.12, page no: 126

In [6]:
class DATE:
    day=None
    month=None
    year=None

class PERSON:
    name=None
    DateOfBirth=DATE
    
aPerson=PERSON()
print 'Please input the name of the person: '#user input
aPerson.name=raw_input()
print 'Please input the date of birth of the person: '#user input
aPerson.DateOfBirth.day=input()
aPerson.DateOfBirth.month=input()
aPerson.DateOfBirth.year=input()

print 'We have got one person whose: '
print 'name is: ',aPerson.name
print 'Date of Birth is: ',aPerson.DateOfBirth.day,'/',aPerson.DateOfBirth.month,'/',aPerson.DateOfBirth.year   #printing the characteristics
Please input the name of the person: 
Soumen
Please input the date of birth of the person: 
5
6
1991
We have got one person whose: 
name is:  Soumen
Date of Birth is:  5 / 6 / 1991
In [ ]: