diff options
author | kinitrupti | 2017-05-12 18:53:46 +0530 |
---|---|---|
committer | kinitrupti | 2017-05-12 18:53:46 +0530 |
commit | 6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d (patch) | |
tree | 22789c9dbe468dae6697dcd12d8e97de4bcf94a2 /ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb | |
parent | d36fc3b8f88cc3108ffff6151e376b619b9abb01 (diff) | |
download | Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.gz Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.tar.bz2 Python-Textbook-Companions-6279fa19ac6e2a4087df2e6fe985430ecc2c2d5d.zip |
Removed duplicates
Diffstat (limited to 'ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb')
-rwxr-xr-x | ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb | 970 |
1 files changed, 970 insertions, 0 deletions
diff --git a/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb new file mode 100755 index 00000000..6e2f2667 --- /dev/null +++ b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter5.ipynb @@ -0,0 +1,970 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:2cf0a369761f6b9850c2adad43b1edd33ca8cbea4df08e2e06de2898a570851d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 5: FUNCTIONS AND POINTERS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message(): #function definition\n", + " print \"Smile,and the world smiles with you...\\n\"\n", + "message() #function call\n", + "print \"Cry, and you stop the monotony!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Smile,and the world smiles with you...\n", + "\n", + "Cry, and you stop the monotony!\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() will be called\n", + "brazil(); #brazil() will be called\n", + "argentina(); #argentina() will be called" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + " brazil(); #it will call brazil()\n", + " print \"I am back in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + " argentina(); #it will call argentina()\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() is called\n", + "print \"I am finally back in main\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n", + "I am back in italy\n", + "\n", + "I am finally back in main\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Can't imagine life without C\\n\"\n", + " main() #it will call back main()\n", + "def main():\n", + " message()\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Jewel Thief!!\\n\"\n", + "message() #1st call\n", + "message() #2nd call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jewel Thief!!\n", + "\n", + "Jewel Thief!!\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message2():\n", + " print \"But the butter was bitter\\n\"\n", + "def message1():\n", + " print \"Mary bought some butter\\n\"\n", + "message1()\n", + "message2()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mary bought some butter\n", + "\n", + "But the butter was bitter\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:151-152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def calsum(x,y,z): #parameterized function\n", + " d=x+y+z\n", + " return d\n", + "print \"Enter any three numbers\"\n", + "a=eval(raw_input())\n", + "b=eval(raw_input())\n", + "c=eval(raw_input())\n", + "sum=calsum(a,b,c) #passing values as arguments\n", + "print \"Sum=%d\\n\" % (sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any three numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum=60\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " print \"Enter any number\"\n", + " n=eval(raw_input())\n", + " if (n>=10 and n<=90):\n", + " return n\n", + " else:\n", + " return n+32\n", + "fun()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "132" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun(b):\n", + " b=60\n", + " print \"%d\\n\" % (b) #prints 60\n", + "a=30\n", + "fun(a) #prints 30\n", + "print \"%d\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n", + "\n", + "30\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:155" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(j):\n", + " k=35\n", + " print \"%d\\n\" % (j) #we can't print i directly here because scope of variable is local by default\n", + " print \"%d\\n\" % (k)\n", + "i=20\n", + "display(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n", + "\n", + "35\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:158-159" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def square(x):\n", + " y=x*x\n", + " return y\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "b=square(a)\n", + "print \"Square of %f is %f\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Square of 2.500000 is 6.250000\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i)) #id() will return the loaction of a variable\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i))\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of j\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986780\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "k=id(j)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of i\n", + "print \"Address of j=%u\\n\" % (k) #print address of j\n", + "print \"Address of k=%u\\n\" % (id(k)) #print address of k\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of k=%u\\n\" % (k) #print value of k\n", + "print \"Value of i=%u\\n\" % (i) #print value of i\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of k=88986708\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of k=88986672\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapv(x,y):\n", + " x,y=y,x\n", + " print \"x=%d y=%d\\n\" % (x,y)\n", + "a=10\n", + "b=20\n", + "swapv(a,b)\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x=20 y=10\n", + "\n", + "a=10 b=20\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapr():\n", + " global a,b #global declaration\n", + " a,b=b,a\n", + "a=10\n", + "b=20\n", + "swapr()\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a=20 b=10\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:167-168" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def areaperi(r,a,p):\n", + " a=3.14*r*r #formula of area\n", + " p=2*3.14*r #formula of perimeter\n", + " print \"Area=%f\\n\" % (a)\n", + " print \"Perimeter=%f\\n\" % (p)\n", + "area=0\n", + "perimeter=0\n", + "print \"Enter radius of a circle\"\n", + "radius=eval(raw_input())\n", + "areaperi(radius,area,perimeter)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of a circle\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area=78.500000\n", + "\n", + "Perimeter=31.400000\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def factorial(x):\n", + " f=1\n", + " for i in range(x,1,-1):\n", + " f=f*i\n", + " return f\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=factorial(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=6\n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:170" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def rec(x):\n", + " if x==1:\n", + " return 1\n", + " else:\n", + " f=x*rec(x-1) \n", + " return f #will call back the rec() function\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=rec(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=120\n", + "\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def add(i,j):\n", + " sum=i+j\n", + " return sum\n", + "a=5\n", + "b=2\n", + "c=add(a,b) #Transfers control to add()\n", + "print \"sum=%d\\n\" % (c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum=7\n", + "\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:175-176" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def factorial(num):\n", + " f=1\n", + " for i in range(1,num+1,1):\n", + " f=f*i;\n", + " return f\n", + "f=factorial(5)\n", + "print \"%d\\n\" % f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "120\n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |