diff options
author | Hardik Ghaghada | 2014-06-20 15:52:25 +0530 |
---|---|---|
committer | Hardik Ghaghada | 2014-06-20 15:52:25 +0530 |
commit | e1e59ca3a50d9f93e8b7bc0693b8081d5db77771 (patch) | |
tree | f54eab21dd3d725d64a495fcd47c00d37abed004 /C++_from_the_Ground/Chapter_9(1).ipynb | |
parent | a78126bbe4443e9526a64df9d8245c4af8843044 (diff) | |
parent | 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (diff) | |
download | Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.tar.gz Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.tar.bz2 Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.zip |
Merge pull request #1 from debashisdeb/master
removing problem statements from all the chapters to avoid copyright violations
Diffstat (limited to 'C++_from_the_Ground/Chapter_9(1).ipynb')
-rw-r--r-- | C++_from_the_Ground/Chapter_9(1).ipynb | 547 |
1 files changed, 490 insertions, 57 deletions
diff --git a/C++_from_the_Ground/Chapter_9(1).ipynb b/C++_from_the_Ground/Chapter_9(1).ipynb index b31c36a0..9e6e1729 100644 --- a/C++_from_the_Ground/Chapter_9(1).ipynb +++ b/C++_from_the_Ground/Chapter_9(1).ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 9" + "name": "", + "signature": "sha256:9ff1f12edc348acd1b1c3b16cc9a9556459332fb35a1337252165631b4cd82af" }, "nbformat": 3, "nbformat_minor": 0, @@ -10,24 +11,37 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h1>Chapter 9: More Data Types and Operations<h1>" + "source": [ + "<h1>Chapter 9: More Data Types and Operations<h1>" + ] }, { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.1, Page Number: 182<h3>" + "source": [ + "<h3>Example 9.1, Page Number: 182<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Printing a string'''\n#pyhton doesnt have constants, hence a normal object has been used\n\ndef code(str):\n print str\n\n#Calling function\ncode(\"this is a test\")", + "input": [ + "\n", + "def code(str):\n", + " print str\n", + "\n", + "#Calling function\n", + "code(\"this is a test\")" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "this is a test\n" + "text": [ + "this is a test\n" + ] } ], "prompt_number": 2 @@ -35,19 +49,34 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.2, Page Number: 183<h3>" + "source": [ + "<h3>Example 9.2, Page Number: 183<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing const references'''\n\ndef f(i):\n i=100\n print i\n \n#Variable declaration\nk=10\n\n#function call\nf(k)\n", + "input": [ + "\n", + "def f(i):\n", + " i=100\n", + " print i\n", + " \n", + "#Variable declaration\n", + "k=10\n", + "\n", + "#function call\n", + "f(k)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "100\n" + "text": [ + "100\n" + ] } ], "prompt_number": 1 @@ -55,19 +84,32 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.3, Page Number: 187<h3>" + "source": [ + "<h3>Example 9.3, Page Number: 187<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementing extern in pyhton'''\n\n#Variable Declaration\nfirst=10 #global definition of first and last\nlast=20\n\n#Result\nprint first,last\n\n", + "input": [ + "\n", + "#Variable Declaration\n", + "first=10 #global definition of first and last\n", + "last=20\n", + "\n", + "#Result\n", + "print first,last\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10 20\n" + "text": [ + "10 20\n" + ] } ], "prompt_number": 7 @@ -75,19 +117,55 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.4, Page Number: 188<h3>" + "source": [ + "<h3>Example 9.4, Page Number: 188<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0 \ncount=0\nnum=5 #Loop for user entries\n\n#compute a running average\ndef r_avg(i):\n global sum,count\n sum=sum+i\n count+=1\n return sum/count\n\n\nwhile True:\n print \"Enter numbers(-1 to quit): \"\n num-=1 #User input\n if not(num==-1):\n print \"Running average is: \",r_avg(num) #Result\n if num<0:\n break", + "input": [ + "\n", + "#Variable declaration\n", + "sum=0 \n", + "count=0\n", + "num=5 #Loop for user entries\n", + "\n", + "#compute a running average\n", + "def r_avg(i):\n", + " global sum,count\n", + " sum=sum+i\n", + " count+=1\n", + " return sum/count\n", + "\n", + "\n", + "while True:\n", + " print \"Enter numbers(-1 to quit): \"\n", + " num-=1 #User input\n", + " if not(num==-1):\n", + " print \"Running average is: \",r_avg(num) #Result\n", + " if num<0:\n", + " break" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter numbers(-1 to quit): \nRunning average is: 4\nEnter numbers(-1 to quit): \nRunning average is: 3\nEnter numbers(-1 to quit): \nRunning average is: 3\nEnter numbers(-1 to quit): \nRunning average is: 2\nEnter numbers(-1 to quit): \nRunning average is: 2\nEnter numbers(-1 to quit): \n" + "text": [ + "Enter numbers(-1 to quit): \n", + "Running average is: 4\n", + "Enter numbers(-1 to quit): \n", + "Running average is: 3\n", + "Enter numbers(-1 to quit): \n", + "Running average is: 3\n", + "Enter numbers(-1 to quit): \n", + "Running average is: 2\n", + "Enter numbers(-1 to quit): \n", + "Running average is: 2\n", + "Enter numbers(-1 to quit): \n" + ] } ], "prompt_number": 3 @@ -95,19 +173,75 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.5, Page Number: 189<h3>" + "source": [ + "<h3>Example 9.5, Page Number: 189<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0 \ncount=0\nnum=10 #Loop for user entries\n\n#user input given: 9,8,7,6,-2,4,3,2,1,-1\n\n#compute a running average\ndef r_avg(i):\n global sum,count\n sum=sum+i\n count+=1\n return sum/count\n\ndef reset():\n global sum,count\n sum=0\n count=0\n \nwhile True:\n print \"Enter numbers(-1 to quit, -2 to reset): \"\n num-=1 #User input\n if num==5:\n num=-2\n if num==-2: #for reset\n num=4\n reset()\n continue\n if not(num==-1):\n print \"Running average is: \",r_avg(num) #Result\n else:\n break", + "input": [ + "\n", + "\n", + "#Variable declaration\n", + "sum=0 \n", + "count=0\n", + "num=10 #Loop for user entries\n", + "\n", + "#user input given: 9,8,7,6,-2,4,3,2,1,-1\n", + "\n", + "#compute a running average\n", + "def r_avg(i):\n", + " global sum,count\n", + " sum=sum+i\n", + " count+=1\n", + " return sum/count\n", + "\n", + "def reset():\n", + " global sum,count\n", + " sum=0\n", + " count=0\n", + " \n", + "while True:\n", + " print \"Enter numbers(-1 to quit, -2 to reset): \"\n", + " num-=1 #User input\n", + " if num==5:\n", + " num=-2\n", + " if num==-2: #for reset\n", + " num=4\n", + " reset()\n", + " continue\n", + " if not(num==-1):\n", + " print \"Running average is: \",r_avg(num) #Result\n", + " else:\n", + " break" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Enter numbers(-1 to quit, -2 to reset): \nRunning average is: 9\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 7\nEnter numbers(-1 to quit, -2 to reset): \nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 3\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is: 1\nEnter numbers(-1 to quit, -2 to reset): \n" + "text": [ + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 9\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 8\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 8\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 7\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 3\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 2\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 2\n", + "Enter numbers(-1 to quit, -2 to reset): \n", + "Running average is: 1\n", + "Enter numbers(-1 to quit, -2 to reset): \n" + ] } ], "prompt_number": 4 @@ -115,19 +249,42 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.6, Page Number: 196<h3>" + "source": [ + "<h3>Example 9.6, Page Number: 196<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of enumeration in python'''\n\n#Array of strings that correspond to the enumeration\nname=[\"Jonathan\",\"Golden Delicious\",\"Red Delicious\",\"Winesap\",\n \"Cortland\",\"McIntosh\"]\n\n#enumeration type\n(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)\n\nfruit=Jonathan\nprint name[fruit]\n\nfruit = Winesap\nprint name[fruit]\n\nfruit = McIntosh\nprint name[fruit]", + "input": [ + "\n", + "#Array of strings that correspond to the enumeration\n", + "name=[\"Jonathan\",\"Golden Delicious\",\"Red Delicious\",\"Winesap\",\n", + " \"Cortland\",\"McIntosh\"]\n", + "\n", + "#enumeration type\n", + "(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)\n", + "\n", + "fruit=Jonathan\n", + "print name[fruit]\n", + "\n", + "fruit = Winesap\n", + "print name[fruit]\n", + "\n", + "fruit = McIntosh\n", + "print name[fruit]" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Jonathan\nWinesap\nMcIntosh\n" + "text": [ + "Jonathan\n", + "Winesap\n", + "McIntosh\n" + ] } ], "prompt_number": 1 @@ -135,19 +292,45 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.7, Page Number: 198<h3>" + "source": [ + "<h3>Example 9.7, Page Number: 198<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Convert to uppercase letter'''\n\n#Variable declaration\nch='j' #User input\nwhile True:\n #This statement turns off the 6th but.\n c=chr(ord(ch)&223) #ch is now uppercase\n print c\n if c=='Q':\n break \n else:\n ch = chr(ord(ch)+1) #incrementing for different user inputs\n \n\n", + "input": [ + "\n", + "\n", + "#Variable declaration\n", + "ch='j' #User input\n", + "while True:\n", + " #This statement turns off the 6th but.\n", + " c=chr(ord(ch)&223) #ch is now uppercase\n", + " print c\n", + " if c=='Q':\n", + " break \n", + " else:\n", + " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", + " \n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "J\nK\nL\nM\nN\nO\nP\nQ\n" + "text": [ + "J\n", + "K\n", + "L\n", + "M\n", + "N\n", + "O\n", + "P\n", + "Q\n" + ] } ], "prompt_number": 3 @@ -155,19 +338,44 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.8, Page Number: 200<h3>" + "source": [ + "<h3>Example 9.8, Page Number: 200<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Convert to lowercase letter'''\n\n#Variable declaration\nch='J' #User input\nwhile True:\n #This statement turns off the 6th but.\n c=chr(ord(ch)|32) #ch is now uppercase\n print c\n if c=='q':\n break \n else:\n ch = chr(ord(ch)+1) #incrementing for different user inputs\n ", + "input": [ + "\n", + "\n", + "#Variable declaration\n", + "ch='J' #User input\n", + "while True:\n", + " #This statement turns off the 6th but.\n", + " c=chr(ord(ch)|32) #ch is now uppercase\n", + " print c\n", + " if c=='q':\n", + " break \n", + " else:\n", + " ch = chr(ord(ch)+1) #incrementing for different user inputs\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "j\nk\nl\nm\nn\no\np\nq\n" + "text": [ + "j\n", + "k\n", + "l\n", + "m\n", + "n\n", + "o\n", + "p\n", + "q\n" + ] } ], "prompt_number": 4 @@ -175,19 +383,46 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.9, Page Number: 201<h3>" + "source": [ + "<h3>Example 9.9, Page Number: 201<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Display the bits within a byte'''\n\n#function to display the bits\ndef disp_binary(u):\n t=128\n while t:\n if u&t:\n print 1,\n else:\n print 0,\n t=t/2\n print \"\"\n \n#Variable declaration\nu=99 #User Input\n\nprint \"Here's the number in binary: \",\ndisp_binary(u)\n\nprint \"Here's the complement of th number: \",\ndisp_binary(~u)\n", + "input": [ + "\n", + "\n", + "#function to display the bits\n", + "def disp_binary(u):\n", + " t=128\n", + " while t:\n", + " if u&t:\n", + " print 1,\n", + " else:\n", + " print 0,\n", + " t=t/2\n", + " print \"\"\n", + " \n", + "#Variable declaration\n", + "u=99 #User Input\n", + "\n", + "print \"Here's the number in binary: \",\n", + "disp_binary(u)\n", + "\n", + "print \"Here's the complement of th number: \",\n", + "disp_binary(~u)\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Here's the number in binary: 0 1 1 0 0 0 1 1 \nHere's the complement of th number: 1 0 0 1 1 1 0 0 \n" + "text": [ + "Here's the number in binary: 0 1 1 0 0 0 1 1 \n", + "Here's the complement of th number: 1 0 0 1 1 1 0 0 \n" + ] } ], "prompt_number": 5 @@ -195,19 +430,68 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.10, Page Number: 202<h3>" + "source": [ + "<h3>Example 9.10, Page Number: 202<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstrate bitshifting'''\n\n#unction to display the bits within a byte\ndef disp_binary(u):\n t=128\n while t:\n if u&t:\n print 1,\n else:\n print 0,\n t=t/2\n print \"\"\n \n#Variable dclaration\ni=1\n\n#Result\nfor t in range(8):\n disp_binary(i)\n i=i<<1\n\nprint\"\\n\"\n\nfor t in range(8):\n i=i>>1\n disp_binary(i)\n ", + "input": [ + "\n", + "\n", + "#unction to display the bits within a byte\n", + "def disp_binary(u):\n", + " t=128\n", + " while t:\n", + " if u&t:\n", + " print 1,\n", + " else:\n", + " print 0,\n", + " t=t/2\n", + " print \"\"\n", + " \n", + "#Variable dclaration\n", + "i=1\n", + "\n", + "#Result\n", + "for t in range(8):\n", + " disp_binary(i)\n", + " i=i<<1\n", + "\n", + "print\"\\n\"\n", + "\n", + "for t in range(8):\n", + " i=i>>1\n", + " disp_binary(i)\n", + " " + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "0 0 0 0 0 0 0 1 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 \n\n\n1 0 0 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 0 0 1 \n" + "text": [ + "0 0 0 0 0 0 0 1 \n", + "0 0 0 0 0 0 1 0 \n", + "0 0 0 0 0 1 0 0 \n", + "0 0 0 0 1 0 0 0 \n", + "0 0 0 1 0 0 0 0 \n", + "0 0 1 0 0 0 0 0 \n", + "0 1 0 0 0 0 0 0 \n", + "1 0 0 0 0 0 0 0 \n", + "\n", + "\n", + "1 0 0 0 0 0 0 0 \n", + "0 1 0 0 0 0 0 0 \n", + "0 0 1 0 0 0 0 0 \n", + "0 0 0 1 0 0 0 0 \n", + "0 0 0 0 1 0 0 0 \n", + "0 0 0 0 0 1 0 0 \n", + "0 0 0 0 0 0 1 0 \n", + "0 0 0 0 0 0 0 1 \n" + ] } ], "prompt_number": 6 @@ -215,19 +499,43 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.11, Page Number: 204<h3>" + "source": [ + "<h3>Example 9.11, Page Number: 204<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''This program implements ? operator in python'''\n\ndef div_zero():\n print \"Cannot divide by zero.\"\n return 0\n \n#Variable declaration\ni=10 #User Input\nj=0\n\n#This statement prevents a divide by zero\nif j:\n result=i/j\nelse:\n result=div_zero()\n\n#Result\nprint \"Result: \",result", + "input": [ + "\n", + "\n", + "def div_zero():\n", + " print \"Cannot divide by zero.\"\n", + " return 0\n", + " \n", + "#Variable declaration\n", + "i=10 #User Input\n", + "j=0\n", + "\n", + "#This statement prevents a divide by zero\n", + "if j:\n", + " result=i/j\n", + "else:\n", + " result=div_zero()\n", + "\n", + "#Result\n", + "print \"Result: \",result" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Cannot divide by zero.\nResult: 0\n" + "text": [ + "Cannot divide by zero.\n", + "Result: 0\n" + ] } ], "prompt_number": 7 @@ -235,19 +543,35 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.12, Page Number: 206<h3>" + "source": [ + "<h3>Example 9.12, Page Number: 206<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of the comma operator'''\n\n#Variable declaration\nj=10\ni=None\n\nj+=1\nj+100\ni=999+j\n\n#Result\nprint i", + "input": [ + "\n", + "#Variable declaration\n", + "j=10\n", + "i=None\n", + "\n", + "j+=1\n", + "j+100\n", + "i=999+j\n", + "\n", + "#Result\n", + "print i" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1010\n" + "text": [ + "1010\n" + ] } ], "prompt_number": 1 @@ -255,19 +579,36 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.13, Page Number: 207<h3>" + "source": [ + "<h3>Example 9.13, Page Number: 207<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Demonstrate sizeof'''\n\nfrom ctypes import *\n\n#Variable declaration\nch=c_char\ni=c_int\n\nprint sizeof(ch), #size of char\nprint sizeof(i), #size of int\nprint sizeof(c_float), #size of float\nprint sizeof(c_double) #size of double\n", + "input": [ + "\n", + "\n", + "from ctypes import *\n", + "\n", + "#Variable declaration\n", + "ch=c_char\n", + "i=c_int\n", + "\n", + "print sizeof(ch), #size of char\n", + "print sizeof(i), #size of int\n", + "print sizeof(c_float), #size of float\n", + "print sizeof(c_double) #size of double\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1 4 4 8\n" + "text": [ + "1 4 4 8\n" + ] } ], "prompt_number": 2 @@ -275,19 +616,33 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.14, Page Number: 209<h3>" + "source": [ + "<h3>Example 9.14, Page Number: 209<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Example of pointers'''\n\nfrom ctypes import *\n\n#Variabke declaration \ni=c_int(20) #allocate memory for int\np=pointer(i) #assign a pointer to the memory\n\n#Result\nprint p[0] #proove that it works by displaying value\n", + "input": [ + "\n", + "from ctypes import *\n", + "\n", + "#Variabke declaration \n", + "i=c_int(20) #allocate memory for int\n", + "p=pointer(i) #assign a pointer to the memory\n", + "\n", + "#Result\n", + "print p[0] #proove that it works by displaying value\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "20\n" + "text": [ + "20\n" + ] } ], "prompt_number": 3 @@ -295,19 +650,33 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.15, Page Number: 210<h3>" + "source": [ + "<h3>Example 9.15, Page Number: 210<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_int(99) #initialize with 99\np=pointer(i) #assign a pointer to the value\n\n#Result\nprint p[0] #displays 99\n", + "input": [ + "\n", + "from ctypes import *\n", + "\n", + "#Variable declaration \n", + "i=c_int(99) #initialize with 99\n", + "p=pointer(i) #assign a pointer to the value\n", + "\n", + "#Result\n", + "print p[0] #displays 99\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "99\n" + "text": [ + "99\n" + ] } ], "prompt_number": 4 @@ -315,19 +684,38 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.16, Page Number: 211<h3>" + "source": [ + "<h3>Example 9.16, Page Number: 211<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_double(10) \np=pointer(i)\n\n#assign the values 100 to 109\nfor i in range(10):\n p[i]=100.00+i\n\n#display the contents of the array\nfor i in range(10):\n print p[i],\n", + "input": [ + "\n", + "from ctypes import *\n", + "\n", + "#Variable declaration \n", + "i=c_double(10) \n", + "p=pointer(i)\n", + "\n", + "#assign the values 100 to 109\n", + "for i in range(10):\n", + " p[i]=100.00+i\n", + "\n", + "#display the contents of the array\n", + "for i in range(10):\n", + " print p[i],\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0\n" + "text": [ + "100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0\n" + ] } ], "prompt_number": 5 @@ -335,19 +723,38 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.17, Page Number: 211<h3>" + "source": [ + "<h3>Example 9.17, Page Number: 211<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of malloc int python'''\n\n#Variable declaration\ni=c_int() \nj=c_double()\npi=pointer(i) #pointer to int\npj=pointer(j) #pointer to double\n\n#Assign values using pointers\npi[0]=10\npj[0]=100.123\n\n#Result\nprint pi[0],pj[0]\n\n", + "input": [ + "\n", + "#Variable declaration\n", + "i=c_int() \n", + "j=c_double()\n", + "pi=pointer(i) #pointer to int\n", + "pj=pointer(j) #pointer to double\n", + "\n", + "#Assign values using pointers\n", + "pi[0]=10\n", + "pj[0]=100.123\n", + "\n", + "#Result\n", + "print pi[0],pj[0]\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10 100.123\n" + "text": [ + "10 100.123\n" + ] } ], "prompt_number": 6 @@ -355,19 +762,45 @@ { "cell_type": "markdown", "metadata": {}, - "source": "<h3>Example 9.18, Page Number: 212<h3>" + "source": [ + "<h3>Example 9.18, Page Number: 212<h3>" + ] }, { "cell_type": "code", "collapsed": false, - "input": "'''Implementation of dynamic memory allocation'''\nfrom ctypes import *\n\n#Variable declaration\ni=pointer(c_int())\nj=pointer(c_double())\n\n#Checking if i and j have been allocated memory addresses\nif not(id(i)):\n print \"Allocation Failure.\"\n \nif not(id(j)):\n print \"Allocation Failure.\" \n\ni[0]=10\nj[0]=100.123\n\n\n#Result\nprint i[0],j[0]\n\n", + "input": [ + "\n", + "from ctypes import *\n", + "\n", + "#Variable declaration\n", + "i=pointer(c_int())\n", + "j=pointer(c_double())\n", + "\n", + "#Checking if i and j have been allocated memory addresses\n", + "if not(id(i)):\n", + " print \"Allocation Failure.\"\n", + " \n", + "if not(id(j)):\n", + " print \"Allocation Failure.\" \n", + "\n", + "i[0]=10\n", + "j[0]=100.123\n", + "\n", + "\n", + "#Result\n", + "print i[0],j[0]\n", + "\n" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "10 100.123\n" + "text": [ + "10 100.123\n" + ] } ], "prompt_number": 11 @@ -375,7 +808,7 @@ { "cell_type": "code", "collapsed": false, - "input": "", + "input": [], "language": "python", "metadata": {}, "outputs": [] |