{ "metadata": { "name": "Chapter XI" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 11: Pointers" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.1, Page number: 237" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #Variable Declaration\n", " count=10\n", " \n", " int_ptr=count\n", " x=int_ptr\n", " \n", " #Result\n", " print(\"count={0} , x={1}\".format(count,x))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "count=10 , x=10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.2, Page number: 238" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #Variable Declaration\n", " c='Q'\n", " char_pointer=c\n", " print(\"{0} {1}\".format(c,char_pointer))\n", "\n", " c='/'\n", " char_pointer=c\n", " print(\"{0} {1}\".format(c,char_pointer))\n", "\n", " char_pointer='('\n", " c=char_pointer\n", " print(\"{0} {1}\".format(c,char_pointer))\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Q Q\n", "/ /\n", "( (\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.3, Page number: 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", "\n", " #Variable Declaration\n", " i1=5\n", " p1=i1\n", " i2=p1/2+10\n", " p2=p1\n", "\n", " #Result\n", " print(\"i1 = {0}, i2 = {1}, p1 = {2}, p2 = {3}\\n\".format(i1, i2, p1, p2))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i1 = 5, i2 = 12, p1 = 5, p2 = 5\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.4, Page number: 242" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", " class date:\n", " def __init__(self): #Class constructor\n", " self.month=0\n", " self.day=0\n", " self.year=0\n", "\n", " #Creating instances\n", " today=date()\n", " datePtr=today\n", "\n", " datePtr.month=9\n", " datePtr.day=25\n", " datePtr.year=2004\n", "\n", " #Result\n", " print(\"Today's date is {0}/{1}/{2}\".format(datePtr.month, datePtr.day, datePtr.year))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Today's date is 9/25/2004\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.5, Page number: 243" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", " class intPtrs:\n", " def __init__(self): #Class constructor\n", " self.p1=0\n", " self.p2=0\n", "\n", " #Variable Declarations & Instance creation\n", " i1=100\n", " pointers=intPtrs()\n", " pointers.p1=i1\n", " i2=-97\n", " pointers.p2=i2\n", "\n", " #Result\n", " print(\"i1 = {0}, pointers.p1 = {1}\\n\".format(i1, pointers.p1))\n", " print(\"i2 = {0}, pointers.p2 = {1}\\n\".format(i2, pointers.p2))\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i1 = 100, pointers.p1 = 100\n", "\n", "i2 = -97, pointers.p2 = -97\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.6, Page number: 246" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def main():\n", " class entry:\n", " def __init(self): #Class constructor\n", " self.value=0\n", " self.nxt=0\n", "\n", " #Variable declarations & instance creation\n", " n1=entry()\n", " n1.value=100\n", " n2=entry()\n", " n2.value=200\n", " n3=entry()\n", " n3.value=300\n", "\n", " n1.nxt=n2\n", " n2.nxt=n3\n", "\n", " i=n1.nxt.value\n", "\n", " #Result\n", " print(\"{0}\".format(i))\n", " print(\"{0}\".format(n2.nxt.value))\n", "\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "200\n", "300\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.7, Page number: 250" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", " class entry:\n", " def __init__(self): #Class constructor\n", " self.value=0\n", " self.nxt=0\n", "\n", " #Creating class instances\n", " n1=entry()\n", " n2=entry()\n", " n3=entry()\n", " list_pointer=n1\n", "\n", " #Variable Declaration\n", " n1.value=100\n", " n2.value=200\n", " n3.value=300\n", " n1.nxt=n2\n", " n2.nxt=n3\n", " n3.nxt=0\n", "\n", " while(list_pointer!=0): #Calculation\n", " print(\"{0}\\n\".format(list_pointer.value))\n", " list_pointer = list_pointer.nxt;\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100\n", "\n", "200\n", "\n", "300\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.8, Page number: 254" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def test(int_pointer):\n", " int_pointer=100\n", " return int_pointer\n", "\n", "def main():\n", "\n", " #Variable declaration\n", " i=50\n", " p=i\n", " print(\"Before the call to test i = {0}\".format(p))\n", " \n", " p=test(i) #Function call\n", "\n", " print(\"After the call to test i = {0}\".format(p))\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before the call to test i = 50\n", "After the call to test i = 100\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.9, Page number: 255" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def exchange(pint1,pint2):\n", " temp=pint1\n", " pint1=pint2\n", " pint2=temp\n", " return pint1,pint2\n", "\n", "\n", "def main():\n", "\n", " #Variable Declaration\n", " i1=-5\n", " i2=66\n", " p1=i1\n", " p2=i2\n", "\n", " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", " \n", " i1,i2=exchange(p1,p2) #Fucntion call-1\n", "\n", " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", "\n", " i1,i2=exchange(i1,i2) #Fucntion call-2\n", "\n", " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i1 =-5, i2 =66\n", "\n", "i1 =66, i2 =-5\n", "\n", "i1 =-5, i2 =66\n", "\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.10, Page number: 257" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class entry:\n", " def __init__(self): #Class constructor\n", " self.value=0\n", " selfnxt=0\n", "\n", " def FindEntry(listPtr,match): #Function to traverse list\n", "\n", " #Calculation\n", " while(listPtr!=0):\n", " if(int(listPtr.value)==int(match)):\n", " return listPtr\n", " else:\n", " listPtr=listPtr.nxt\n", "\n", " return 0\n", "\n", "def main():\n", "\n", " #Creating instance\n", " n1=entry()\n", " n2=entry()\n", " n3=entry()\n", " listPtr=entry()\n", "\n", " #Variable declaration\n", " n1.value=100\n", " n2.value=200\n", " n3.value=300\n", "\n", " n1.nxt=n2\n", " n2.nxt=n3\n", " n3.nxt=0\n", " listStart=n1\n", " \n", " print(\"Enter value to locate: \")\n", " search=200 #search=raw_input()\n", " listPtr = entry.FindEntry (listStart, search) #Function call\n", "\n", " #Result\n", " if ( listPtr != 0 ):\n", " print(\"Found {0}.\\n\".format(listPtr.value))\n", " else:\n", " print(\"Not found.\\n\")\n", "\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter value to locate: \n", "Found 200.\n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.11, Page number: 262" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def arraySum (array,n):\n", "\n", " #Variable Declaration\n", " sum = 0 \n", " arrayEnd = n\n", " #Calculation\n", " for i in range(0,arrayEnd):\n", " sum += array[i]\n", " return sum\n", "\n", "def main():\n", "\n", " #List Declaration\n", " values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]\n", " #Result\n", " print(\"The sum is {0}\\n\".format(arraySum (values, 10)))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sum is 21\n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.12, Page number: 265" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def arraySum (array,n):\n", " #Variable Declaration\n", " sum = 0 \n", " #Calculation\n", " for i in array:\n", " sum += i\n", " return sum\n", "\n", "def main():\n", "\n", " #List Declaration\n", " values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]\n", " #Result\n", " print(\"The sum is {0}\\n\".format(arraySum(values,10)))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sum is 21\n", "\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.13, Page number: 266" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def copyString(to, frm):\n", " global string2\n", " to=frm\n", " string2=to\n", "\n", "def main():\n", " \n", " global string2\n", " #String declaration\n", " string1=\"A string to be copied\"\n", " string2=\"\"\n", " copyString(string2,string1) #Functon call\n", "\n", " print(\"{0}\\n\".format(string2))\n", " copyString (string2, \"So is this.\") #Function call\n", " print(\"{0}\\n\".format(string2))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A string to be copied\n", "\n", "So is this.\n", "\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.14, Page number: 271" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def copyString(to, frm):\n", " global string2\n", " to=frm\n", " string2=to\n", "\n", "def main():\n", " \n", " global string2\n", " #String declaration\n", " string1=\"A string to be copied\"\n", " string2=\"\"\n", " copyString(string2,string1) #Functon call\n", "\n", " print(\"{0}\\n\".format(string2))\n", " copyString (string2, \"So is this.\") #Function call\n", " print(\"{0}\\n\".format(string2))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A string to be copied\n", "\n", "So is this.\n", "\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 11.15, Page number: 272" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def stringLength(string):\n", " return len(string) #Calculation\n", "\n", "def main():\n", " print(\"{0} \".format(stringLength (\"stringLength test\")))\n", " print(\"{0} \".format(stringLength (\"\")))\n", " print(\"{0}\".format(stringLength (\"complete\")))\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "17 \n", "0 \n", "8\n" ] } ], "prompt_number": 15 } ], "metadata": {} } ] }