Chapter 11: Pointers

Program 11.1, Page number: 237

In [1]:
def main():

        #Variable Declaration
        count=10
        
        int_ptr=count
        x=int_ptr
        
        #Result
        print("count={0} , x={1}".format(count,x))

if __name__=='__main__':
        main()
count=10 , x=10

Program 11.2, Page number: 238

In [2]:
def main():

        #Variable Declaration
        c='Q'
        char_pointer=c
        print("{0} {1}".format(c,char_pointer))

        c='/'
        char_pointer=c
        print("{0} {1}".format(c,char_pointer))

        char_pointer='('
        c=char_pointer
        print("{0} {1}".format(c,char_pointer))



if __name__=='__main__':
        main()
Q Q
/ /
( (

Program 11.3, Page number: 239

In [3]:
def main():

        #Variable Declaration
        i1=5
        p1=i1
        i2=p1/2+10
        p2=p1

        #Result
        print("i1 = {0}, i2 = {1}, p1 = {2}, p2 = {3}\n".format(i1, i2, p1, p2))

if __name__=='__main__':
        main()
i1 = 5, i2 = 12, p1 = 5, p2 = 5

Program 11.4, Page number: 242

In [4]:
def main():
        class date:
                def __init__(self):             #Class constructor
                        self.month=0
                        self.day=0
                        self.year=0

        #Creating instances
        today=date()
        datePtr=today

        datePtr.month=9
        datePtr.day=25
        datePtr.year=2004

        #Result
        print("Today's date is {0}/{1}/{2}".format(datePtr.month, datePtr.day, datePtr.year))

if __name__=='__main__':
        main()
Today's date is 9/25/2004

Program 11.5, Page number: 243

In [5]:
def main():
        class intPtrs:
                def __init__(self):             #Class constructor
                        self.p1=0
                        self.p2=0

        #Variable Declarations & Instance creation
        i1=100
        pointers=intPtrs()
        pointers.p1=i1
        i2=-97
        pointers.p2=i2

        #Result
        print("i1 = {0}, pointers.p1 = {1}\n".format(i1, pointers.p1))
        print("i2 = {0}, pointers.p2 = {1}\n".format(i2, pointers.p2))


if __name__=='__main__':
        main()
i1 = 100, pointers.p1 = 100

i2 = -97, pointers.p2 = -97

Program 11.6, Page number: 246

In [6]:
def main():
        class entry:
                def __init(self):                       #Class constructor
                        self.value=0
                        self.nxt=0

        #Variable declarations & instance creation
        n1=entry()
        n1.value=100
        n2=entry()
        n2.value=200
        n3=entry()
        n3.value=300

        n1.nxt=n2
        n2.nxt=n3

        i=n1.nxt.value

        #Result
        print("{0}".format(i))
        print("{0}".format(n2.nxt.value))



if __name__=='__main__':
        main()
200
300

Program 11.7, Page number: 250

In [7]:
def main():
        class entry:
                def __init__(self):                    #Class constructor
                        self.value=0
                        self.nxt=0

        #Creating class instances
        n1=entry()
        n2=entry()
        n3=entry()
        list_pointer=n1

        #Variable Declaration
        n1.value=100
        n2.value=200
        n3.value=300
        n1.nxt=n2
        n2.nxt=n3
        n3.nxt=0

        while(list_pointer!=0):                         #Calculation
                print("{0}\n".format(list_pointer.value))
                list_pointer = list_pointer.nxt;


if __name__=='__main__':
        main()
100

200

300

Program 11.8, Page number: 254

In [8]:
def test(int_pointer):
        int_pointer=100
        return int_pointer

def main():

        #Variable declaration
        i=50
        p=i
        print("Before the call to test i = {0}".format(p))
       
        p=test(i)                                           #Function call

        print("After the call to test i = {0}".format(p))


if __name__=='__main__':
        main()
Before the call to test i = 50
After the call to test i = 100

Program 11.9, Page number: 255

In [9]:
def exchange(pint1,pint2):
         temp=pint1
         pint1=pint2
         pint2=temp
         return pint1,pint2


def main():

        #Variable Declaration
        i1=-5
        i2=66
        p1=i1
        p2=i2

        print("i1 ={0}, i2 ={1}\n".format(i1, i2))
        
        i1,i2=exchange(p1,p2)                     #Fucntion call-1

        print("i1 ={0}, i2 ={1}\n".format(i1, i2))

        i1,i2=exchange(i1,i2)                     #Fucntion call-2

        print("i1 ={0}, i2 ={1}\n".format(i1, i2))


if __name__=='__main__':
        main()
i1 =-5, i2 =66

i1 =66, i2 =-5

i1 =-5, i2 =66

Program 11.10, Page number: 257

In [10]:
class entry:
        def __init__(self):                     #Class constructor
                self.value=0
                selfnxt=0

        def FindEntry(listPtr,match):           #Function to traverse list

                #Calculation
                while(listPtr!=0):
                        if(int(listPtr.value)==int(match)):
                                return listPtr
                        else:
                                listPtr=listPtr.nxt

                return 0

def main():

        #Creating instance
        n1=entry()
        n2=entry()
        n3=entry()
        listPtr=entry()

        #Variable declaration
        n1.value=100
        n2.value=200
        n3.value=300

        n1.nxt=n2
        n2.nxt=n3
        n3.nxt=0
        listStart=n1
        
        print("Enter value to locate: ")
        search=200                                       #search=raw_input()
        listPtr = entry.FindEntry (listStart, search)    #Function call

        #Result
        if ( listPtr != 0 ):
                print("Found {0}.\n".format(listPtr.value))
        else:
                print("Not found.\n")


if __name__=='__main__':
        main()
Enter value to locate: 
Found 200.

Program 11.11, Page number: 262

In [11]:
def arraySum (array,n):

        #Variable Declaration
        sum = 0 
        arrayEnd = n
        #Calculation
        for i in range(0,arrayEnd):
                 sum += array[i]
        return sum

def main():

        #List Declaration
        values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]
        #Result
        print("The sum is {0}\n".format(arraySum (values, 10)))

if __name__=='__main__':
        main()
The sum is 21

Program 11.12, Page number: 265

In [12]:
def arraySum (array,n):
        #Variable Declaration
        sum = 0 
        #Calculation
        for i in array:
                 sum += i
        return sum

def main():

        #List Declaration
        values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]
        #Result
        print("The sum is {0}\n".format(arraySum(values,10)))

if __name__=='__main__':
        main()
The sum is 21

Program 11.13, Page number: 266

In [13]:
def copyString(to, frm):
        global string2
        to=frm
        string2=to

def main():
       
        global string2
        #String declaration
        string1="A string to be copied"
        string2=""
        copyString(string2,string1)                          #Functon call

        print("{0}\n".format(string2))
        copyString (string2, "So is this.")                  #Function call
        print("{0}\n".format(string2))

if __name__=='__main__':
        main()
A string to be copied

So is this.

Program 11.14, Page number: 271

In [14]:
def copyString(to, frm):
        global string2
        to=frm
        string2=to

def main():
       
        global string2
        #String declaration
        string1="A string to be copied"
        string2=""
        copyString(string2,string1)                          #Functon call

        print("{0}\n".format(string2))
        copyString (string2, "So is this.")                  #Function call
        print("{0}\n".format(string2))

if __name__=='__main__':
        main()
A string to be copied

So is this.

Program 11.15, Page number: 272

In [15]:
def stringLength(string):
        return len(string)                      #Calculation

def main():
        print("{0} ".format(stringLength ("stringLength test")))
        print("{0} ".format(stringLength ("")))
        print("{0}".format(stringLength ("complete")))

if __name__=='__main__':
        main()
17 
0 
8