summaryrefslogtreecommitdiff
path: root/Programming_With_C/chapter9.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-07-07 16:34:28 +0530
committerJovina Dsouza2014-07-07 16:34:28 +0530
commitfffcc90da91b66ee607066d410b57f34024bd1de (patch)
tree7b8011d61013305e0bf7794a275706abd1fdb0d3 /Programming_With_C/chapter9.ipynb
parent299711403e92ffa94a643fbd960c6f879639302c (diff)
downloadPython-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.gz
Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.tar.bz2
Python-Textbook-Companions-fffcc90da91b66ee607066d410b57f34024bd1de.zip
adding book
Diffstat (limited to 'Programming_With_C/chapter9.ipynb')
-rwxr-xr-xProgramming_With_C/chapter9.ipynb555
1 files changed, 555 insertions, 0 deletions
diff --git a/Programming_With_C/chapter9.ipynb b/Programming_With_C/chapter9.ipynb
new file mode 100755
index 00000000..00db2e74
--- /dev/null
+++ b/Programming_With_C/chapter9.ipynb
@@ -0,0 +1,555 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 9: Arrays<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.2, Page number: 9.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "letter='heavenly feeling'\n",
+ "letter=list(letter)\n",
+ "\n",
+ "for count in letter:\n",
+ " print count.upper(),\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "H E A V E N L Y F E E L I N G\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.8, Page number: 9.6<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "Sum=0.0\n",
+ "List=[]\n",
+ "n=5\n",
+ "for count in range(0,n):\n",
+ " x=count+1\n",
+ " print \"\\ni = %d x = %d\" %(count+1,x),\n",
+ " List.append(count)\n",
+ " Sum+=List[count]\n",
+ "\n",
+ "avg=Sum/n\n",
+ "print \"\\n\\nThe average is %5.2f\\n\\n\" %avg\n",
+ "for count in range(0,n):\n",
+ " d=List[count]-avg\n",
+ " print \"i=%d x=%5.2f d=%5.2f\" %(count+1,List[count],d)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "i = 1 x = 1 \n",
+ "i = 2 x = 2 \n",
+ "i = 3 x = 3 \n",
+ "i = 4 x = 4 \n",
+ "i = 5 x = 5 \n",
+ "\n",
+ "The average is 2.00\n",
+ "\n",
+ "\n",
+ "i=1 x= 0.00 d=-2.00\n",
+ "i=2 x= 1.00 d=-1.00\n",
+ "i=3 x= 2.00 d= 0.00\n",
+ "i=4 x= 3.00 d= 1.00\n",
+ "i=5 x= 4.00 d= 2.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.9, Page number: 9.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n=5\n",
+ "List=[3.0,-2.0,12.0,4.4,3.5]\n",
+ "Sum=0.0\n",
+ "\n",
+ "for count in range(0,n):\n",
+ " Sum+=List[count]\n",
+ "\n",
+ "avg=Sum/n\n",
+ "\n",
+ "print \"The average is %5.2f \\n\\n\" %avg\n",
+ "\n",
+ "for count in range(0,n):\n",
+ " d=List[count]-avg\n",
+ " print \"i=%d x=%5.2f d=%5.2f\\n\" %(count+1,List[count],d)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average is 4.18 \n",
+ "\n",
+ "\n",
+ "i=1 x= 3.00 d=-1.18\n",
+ "\n",
+ "i=2 x=-2.00 d=-6.18\n",
+ "\n",
+ "i=3 x=12.00 d= 7.82\n",
+ "\n",
+ "i=4 x= 4.40 d= 0.22\n",
+ "\n",
+ "i=5 x= 3.50 d=-0.68\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.11, Page number: 9.10<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def modify(a):\n",
+ " print \"From the function after modifying the values: \"\n",
+ "\n",
+ " for count in range(0,3):\n",
+ " a[count]=-9\n",
+ " print \"a[%d] = %d \" %(count,a[count])\n",
+ "\n",
+ " return\n",
+ "\n",
+ "a=[]\n",
+ "print \"From main, before calling the function: \"\n",
+ "for count in range(0,3):\n",
+ " a.append(count+1)\n",
+ " print \"a[%d] = %d \" %(count,a[count])\n",
+ "\n",
+ "modify(a)\n",
+ "print \"From the main, after calling the function: \"\n",
+ "for count in range(0,3):\n",
+ " print \"a[%d] = %d \" %(count,a[count])\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "From main, before calling the function: \n",
+ "a[0] = 1 \n",
+ "a[1] = 2 \n",
+ "a[2] = 3 \n",
+ "From the function after modifying the values: \n",
+ "a[0] = -9 \n",
+ "a[1] = -9 \n",
+ "a[2] = -9 \n",
+ "From the main, after calling the function: \n",
+ "a[0] = -9 \n",
+ "a[1] = -9 \n",
+ "a[2] = -9 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.12, Page number: 9.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "a=1\n",
+ "def modify(b,c):\n",
+ "\n",
+ " print \"From the function, after modifying the value : \"\n",
+ " global a\n",
+ " a=-999\n",
+ " b=-999\n",
+ " print \"a = %d b = %d\" %(a,b)\n",
+ " for count in range(0,3):\n",
+ " c[count]=-9\n",
+ " print \"c[%d] = %d\" %(count,c[count])\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "\n",
+ "b=2\n",
+ "c=[]\n",
+ "\n",
+ "print \"From main, before calling the function: \"\n",
+ "print \"a = %d b = %d\" %(a,b)\n",
+ "\n",
+ "for count in range(0,3):\n",
+ " c.append(10*(count+1))\n",
+ " print \"c[%d] = %d\" %(count,c[count])\n",
+ "\n",
+ "modify(b,c)\n",
+ "print \"From main, after calling the function:\"\n",
+ "print \"a = %d b = %d\" %(a,b)\n",
+ "\n",
+ "for count in range(0,3):\n",
+ " print \"c[%d] = %d\" %(count,c[count])\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "From main, before calling the function: \n",
+ "a = 1 b = 2\n",
+ "c[0] = 10\n",
+ "c[1] = 20\n",
+ "c[2] = 30\n",
+ "From the function, after modifying the value : \n",
+ "a = -999 b = -999\n",
+ "c[0] = -9\n",
+ "c[1] = -9\n",
+ "c[2] = -9\n",
+ "From main, after calling the function:\n",
+ "a = -999 b = 2\n",
+ "c[0] = -9\n",
+ "c[1] = -9\n",
+ "c[2] = -9\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.13, Page number: 9.13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def reorder(n,x):\n",
+ " for item in range(0,n-1):\n",
+ " for i in range(item+1,n):\n",
+ " if x[i]<x[item]:\n",
+ " temp=x[item]\n",
+ " x[item]=x[i]\n",
+ " x[i]=temp\n",
+ "\n",
+ " return\n",
+ "\n",
+ "x=[]\n",
+ "n=10\n",
+ "print\n",
+ "\n",
+ "for i in range(0,n):\n",
+ " inp=i+1\n",
+ " print \"\\ni = %d x = %d\" %(i+1,inp),\n",
+ " x.append(inp)\n",
+ "\n",
+ "reorder(n,x)\n",
+ "\n",
+ "print \"\\n\\nReordered list of numbers: \\n\"\n",
+ "\n",
+ "for i in range(0,n):\n",
+ " print \"i = %d x = %d\" %(i+1,x[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "i = 1 x = 1 \n",
+ "i = 2 x = 2 \n",
+ "i = 3 x = 3 \n",
+ "i = 4 x = 4 \n",
+ "i = 5 x = 5 \n",
+ "i = 6 x = 6 \n",
+ "i = 7 x = 7 \n",
+ "i = 8 x = 8 \n",
+ "i = 9 x = 9 \n",
+ "i = 10 x = 10 \n",
+ "\n",
+ "Reordered list of numbers: \n",
+ "\n",
+ "i = 1 x = 1\n",
+ "i = 2 x = 2\n",
+ "i = 3 x = 3\n",
+ "i = 4 x = 4\n",
+ "i = 5 x = 5\n",
+ "i = 6 x = 6\n",
+ "i = 7 x = 7\n",
+ "i = 8 x = 8\n",
+ "i = 9 x = 9\n",
+ "i = 10 x = 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.14, Page number: 9.16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def countwords(english):\n",
+ " words=1\n",
+ " for count in range(0,len(english)-1):\n",
+ " if english[count]==' ' and english[count+1]!=' ':\n",
+ " words+=1\n",
+ " \n",
+ " return words\n",
+ "\n",
+ "def convert(words,english,piglatin):\n",
+ " m1=0\n",
+ " for n in range(1,words+1):\n",
+ " count=m1\n",
+ " while english[count]!=' ':\n",
+ " m2=count\n",
+ " count+=1\n",
+ "\n",
+ " for count in range(m1,m2):\n",
+ " piglatin.append(english[count+1])\n",
+ " piglatin.append(english[m1])\n",
+ " piglatin.append('a')\n",
+ " piglatin.append(' ')\n",
+ "\n",
+ " m1=m2+2\n",
+ "\n",
+ " return\n",
+ "\n",
+ "def writeoutput(piglatin):\n",
+ " piglatin=''.join(piglatin)\n",
+ " print piglatin\n",
+ " return\n",
+ "\n",
+ "def main(english):\n",
+ " english=list(english)\n",
+ " piglatin=[]\n",
+ " english.append(' ')\n",
+ "\n",
+ " words=countwords(english)\n",
+ " convert(words,english,piglatin)\n",
+ " writeoutput(piglatin)\n",
+ " \n",
+ " return\n",
+ "\n",
+ "print '\\nC is a popular structured programming language'\n",
+ "main('C is a popular structured programming language')\n",
+ "print '\\nbaseball is the great American pastime.'\n",
+ "main('baseball is the great American pastime.')\n",
+ "print '\\nthough there are many who prefer football'\n",
+ "main('though there are many who prefer football')\n",
+ "print '\\nplease do not sneeza in the computer room'\n",
+ "main('please do not sneeza in the computer room')\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "C is a popular structured programming language\n",
+ "Ca sia aa opularpa tructuredsa rogrammingpa anguagela \n",
+ "\n",
+ "baseball is the great American pastime.\n",
+ "aseballba sia heta reatga mericanAa astime.pa \n",
+ "\n",
+ "though there are many who prefer football\n",
+ "houghta hereta reaa anyma howa referpa ootballfa \n",
+ "\n",
+ "please do not sneeza in the computer room\n",
+ "leasepa oda otna neezasa nia heta omputerca oomra \n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9.19, Page number: 9.26<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def readinput(m,n,i=0):\n",
+ "\n",
+ " at=[]\n",
+ " for row in range(0,m):\n",
+ " temp=[]\n",
+ " for col in range(0,n):\n",
+ " t=i\n",
+ " i+=1\n",
+ " temp.append(t)\n",
+ " at.append(temp)\n",
+ "\n",
+ "\n",
+ " return at\n",
+ " \n",
+ "def computesum(a,b,m,n):\n",
+ "\n",
+ " c=[]\n",
+ "\n",
+ " for row in range(0,m):\n",
+ " temp=[]\n",
+ " for col in range(0,n):\n",
+ " t=a[row][col]+b[row][col]\n",
+ " temp.append(t)\n",
+ " c.append(temp)\n",
+ "\n",
+ " return c\n",
+ "\n",
+ "def writeoutput(c,m,n):\n",
+ "\n",
+ " for row in range(0,m):\n",
+ " for col in range(0,n):\n",
+ " print \"%4d\" %(c[row][col]),\n",
+ " print\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "\n",
+ "print \"\\n FIRST TABLE : \\n\"\n",
+ "a=readinput(5,5,1)\n",
+ "writeoutput(a,5,5)\n",
+ "\n",
+ "print \"\\n SECOND TABLE : \\n\"\n",
+ "b=readinput(5,5,50)\n",
+ "writeoutput(b,5,5)\n",
+ "\n",
+ "c=computesum(a,b,5,5)\n",
+ "print \"Sums of the elements : \\n\"\n",
+ "writeoutput(c,5,5)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " FIRST TABLE : \n",
+ "\n",
+ " 1 2 3 4 5\n",
+ " 6 7 8 9 10\n",
+ " 11 12 13 14 15\n",
+ " 16 17 18 19 20\n",
+ " 21 22 23 24 25\n",
+ "\n",
+ " SECOND TABLE : \n",
+ "\n",
+ " 50 51 52 53 54\n",
+ " 55 56 57 58 59\n",
+ " 60 61 62 63 64\n",
+ " 65 66 67 68 69\n",
+ " 70 71 72 73 74\n",
+ "Sums of the elements : \n",
+ "\n",
+ " 51 53 55 57 59\n",
+ " 61 63 65 67 69\n",
+ " 71 73 75 77 79\n",
+ " 81 83 85 87 89\n",
+ " 91 93 95 97 99\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file