diff options
author | hardythe1 | 2015-01-30 12:30:05 +0530 |
---|---|---|
committer | hardythe1 | 2015-01-30 12:30:05 +0530 |
commit | 62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 (patch) | |
tree | 7b632b21341f832c3d3b1352dabceaefb36c9a7b /Teach_Yourself_C_in_24_Hours/hour16.ipynb | |
parent | 9add422993fb2649287260bc91d429a07d1810d5 (diff) | |
download | Python-Textbook-Companions-62aa228e2519ac7b7f1aef53001f2f2e988a6eb1.tar.gz Python-Textbook-Companions-62aa228e2519ac7b7f1aef53001f2f2e988a6eb1.tar.bz2 Python-Textbook-Companions-62aa228e2519ac7b7f1aef53001f2f2e988a6eb1.zip |
added books
Diffstat (limited to 'Teach_Yourself_C_in_24_Hours/hour16.ipynb')
-rwxr-xr-x | Teach_Yourself_C_in_24_Hours/hour16.ipynb | 426 |
1 files changed, 426 insertions, 0 deletions
diff --git a/Teach_Yourself_C_in_24_Hours/hour16.ipynb b/Teach_Yourself_C_in_24_Hours/hour16.ipynb new file mode 100755 index 00000000..c9dab487 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour16.ipynb @@ -0,0 +1,426 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:f0f6a2ccafa5bf612c316f32caa438a56abd55c7d615f06f2f9b34b2ed02f28d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 16: Applying Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.1, Page No 260"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "ptr_ch=chr\n",
+ "ptr_int=int\n",
+ "ptr_db=float\n",
+ "\n",
+ "#Char pointer ptr_ch\n",
+ "print \" Corrent position of ptr_ch: %#x \" % id(ptr_ch)\n",
+ "print \" The position after ptr_ch+1 : %#x\" % (id(ptr_ch)+1)\n",
+ "print \" The position after ptr_ch+2 : %#x\" % (id(ptr_ch)+2)\n",
+ "print \" The position after ptr_ch+1 : %#x\" % (id(ptr_ch)+1)\n",
+ "print \" The position after ptr_ch+2 : %#x\\n\" % (id(ptr_ch)+2)\n",
+ "\n",
+ "print \" Corrent position of ptr_int: %#x \" % id(ptr_int)\n",
+ "print \" The position after ptr_int+1 : %#x\" % (id(ptr_int)+1)\n",
+ "print \" The position after ptr_int+2 : %#x\" % (id(ptr_int)+2)\n",
+ "print \" The position after ptr_int+1 : %#x\" % (id(ptr_int)+1)\n",
+ "print \" The position after ptr_int+2 : %#x\\n\" % (id(ptr_int)+2)\n",
+ "\n",
+ "print \" Corrent position of ptr_db: %#x \" % id(ptr_db)\n",
+ "print \" The position after ptr_db+1 : %#x\" % (id(ptr_db)+1)\n",
+ "print \" The position after ptr_db+2 : %#x\" % (id(ptr_db)+2)\n",
+ "print \" The position after ptr_db+1 : %#x\" % (id(ptr_db)+1)\n",
+ "print \" The position after ptr_db+2 : %#x\\n\" % (id(ptr_db)+2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Corrent position of ptr_ch: 0x12a30a8 \n",
+ " The position after ptr_ch+1 : 0x12a30a9\n",
+ " The position after ptr_ch+2 : 0x12a30aa\n",
+ " The position after ptr_ch+1 : 0x12a30a9\n",
+ " The position after ptr_ch+2 : 0x12a30aa\n",
+ "\n",
+ " Corrent position of ptr_int: 0x1e222010 \n",
+ " The position after ptr_int+1 : 0x1e222011\n",
+ " The position after ptr_int+2 : 0x1e222012\n",
+ " The position after ptr_int+1 : 0x1e222011\n",
+ " The position after ptr_int+2 : 0x1e222012\n",
+ "\n",
+ " Corrent position of ptr_db: 0x1e220be0 \n",
+ " The position after ptr_db+1 : 0x1e220be1\n",
+ " The position after ptr_db+2 : 0x1e220be2\n",
+ " The position after ptr_db+1 : 0x1e220be1\n",
+ " The position after ptr_db+2 : 0x1e220be2\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.2, Page No 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "ptr_int1=int\n",
+ "ptr_int2=int\n",
+ "\n",
+ "print \"The position of ptr_int1: %#x\" % id(ptr_int1)\n",
+ "ptr_int2=id(ptr_int1)+5\n",
+ "print \"The position of ptr_int2=id(ptr_int1)+5 :%#x\" % ptr_int2\n",
+ "print \"The subtraction of ptr_int2-ptr_int1 :%#x\" % (ptr_int2 - id(ptr_int1))\n",
+ "\n",
+ "ptr_int2=id(ptr_int1)-5\n",
+ "print \"The position of ptr_int2=id(ptr_int1)-5 :%#x\" % ptr_int2\n",
+ "print \"The subtraction of ptr_int2-ptr_int1 :%#x\" % (ptr_int2 - id(ptr_int1))\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The position of ptr_int1: 0x1e222010\n",
+ "The position of ptr_int2=id(ptr_int1)+5 :0x1e222015\n",
+ "The subtraction of ptr_int2-ptr_int1 :0x5\n",
+ "The position of ptr_int2=id(ptr_int1)-5 :0x1e22200b\n",
+ "The subtraction of ptr_int2-ptr_int1 :-0x5\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.3, Page No 265"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "str1=\"It's a string!\"\n",
+ "ptr_str=chr\n",
+ "list1=[1,2,3,4,5]\n",
+ "ptr_int=int\n",
+ "str1=list(str1)\n",
+ "ptr_str=str1\n",
+ "print \"Before the change, str1 contains: \",\n",
+ "print \"\".join(str1)\n",
+ "print \"Before the change, str1[5] contains : %c\" % str1[5]\n",
+ "str1[5]='A'\n",
+ "print \"After The change, str1[5] contains : %c\" % str1[5]\n",
+ "print \"After The change, str1 contains: \",\n",
+ "print \"\".join(str1)\n",
+ "\n",
+ "ptr_int=list1\n",
+ "print \"Before The change, list1[2] contains: %d\" % list1[2]\n",
+ "list1[2]=-3\n",
+ "print \"After the change, list1[2] contains : %d\" % list1[2]\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before the change, str1 contains: It's a string!\n",
+ "Before the change, str1[5] contains : a\n",
+ "After The change, str1[5] contains : A\n",
+ "After The change, str1 contains: It's A string!\n",
+ "Before The change, list1[2] contains: 3\n",
+ "After the change, list1[2] contains : -3\n"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.4, Page No 266"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def AddThree(list1):\n",
+ " result=0\n",
+ " for i in range(len(list1)):\n",
+ " result=result+list1[i]\n",
+ " return result\n",
+ "\n",
+ "sum1=0\n",
+ "listt=[x for x in range(3)]\n",
+ "\n",
+ "listt[0]=int(raw_input(\"Enter integer1 :\"))\n",
+ "listt[1]=int(raw_input(\"Enter integer2 :\"))\n",
+ "listt[2]=int(raw_input(\"Enter integer3 :\"))\n",
+ "sum1=AddThree(listt)\n",
+ "print \"The sum of three integers is: %d\" % sum1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer1 :10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer2 :20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integer3 :30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of three integers is: 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 42
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.5, Page No 268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def ChPrint(c):\n",
+ " print \"%s\" % c\n",
+ "def DataAdd(list1,max1):\n",
+ " sum1=0\n",
+ " for i in range(max1):\n",
+ " sum1 += list1[i]\n",
+ " return sum1\n",
+ "\n",
+ "str1=\"It's a string!\"\n",
+ "ptr_str=chr\n",
+ "listt=[1,2,3,4,5]\n",
+ "ptr_int=int\n",
+ "\n",
+ "ptr_str=str1\n",
+ "ChPrint(ptr_str)\n",
+ "ChPrint(str1)\n",
+ "\n",
+ "ptr_int=listt\n",
+ "\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(ptr_int,5)\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(listt,5)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "It's a string!\n",
+ "It's a string!\n",
+ "The sum returned by DataAdd() : 15\n",
+ "The sum returned by DataAdd() : 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 47
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.6, Page No 270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def DataAdd(list1,max1,max2):\n",
+ " sum1=0\n",
+ " for i in range(max1):\n",
+ " for j in range(max2):\n",
+ " sum1 = sum1 + list1[i][j]\n",
+ " return sum1\n",
+ "def DataAdd2(list2,max1,max2):\n",
+ " sum2=0\n",
+ " for i in range(max1):\n",
+ " for j in range(max2):\n",
+ " sum2 = sum2 + list2[i][j]\n",
+ " return sum2\n",
+ "\n",
+ "listt=[[1,2],[3,4],[5,5],[4,3],[2,1]]\n",
+ "ptr_int=int\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd(listt,5,2)\n",
+ "ptr_int=listt\n",
+ "print \"The sum returned by DataAdd() : %d\" % DataAdd2(ptr_int,5,2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum returned by DataAdd() : 30\n",
+ "The sum returned by DataAdd() : 30\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.7, Page No 272"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def StrPrint1(str1,size):\n",
+ " for i in range(size):\n",
+ " print \"%s\" % str1[i]\n",
+ "def StrPrint2(str2):\n",
+ " print \"%s\" % str2\n",
+ " \n",
+ "str1=[\"There's music in the sighing of a reed;\",\"There's music in the gushing of a rill;\",\"There's music in all things if men had ears;\",\"There earth is but an echo of the spheres.\\n\"]\n",
+ "size=4\n",
+ "\n",
+ "StrPrint1(str1,size)\n",
+ "for i in range(size):\n",
+ " StrPrint2(str1[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "There's music in the sighing of a reed;\n",
+ "There's music in the gushing of a rill;\n",
+ "There's music in all things if men had ears;\n",
+ "There earth is but an echo of the spheres.\n",
+ "\n",
+ "There's music in the sighing of a reed;\n",
+ "There's music in the gushing of a rill;\n",
+ "There's music in all things if men had ears;\n",
+ "There earth is but an echo of the spheres.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16.8, Page No 274"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner\n",
+ "def StrPrint(str1):\n",
+ " print \"%s\" % str1\n",
+ "\n",
+ "str1=\"Pointing to a function\"\n",
+ "ptr=StrPrint\n",
+ "if(not(ptr(str1))):\n",
+ " print \"Done.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Pointing to a function\n",
+ "Done.\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |