diff options
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb')
-rw-r--r-- | Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb | 329 |
1 files changed, 329 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb new file mode 100644 index 00000000..f480fc86 --- /dev/null +++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch7.ipynb @@ -0,0 +1,329 @@ +{ + "metadata": { + "name": "ch7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.1 Printing Pointer Values\n'''\n\nn=44\nprint \"n = \" , n \n# prints the value of n\nprint \"&n = \" , hex(id(n)) # prints the address of n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n&n = 0x8fc0eec\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.2\nNote : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"n = \" , n , \", rn = \" , rn \nn[0] -= 1\nprint \"n = \" , n , \", rn = \" , rn \nrn[0] *= 2\nprint \"n = \" , n , \", rn = \" , rn \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , rn = [44]\nn = [43] , rn = [43]\nn = [86] , rn = [86]\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.3 References Are Not Separate Variables\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\nrn2 = n\nrn3 = rn\nprint \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "&n = 0x9c6228c , rn = 0x9c6228c\n&rn2 = 0x9c6228c , rn = 0x9c6228c\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.4 Using Pointer Variables\n\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c624ec\npn = 0x9c624ec , &pn = 0x9c6aa60\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.5\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c58d6c\n\tpn = 0x9c58d6c ,\n &pn = 0x9c6ab20\n*pn = [44]\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.6 \n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nppn = pn\n\nprint \" ppn = \" , hex(id(hex(id(ppn)))) \nprint \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\nprint \" *ppn = \" , hex(id(ppn)) \nprint \"**ppn = \" , ppn \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9bf05ac\n\tpn = 0x9bf05ac ,\n &pn = 0x9c58160\n*pn = [44]\n ppn = 0x9c58680\n &ppn = 0x9c58160\n *ppn = 0x9bf05ac\n**ppn = [44]\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.7\n'''\n\nn = [44]\nprint \"n = \" , n , \"\\n &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nnn = pn\nprint \" ppn = \" , hex(id(nn))\nprint \" &ppn = \" , hex(id(hex(id(nn))))\nrpn = pn\nprint \" ppn = \" , hex(id(rpn))\nprint \" &ppn = \" , hex(id(hex(id(rpn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] \n &n = 0x9bf60ec\n\tpn = 0x9bf60ec ,\n &pn = 0x9bf0e40\n*pn = [44]\n ppn = 0x9bf60ec\n &ppn = 0x9bf0e40\n ppn = 0x9bf60ec\n &ppn = 0x9bf0f20\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.8\n'''\n\ndef max_(m,n):\n if m>n:\n return m\n else:\n return n\n\nm = 44\nn = 22\nprint m , \", \" , n , \", \" , max_(m,n)\nm = max_(m,n) \nm = 55\n# changes the value of m from 44 to 55\nprint m , \", \" , n , \", \" , max_(m,n) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "44 , 22 , 44\n55 , 22 , 55\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.9 \n'''\n\nv = []\nfor k in range(1,5):\n v.append(1.0/k)\n\nfor i in range(4):\n print \"v[\" , i , \"] = \" , v[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "v[ 0 ] = 1.0\nv[ 1 ] = 0.5\nv[ 2 ] = 0.333333333333\nv[ 3 ] = 0.25\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.10 \n'''\nimport sys\na = [22, 33, 44]\n\nprint \"a = \" , hex(id(a))\nprint \"sizeof(int) = \" , sys.getsizeof(1) \ns = 0\nfor i in a:\n s += i\n print \"\\t i = \" , hex(id(i)),\n print \"\\t *i = \" , i,\n print \"\\t sum = \" , s\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9bf688c\nsizeof(int) = 12\n\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.11 Examining the Addresses of Array Elements\n'''\n\na = [22, 33, 44, 55, 66]\nprint \"a = \" , hex(id(a)) , \", *a = \" , a[0] \nfor p in a:\n print \"p = \" , hex(id(p)) , \", *p = \" , p \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9c6526c , *a = 22\np = 0x8fc0ff4 , *p = 22\np = 0x8fc0f70 , *p = 33\np = 0x8fc0eec , *p = 44\np = 0x8fc0e68 , *p = 55\np = 0x8fc0de4 , *p = 66\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.12 Pattern Matching\n'''\n\ndef loc(a1,a2,n1,n2):\n p = []\n for element in a2:\n if element in a1:\n p.append(element)\n return p\n\na1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\na2 = [11, 11, 11, 22, 33]\nprint \"Array a1 begins at location\\t\" , hex(id(a1 ))\nprint \"Array a2 begins at location\\t\" , hex(id(a2)) \np = loc(a1, a2, 9, 5)\nif (p):\n print \"Array a2 found at location\\t\" , hex(id(p))\n for i in range(len(p)):\n print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \nelse:\n print \"Not found.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Array a1 begins at location\t0x9bea56c\nArray a2 begins at location\t0x9bea62c\nArray a2 found at location\t0x9bea6cc\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.15 Using Dynamic Arrays\n'''\n\ndef get(a):\n print \"Enter number of items: \"\n n = int(raw_input())\n print \"Enter \" , n , \" items, one per line:\"\n for i in range(n):\n print \"\\t\" , i+1 , \": \",\n a.append(float(raw_input()))\n\ndef print_(a):\n for i in range(len(a)):\n print a[i] ,\n print ''\n\na = []\nget(a)\nprint_(a)\na = []\nget(a)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 4 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44.4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77.7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t3 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22.2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t4 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "88.8\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 44.4 77.7 22.2 88.8 \nEnter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 2 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3.33\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9.99\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 3.33 9.99 \n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.17 Indirect Bubble Sort\n'''\n\ndef sort(p, n):\n for i in range(1,n):\n for j in range(n-i):\n if (p[j] > p[j+1]):\n p[j],p[j+1] = p[j+1],p[j]\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.18 The Sum of a Function\n'''\n\ndef sum_(k,n):\n # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n s = 0\n for i in range(1,n+1):\n s += k(i)\n return s\n\ndef square(k):\n return k*k\n\ndef cube(k):\n return k*k*k\n\n\nprint sum_(square,4) # 1 + 4 + 9 + 16\nprint sum_(cube,4) # 1 + 8 + 27 + 64\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "30\n100\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |