diff options
Diffstat (limited to 'Programming_With_C_by_Byron_S._Gottfried')
18 files changed, 6603 insertions, 0 deletions
diff --git a/Programming_With_C_by_Byron_S._Gottfried/README.txt b/Programming_With_C_by_Byron_S._Gottfried/README.txt new file mode 100755 index 00000000..87b1d603 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/README.txt @@ -0,0 +1,10 @@ +Contributed By: Adnaan Ahmed +Course: btech +College/Institute/Organization: Heritage Institute of Technology +Department/Designation: Computer Science and Engineering +Book Title: Programming With C +Author: Byron S. Gottfried +Publisher: McGraw Hill Education (India) Private Limited, New Delhi +Year of publication: 2013 +Isbn: 978-0-07-014590-0 +Edition: 3rd Edition
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter1.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter1.ipynb new file mode 100755 index 00000000..c1eb1997 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter1.ipynb @@ -0,0 +1,62 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:be853ba23904264307d7e754c6f226daff90dc0e3bd2eca38f5b65419e2b80bc" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 1: Introductory Concepts <h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 1.6, Page number: 1.19<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "import math\n", + "\n", + "radius=5\n", + "area=math.pi*radius*radius\n", + "print \"Area = \",area\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area = 78.5398163397\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter10.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter10.ipynb new file mode 100755 index 00000000..ea2e1985 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter10.ipynb @@ -0,0 +1,556 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:3323f7a9a077b84fffdd30478d4b0d0b7f4032b86670a58b5b333d42bee04f15" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 10: Strings<h1> " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.3, Page Number: 10.3<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "st='Programming'\n", + "print 'The string is %s' %st" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The string is Programming\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.4, Page Number: 10.4<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "st='Hello World'\n", + "print 'The line is :'\n", + "print st" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The line is :\n", + "Hello World\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.6, Page Number: 10.5<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "strin='Welcome to python'\n", + "for i in strin:\n", + " print i," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "W e l c o m e t o p y t h o n\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.7, Page Number: 10.6<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "str1='Programming'\n", + "len1=len(str1)\n", + "print 'The length of the string is ',len1\n", + "\n", + "str2=str1\n", + "print 'First string is %s and copied string is %s' %(str1,str2)\n", + "\n", + "str3='Computer'\n", + "\n", + "if str1==str3:\n", + " print 'Both strings are equal'\n", + "elif str1<str2:\n", + " print 'First string is lesser than second string'\n", + "else:\n", + " print 'First string is greater than second string'\n", + " \n", + "tempstr=' with C'\n", + "str1=str1+tempstr\n", + "print 'The concated string is ',str1\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The length of the string is 11\n", + "First string is Programming and copied string is Programming\n", + "First string is greater than second string\n", + "The concated string is Programming with C\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.8,Page Number: 10.7<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def strlength(str1):\n", + " count=0\n", + " for i in str1:\n", + " count+=1\n", + " \n", + " return count\n", + "def strcopy(src):\n", + " dst=[]\n", + " for i in src:\n", + " dst.append(i)\n", + " \n", + " dst=''.join(dst)\n", + " \n", + " return dst\n", + "\n", + "\n", + "str1='New Delhi'\n", + "len1=strlength(str1)\n", + "print 'The length of the string is ',len1\n", + "\n", + "str2=strcopy(str1)\n", + "print 'First string is %s and copied string is %s' %(str1,str2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The length of the string is 9\n", + "First string is New Delhi and copied string is New Delhi\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.9,Page Number: 10.9<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def strcompare(str1,str2):\n", + " \n", + " len1=len(str1)\n", + " len2=len(str2)\n", + " \n", + " if len1<len2:\n", + " length=len1\n", + " else:\n", + " length=len2\n", + " \n", + " for i in xrange(0,length):\n", + " if str1[i]<str2[i]:\n", + " return -1\n", + " elif str1[i]>str2[i]:\n", + " return 1\n", + " \n", + " return 0\n", + "\n", + "str1='Programming'\n", + "str2='Computer'\n", + "status=strcompare(str1,str2)\n", + "if status==-1:\n", + " print 'First string is lesser than second string'\n", + "elif status==1:\n", + " print 'First string is greater than second string'\n", + "else:\n", + " print 'Both strings ae equal'\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First string is greater than second string\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.10, Page Number: 10.10<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def leftconcat(dst,src):\n", + " dst=src+dst\n", + " return dst\n", + "\n", + "def rightconcat(dst,src):\n", + " dst=dst+src\n", + " return dst\n", + "\n", + "str1='Hello'\n", + "str2='Friends'\n", + "\n", + "tempstr=leftconcat(str2,str1)\n", + "print 'The first string after left concatenation becomes ', tempstr\n", + "\n", + "tempstr=rightconcat(str2,str1)\n", + "print 'The first string after right concatenation becomes', tempstr\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The first string after left concatenation becomes HelloFriends\n", + "The first string after right concatenation becomes FriendsHello\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.11,Page Numbr: 10.12<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "str1='All good boys have bread'\n", + "count=0\n", + "\n", + "for i in str1:\n", + " if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n", + " count+=1\n", + " \n", + "print 'Total number of vowels in a given text are ',count" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Total number of vowels in a given text are 8\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.12, Page Number: 10.13<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "ch1='A'\n", + "ch2=ord(ch1)+3\n", + "print chr(ch2)\n", + "\n", + "ch1=chr(ord(ch1)+1)\n", + "print ch1\n", + "\n", + "print ord('a')\n", + "print ord('l')\n", + "\n", + "val=ord(ch1)*ch2\n", + "print val\n", + "\n", + "print chr(100)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "D\n", + "B\n", + "97\n", + "108\n", + "4488\n", + "d\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.13, Page Number: 10.13<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "text='I am studying 6 Theory Papers & 4 practicals'\n", + "\n", + "len1=len(text)\n", + "text=list(text)\n", + "for i in xrange(0,len1):\n", + " if text[i]>='a' and text[i]<='z':\n", + " text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n", + " \n", + " \n", + "text=''.join(text)\n", + "\n", + "print 'The text after converting lowercase alphabets to uppercase is '\n", + "print text" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The text after converting lowercase alphabets to uppercase is \n", + "I AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.14, Page Number: 10.14<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "text='The programming is a systematic process'\n", + "substr='pro'\n", + "text_len=len(text)\n", + "sub_len=len(substr)\n", + "text=list(text)\n", + "substr=list(substr)\n", + "\n", + "for i in xrange(0,text_len-sub_len+1):\n", + " \n", + " for j in xrange(0,sub_len):\n", + " \n", + " if text[i+j]==substr[j]:\n", + " continue\n", + " else:\n", + " break\n", + " \n", + " if j==sub_len-1:\n", + " print 'The substring is present from subscript %d onwards' %i\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The substring is present from subscript 4 onwards\n", + "The substring is present from subscript 32 onwards\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 10.15,Page number: 10.15<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def reorder(x):\n", + "\n", + " n=len(x)\n", + " for item in range(0,n-1):\n", + " for i in range(item+1,n):\n", + " if x[item]>x[i]:\n", + " temp=x[item]\n", + " x[item]=x[i]\n", + " x[i]=temp\n", + "\n", + "\n", + " return\n", + "\n", + "x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n", + "print 'Original list of strings :\\n\\n'\n", + "\n", + "for i in x:\n", + " print \"String : \",i\n", + "\n", + "reorder(x)\n", + "\n", + "print \"\\nReodered list of strings : \\n\\n\"\n", + "\n", + "for i in x:\n", + " print \"String : \",i\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original list of strings :\n", + "\n", + "\n", + "String : PACIFIC\n", + "String : ATLANTIC\n", + "String : INDIAN\n", + "String : CARIBBEAN\n", + "String : BERING\n", + "String : BLACK\n", + "String : RED\n", + "String : NORTH\n", + "String : BALTIC\n", + "String : CASPIAN\n", + "\n", + "Reodered list of strings : \n", + "\n", + "\n", + "String : ATLANTIC\n", + "String : BALTIC\n", + "String : BERING\n", + "String : BLACK\n", + "String : CARIBBEAN\n", + "String : CASPIAN\n", + "String : INDIAN\n", + "String : NORTH\n", + "String : PACIFIC\n", + "String : RED\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 15 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter11.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter11.ipynb new file mode 100755 index 00000000..d858cd07 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter11.ipynb @@ -0,0 +1,977 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:81f953770b0acac35782d6d61b5968cf0c7681b2e047ca75cfe8abd7244d5bab" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 11: Pointers<h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.1, Page 11.2<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "u,v=[3],[]\n", + "pu=u\n", + "v=pu\n", + "pv=v\n", + "\n", + "print \"u=\",u[0],\n", + "print \"&u=\",id(u),\n", + "print \"pu=\",id(u),\n", + "print \"*pu\",pu[0]\n", + "\n", + "print \"v=\",v[0],\n", + "print \"&v=\",id(v),\n", + "print \"pv=\",id(v),\n", + "print \"*pv\",pv[0]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "u= 3 &u= 56575840 pu= 56575840 *pu 3\n", + "v= 3 &v= 56575840 pv= 56575840 *pv 3\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.2, Page number: 11.3<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "v=[3]\n", + "u1=2*(v[0]+5)\n", + "pv=v\n", + "u2=2*(pv[0]+5)\n", + "\n", + "print \"u1=%d u2=%d\" %(u1,u2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "u1=16 u2=16\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.3, Page munber: 11.4<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "v=[3]\n", + "pv=v\n", + "print \"*pv=%d v=%d\" %(pv[0],v[0])\n", + "\n", + "pv[0]=0\n", + "print \"*pv=%d v=%d\" %(pv[0],v[0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "*pv=3 v=3\n", + "*pv=0 v=0\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.7, Page number: 11.6<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def funct1(u,v):\n", + " u=0\n", + " v=0\n", + " print \"Within funct1 : u=%d v=%d\" %(u,v)\n", + " return\n", + "\n", + "def funct2(u,v):\n", + " u[0]=0\n", + " v[0]=0\n", + " print \"Within funct2 : *pu=%d *pv=%d\" %(u[0],v[0])\n", + " return\n", + "\n", + "u=[1]\n", + "v=[3]\n", + "\n", + "print \"Before calling funct1: u=%d v=%d\" %(u[0],v[0])\n", + "funct1(u[0],v[0])\n", + "print \"After calling funct1 : u=%d v=%d\" %(u[0],v[0])\n", + "\n", + "print \"Before calling funct2: u=%d v=%d\" %(u[0],v[0])\n", + "funct2(u,v)\n", + "print \"After calling funct2 : u=%d v=%d\" %(u[0],v[0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before calling funct1: u=1 v=3\n", + "Within funct1 : u=0 v=0\n", + "After calling funct1 : u=1 v=3\n", + "Before calling funct2: u=1 v=3\n", + "Within funct2 : *pu=0 *pv=0\n", + "After calling funct2 : u=0 v=0\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.8, Page number: 11.8<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "def scan_line(line,pv,pc,pd,pw,po):\n", + "\n", + " for c in line:\n", + "\n", + " if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':\n", + " pv[0]+=1\n", + " elif c>='A' and c<='Z':\n", + " pc[0]+=1\n", + " elif c>='0' and c<='9':\n", + " pd[0]+=1\n", + " elif c==' ' or c=='\\t':\n", + " pw[0]+=1\n", + " else:\n", + " po[0]+=1\n", + "\n", + " return\n", + "\n", + "\n", + "vowel,consonants,digits,whitespc,other=[0],[0],[0],[0],[0]\n", + "line=\"Personal computers with memories in excess of 4096 KB are now quite common.\"\n", + "line=line.upper()\n", + "scan_line(line,vowel,consonants,digits,whitespc,other)\n", + "print \"\\n\\n\"\n", + "print \"No. of vowels : \",vowel[0]\n", + "print \"No. of consonants : \",consonants[0]\n", + "print \"No. of digits : \",digits[0]\n", + "print \"No. of whitespace characters : \",whitespc[0]\n", + "print \"No. of other characters : \",other[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "\n", + "No. of vowels : 23\n", + "No. of consonants : 35\n", + "No. of digits : 4\n", + "No. of whitespace characters : 12\n", + "No. of other characters : 1\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.12, Page number: 11.15<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "x=[10,11,12,13,14,15,16,17,18,19]\n", + "\n", + "for i in range(0,10):\n", + " print \"i=%d x[i]=%d *(x+1)=%d\" %(i,x[i],x[i]),\n", + " print \" &x[i]=\",id(x[i]),\n", + " print \" x+i=\",id(x[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i=0 x[i]=10 *(x+1)=10 &x[i]= 30696620 x+i= 30696620\n", + "i=1 x[i]=11 *(x+1)=11 &x[i]= 30696608 x+i= 30696608\n", + "i=2 x[i]=12 *(x+1)=12 &x[i]= 30696596 x+i= 30696596\n", + "i=3 x[i]=13 *(x+1)=13 &x[i]= 30696584 x+i= 30696584\n", + "i=4 x[i]=14 *(x+1)=14 &x[i]= 30696572 x+i= 30696572\n", + "i=5 x[i]=15 *(x+1)=15 &x[i]= 30696560 x+i= 30696560\n", + "i=6 x[i]=16 *(x+1)=16 &x[i]= 30696548 x+i= 30696548\n", + "i=7 x[i]=17 *(x+1)=17 &x[i]= 30696536 x+i= 30696536\n", + "i=8 x[i]=18 *(x+1)=18 &x[i]= 30696524 x+i= 30696524\n", + "i=9 x[i]=19 *(x+1)=19 &x[i]= 30696512 x+i= 30696512\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.14, Page number: 11.17<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def main():\n", + " global x\n", + " y=\"This string is declared within main\"\n", + " y=list(y)\n", + " x=list(x)\n", + " print x\n", + " print y\n", + "\n", + " return\n", + "\n", + "x=\"This string is declared externally\"\n", + "main()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', 'l', 'y']\n", + "['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 'm', 'a', 'i', 'n']\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.16, Page number: 11.19<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def reorder(x):\n", + " n=len(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", + "n=10\n", + "x=[]\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(x)\n", + "\n", + "print \"\\n\"\n", + "for i in range(0,n):\n", + " print \"i=%d x=%d\" %(i+1,x[i])\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", + "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", + "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": 18 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.17, Page number: 11.21<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "i=[4]\n", + "i[0]=1\n", + "f=0.3\n", + "d=0.005\n", + "c='*'\n", + "\n", + "px=i\n", + "print 'Values: i=%d f=%f d=%f c=%c \\n' %(i[0],f,d,c)\n", + "print 'Addresses: &i=%X &f=%X &d=%X &c=%X \\n' %(id(i[0]),id(f),id(d),id(c))\n", + "print 'Pointer Values: px=%X px+1=%X px+2=%X px+3=%X' %(id(px[0]),id(px[0]+1),id(px[0]+2),id(px[0]+3))\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Values: i=1 f=0.300000 d=0.005000 c=* \n", + "\n", + "Addresses: &i=1D46518 &f=32BD940 &d=32BD950 &c=1D9F158 \n", + "\n", + "Pointer Values: px=1D46518 px+1=1D4650C px+2=1D46500 px+3=1D464F4\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.18, Page number: 11.22<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "a=[1,2,3,4,5,6]\n", + "\n", + "px=a[0]\n", + "py=a[5]\n", + "\n", + "print 'px=%X py=%X \\n' %(id(px),id(py))\n", + "print 'py - px = %X' %(id(py)-id(px))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "px=1D46518 py=1D464DC \n", + "\n", + "py - px = -3C\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.22, Page number: 11.26<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\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+=2\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 3 5 7 9\n", + " 11 13 15 17 19\n", + " 21 23 25 27 29\n", + " 31 33 35 37 39\n", + " 41 43 45 47 49\n", + "\n", + " SECOND TABLE : \n", + "\n", + " 50 52 54 56 58\n", + " 60 62 64 66 68\n", + " 70 72 74 76 78\n", + " 80 82 84 86 88\n", + " 90 92 94 96 98\n", + "Sums of the elements : \n", + "\n", + " 51 55 59 63 67\n", + " 71 75 79 83 87\n", + " 91 95 99 103 107\n", + " 111 115 119 123 127\n", + " 131 135 139 143 147\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.24, Page number: 11.31<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\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+=2\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 3 5 7 9\n", + " 11 13 15 17 19\n", + " 21 23 25 27 29\n", + " 31 33 35 37 39\n", + " 41 43 45 47 49\n", + "\n", + " SECOND TABLE : \n", + "\n", + " 50 52 54 56 58\n", + " 60 62 64 66 68\n", + " 70 72 74 76 78\n", + " 80 82 84 86 88\n", + " 90 92 94 96 98\n", + "Sums of the elements : \n", + "\n", + " 51 55 59 63 67\n", + " 71 75 79 83 87\n", + " 91 95 99 103 107\n", + " 111 115 119 123 127\n", + " 131 135 139 143 147\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.26, Page number: 11.34<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def reorder(x):\n", + "\n", + " n=len(x)\n", + " for item in range(0,n-1):\n", + " for i in range(item+1,n):\n", + " if x[item]>x[i]:\n", + " temp=x[item]\n", + " x[item]=x[i]\n", + " x[i]=temp\n", + "\n", + "\n", + " return\n", + "\n", + "x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n", + "print 'Original list of strings :\\n\\n'\n", + "\n", + "for i in x:\n", + " print \"String : \",i\n", + "\n", + "reorder(x)\n", + "\n", + "print \"\\nReodered list of strings : \\n\\n\"\n", + "\n", + "for i in x:\n", + " print \"String : \",i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original list of strings :\n", + "\n", + "\n", + "String : PACIFIC\n", + "String : ATLANTIC\n", + "String : INDIAN\n", + "String : CARIBBEAN\n", + "String : BERING\n", + "String : BLACK\n", + "String : RED\n", + "String : NORTH\n", + "String : BALTIC\n", + "String : CASPIAN\n", + "\n", + "Reodered list of strings : \n", + "\n", + "\n", + "String : ATLANTIC\n", + "String : BALTIC\n", + "String : BERING\n", + "String : BLACK\n", + "String : CARIBBEAN\n", + "String : CASPIAN\n", + "String : INDIAN\n", + "String : NORTH\n", + "String : PACIFIC\n", + "String : RED\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.28, Page number: 11.37<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def convert(mm,dd,yy):\n", + " yy-=1900\n", + " ndays=long(30.42*(mm-1)+dd)\n", + " if mm==2:\n", + " ndays+=1\n", + " if mm>2 and mm<8:\n", + " ndays-=1\n", + " if yy%4==0 and mm<2:\n", + " ndays+=1\n", + "\n", + " ncycles=yy/4\n", + " ndays+=ncycles*1461\n", + "\n", + " nyears=yy%4\n", + " if nyears>0:\n", + " ndays+=365*nyears+1\n", + " if ndays>59:\n", + " ndays-=1\n", + "\n", + " day=ndays%7\n", + "\n", + " return day\n", + "\n", + "def main(mm,dd,yy):\n", + " day_of_week=convert(mm,dd,yy)\n", + " print \"%s, %s %d %d\" %(weekday[day_of_week],month[mm-1],dd,yy)\n", + " return\n", + " \n", + "weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']\n", + "month=['January','February','March','April','May','June','July','August',\\\n", + " 'September','October','November','December']\n", + "\n", + "main(10,29,1929)\n", + "print\n", + "main(8,15,1945)\n", + "print\n", + "main(7,20,1969)\n", + "print\n", + "main(5,24,1997)\n", + "print\n", + "main(8,30,2010)\n", + "print\n", + "main(4,12,2069)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tuesday, October 29 1929\n", + "\n", + "Wednesday, August 15 1945\n", + "\n", + "Sunday, July 20 1969\n", + "\n", + "Saturday, May 24 1997\n", + "\n", + "Monday, August 30 2010\n", + "\n", + "Friday, April 12 2069\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Example 11.30, Page number: 11.44<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def ratio_choose(freq,a,m,n):\n", + "\n", + " if freq=='C':\n", + " ratio=md3(a,m,n)\n", + " elif freq=='D':\n", + " ratio=md2(a,m,n)\n", + " else:\n", + " ratio=md1(a,m,n)\n", + "\n", + " return ratio\n", + "\n", + "\n", + "def table(freq,a,m,n):\n", + "\n", + "\n", + " print \"Interest Rate Future Amount\\n\\n\"\n", + " for count in range(1,21):\n", + " i=0.01*count\n", + " f=a*ratio_choose(freq,i,m,n)\n", + " print \" %2d %.2f\" %(count,f)\n", + "\n", + " return\n", + "\n", + "def md1(i,m,n):\n", + " \n", + " factor=1+i/m\n", + " ratio=12*(factor**(m*n)-1)/i\n", + " return ratio\n", + "\n", + "def md2(i,m,n):\n", + "\n", + " factor=1+i/m\n", + " ratio=(factor**(m*n)-1)/(factor**(m/12)-1)\n", + " return ratio\n", + "\n", + "def md3(i,dummy,n):\n", + " ratio=(10**(i*n)-1)/(10**(i/12)-1)\n", + " return ratio\n", + "\n", + "def main(freq):\n", + " m=0\n", + " freq=freq.upper()\n", + " if freq=='A':\n", + " m=1\n", + " print \"\\nAnual Compounding\\n\"\n", + " \n", + " elif freq=='S':\n", + " m=2\n", + " print \"\\nSemiannual Compounding\\n\"\n", + " \n", + " elif freq=='Q':\n", + " print \"\\nQuaterly Compounding\\n\"\n", + " m=4\n", + " \n", + " elif freq=='M':\n", + " m=12\n", + " print \"\\nMonthly Compounding\\n\"\n", + " \n", + " elif freq=='D':\n", + " m=360\n", + " print \"\\nDaily Compounding\\n\"\n", + "\n", + " elif freq=='C':\n", + " m=0\n", + " print \"\\nContinuous Compounding\\n\"\n", + " \n", + " else:\n", + " print \"\\nERROR!!! Please Repeat\\n\\n\"\n", + " return\n", + " \n", + " return m\n", + "\n", + "\n", + "a,n,freq=100,3,'m'\n", + "print \"FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\\n\\n\"\n", + "print \"Frequency of Compunding (A,S,Q,M,D,C): \",freq\n", + "print \"Amount of Each Monthly Payement : \",a\n", + "print \"Number of years: \",n\n", + "m=main(freq)\n", + "\n", + "table(freq,a,m,n)\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\n", + "\n", + "\n", + "Frequency of Compunding (A,S,Q,M,D,C): m\n", + "Amount of Each Monthly Payement : 100\n", + "Number of years: 3\n", + "\n", + "Monthly Compounding\n", + "\n", + "Interest Rate Future Amount\n", + "\n", + "\n", + " 1 3653.00\n", + " 2 3707.01\n", + " 3 3762.06\n", + " 4 3818.16\n", + " 5 3875.33\n", + " 6 3933.61\n", + " 7 3993.01\n", + " 8 4053.56\n", + " 9 4115.27\n", + " 10 4178.18\n", + " 11 4242.31\n", + " 12 4307.69\n", + " 13 4374.33\n", + " 14 4442.28\n", + " 15 4511.55\n", + " 16 4582.17\n", + " 17 4654.18\n", + " 18 4727.60\n", + " 19 4802.45\n", + " 20 4878.78\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 25 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb new file mode 100755 index 00000000..09e4c7e9 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter12.ipynb @@ -0,0 +1,565 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 12: Structures and Unions<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.1, Page 12.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*20\n",
+ "\n",
+ "class account(Structure):\n",
+ "\t_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_int)]\n",
+ "\n",
+ "customer=account(12632,'r','Joseph',1200)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.4, Page 12.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*20\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\t\n",
+ "class account(Structure):\n",
+ "\t_fields_=[('acct_no',c_int),('acct_type',c_char),('name',string),('balance',c_float),('lastpayment',date)]\n",
+ "\t\n",
+ "customer=account(12345,'r','John W. Smith',586.30,date(5,24,90))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.6, Page 12.6<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*20\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t\n",
+ "\t_fields_=[('name',string),('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\n",
+ "birthday=[]\n",
+ "birthday.append(date('Amy',12,30,73))\n",
+ "birthday.append(date('Gail',5,13,66))\n",
+ "birthday.append(date('Marc',7,15,72))\n",
+ "birthday.append(date('Marla',11,29,70))\n",
+ "birthday.append(date('Megan',2,4,77))\n",
+ "birthday.append(date('Sharon',12,29,63))\n",
+ "birthday.append(date('Susan',4,12,69))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.14, Page 12.13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*50\n",
+ "\n",
+ "def writeoutput(obj):\n",
+ "\tprint \"Name : \",obj.name,\n",
+ "\tprint \"\\t Account Number : \",obj.acct_no\n",
+ "\tprint \"Street : \",obj.street\n",
+ "\tprint \"City : \",obj.city\n",
+ "\tprint \"Old Balance : %7.2f\" %(obj.oldbalance)\n",
+ "\tprint \"Payment : %7.2f\" %(obj.payment)\n",
+ "\tprint \"New Balance : %7.2f\" %(obj.newbalance)\n",
+ "\tprint \"Account Status : \",\n",
+ "\t\n",
+ "\tif obj.acct_type=='C':\n",
+ "\t\tprint \"CURRENT\\n\\n\"\n",
+ "\telif obj.acct_type=='O':\n",
+ "\t\tprint \"OVERDUE\\n\\n\"\n",
+ "\telse:\n",
+ "\t\tprint \"DELINQUENT\\n\\n\"\n",
+ "\t\t\n",
+ "\treturn\n",
+ "\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\t\n",
+ "\n",
+ "class account(Structure):\n",
+ "\t_fields_=[('name',string),('street',string),('city',string),('acct_no',c_int),('oldbalance',c_float),('payment',c_float),('lastpayment',date),('newbalance',c_float),('acct_type',c_char)]\n",
+ "\t\t\n",
+ "print \"CUSOMER BILLING SYSTEM\\n\\n\"\n",
+ "customer=[]\n",
+ "name='Steve Johnson'\n",
+ "street='123 Mountainview Drive '\n",
+ "city='Denver . CO'\n",
+ "acct_no=4208\n",
+ "oldbalance=247.88\n",
+ "payment=25.00\n",
+ "lastpay=date(6,14,1998)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Susan Richards'\n",
+ "street='4383 Aligator Blvd'\n",
+ "city='Fort Lauderdale. FL'\n",
+ "acct_no=2219\n",
+ "oldbalance=135.00\n",
+ "payment=135.00\n",
+ "lastpay=date(8,10,2000)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Martin Peterson'\n",
+ "street='1787 Pacific Parkway'\n",
+ "city='San Diego. CA'\n",
+ "acct_no=8452\n",
+ "oldbalance=387.42\n",
+ "payment=35.00\n",
+ "lastpay=date(9,22,1999)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Phyllis Smith'\n",
+ "street='1000 Great White Way'\n",
+ "city='New York. NY'\n",
+ "acct_no=711\n",
+ "oldbalance=260.00\n",
+ "payment=0.00\n",
+ "lastpay=date(11,27,2001)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "\n",
+ "\n",
+ "for i in range(0,4):\n",
+ "\tif customer[i].payment>0:\n",
+ "\t\tif customer[i].payment<0.1*customer[i].oldbalance:\n",
+ "\t\t\tcustomer[i].acct_type='O'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\telse:\n",
+ "\t\tif customer[i].oldbalance>0:\n",
+ "\t\t\tcustomer[i].acct_type='D'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\t\n",
+ "\tcustomer[i].newbalance=customer[i].oldbalance-customer[i].payment\n",
+ "\t\n",
+ "\twriteoutput(customer[i])\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "CUSOMER BILLING SYSTEM\n",
+ "\n",
+ "\n",
+ "Name : Steve Johnson \t Account Number : 4208\n",
+ "Street : 123 Mountainview Drive \n",
+ "City : Denver . CO\n",
+ "Old Balance : 247.88\n",
+ "Payment : 25.00\n",
+ "New Balance : 222.88\n",
+ "Account Status : CURRENT\n",
+ "\n",
+ "\n",
+ "Name : Susan Richards \t Account Number : 2219\n",
+ "Street : 4383 Aligator Blvd\n",
+ "City : Fort Lauderdale. FL\n",
+ "Old Balance : 135.00\n",
+ "Payment : 135.00\n",
+ "New Balance : 0.00\n",
+ "Account Status : CURRENT\n",
+ "\n",
+ "\n",
+ "Name : Martin Peterson \t Account Number : 8452\n",
+ "Street : 1787 Pacific Parkway\n",
+ "City : San Diego. CA\n",
+ "Old Balance : 387.42\n",
+ "Payment : 35.00\n",
+ "New Balance : 352.42\n",
+ "Account Status : OVERDUE\n",
+ "\n",
+ "\n",
+ "Name : Phyllis Smith \t Account Number : 711\n",
+ "Street : 1000 Great White Way\n",
+ "City : New York. NY\n",
+ "Old Balance : 260.00\n",
+ "Payment : 0.00\n",
+ "New Balance : 260.00\n",
+ "Account Status : DELINQUENT\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.25, Page 12.29<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*10\n",
+ "\n",
+ "class record(Structure):\n",
+ "\t_fields_=[('name',string),('acct_no',c_int),('acct_type',c_char),('balance',c_float)]\n",
+ "\t\n",
+ "def adjust(obj):\n",
+ "\tobj.name='Jones'\n",
+ "\tobj.acct_no=9999\n",
+ "\tobj.acct_type='R'\n",
+ "\tobj.balance=99.99\n",
+ "\treturn\n",
+ "\n",
+ "\n",
+ "customer=record('Smith',3333,'C',33.33)\n",
+ "print customer.name,customer.acct_no,customer.acct_type,\n",
+ "print \"%.2f\" %customer.balance\n",
+ "\n",
+ "adjust(customer)\n",
+ "\n",
+ "print customer.name,customer.acct_no,customer.acct_type,\n",
+ "print \"%.2f\" %customer.balance\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Smith 3333 C 33.33\n",
+ "Jones 9999 R 99.99\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.32, Page 12.42<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "class node():\n",
+ "\t\n",
+ "\tdef __init__(self,data):\n",
+ "\t\tself.data=data\n",
+ "\t\tself.next=None\n",
+ "\n",
+ "\t\t\n",
+ "class list():\n",
+ "\t\n",
+ "\tdef __init__(self):\n",
+ "\t\tself.head=None\n",
+ "\t\tself.tail=None\n",
+ "\t\n",
+ "\tdef insert(self,x):\n",
+ "\t\t\n",
+ "\t\te=node(x)\n",
+ "\t\t\n",
+ "\t\tif self.head==None:\n",
+ "\t\t\tself.head=e\n",
+ "\t\t\tself.tail=e\n",
+ "\t\t\t\n",
+ "\t\telse:\n",
+ "\t\t\tself.tail.next=e\n",
+ "\t\t\tself.tail=e\n",
+ "\t\t\t\n",
+ "def display(ptr):\n",
+ "\tnptr=ptr.head\n",
+ "\twhile nptr!=None:\n",
+ "\t\tprint nptr.data\n",
+ "\t\tnptr=nptr.next\n",
+ "\t\n",
+ "\treturn\n",
+ "\t\n",
+ "def delete(ptr,element):\n",
+ "\tnptr=ptr.head\n",
+ "\twhile nptr.next.data!=element:\n",
+ "\t\tnptr=nptr.next\n",
+ "\t\tif nptr.next==None:\n",
+ "\t\t\treturn\n",
+ "\t\n",
+ "\tdptr=nptr.next\n",
+ "\tnptr.next=dptr.next\n",
+ "\tdel dptr\n",
+ "\treturn\n",
+ "\t\n",
+ "p=list()\n",
+ "p.insert('BOSTON')\n",
+ "p.insert('CHICAGO')\n",
+ "p.insert('DENVER')\n",
+ "p.insert('NEW YORK')\n",
+ "p.insert('PITTSBURG')\n",
+ "\n",
+ "display(p)\n",
+ "print '\\n\\nAFTER DELETING DENVER\\n\\n'\n",
+ "delete(p,'DENVER')\n",
+ "display(p)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "BOSTON\n",
+ "CHICAGO\n",
+ "DENVER\n",
+ "NEW YORK\n",
+ "PITTSBURG\n",
+ "\n",
+ "\n",
+ "AFTER DELETING DENVER\n",
+ "\n",
+ "\n",
+ "BOSTON\n",
+ "CHICAGO\n",
+ "NEW YORK\n",
+ "PITTSBURG\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.35, Page 12.57<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*20\n",
+ "\n",
+ "class id(Union):\n",
+ "\t_fields_=[('color',c_char),('size',c_int)]\n",
+ "\t\n",
+ "class clothes(Structure):\n",
+ "\t_fields_=[('manufacturer',string),('cost',c_float),('description',id)]\n",
+ "\t\n",
+ "shirt=clothes()\n",
+ "shirt.description.color='w'\n",
+ "print \"%c %d\\n\" %(shirt.description.color,shirt.description.size)\n",
+ "\n",
+ "shirt.description.size=12\n",
+ "print \"%c %d\" %(shirt.description.color,shirt.description.size)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "w 119\n",
+ "\n",
+ "\f",
+ " 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 12.37, Page 12.59<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "import math\n",
+ "\n",
+ "\n",
+ "class nvals(Union):\n",
+ "\t_fields_=[('fexp',c_float),('nexp',c_int)]\n",
+ "\t\n",
+ "class values(Structure):\n",
+ "\t_fields_=[('x',c_float),('flag',c_char),('exp',nvals)]\n",
+ "\t\n",
+ "def power(a):\n",
+ "\ty=a.x\n",
+ "\t\n",
+ "\tif a.flag=='i':\n",
+ "\t\tif a.exp.nexp==0:\n",
+ "\t\t\ty=1.0\n",
+ "\t\telse:\n",
+ "\t\t\ti=1\n",
+ "\t\t\twhile i<abs(a.exp.nexp):\n",
+ "\t\t\t\ty*=a.x\n",
+ "\t\t\t\ti+=1\n",
+ "\t\t\t\n",
+ "\t\t\tif a.exp.nexp<0:\n",
+ "\t\t\t\ty=1.0/y\n",
+ "\t\n",
+ "\telse:\n",
+ "\t\ty=math.exp(a.exp.fexp*math.log(a.x))\n",
+ "\t\n",
+ "\treturn y\n",
+ "\t\n",
+ "def main(in1,n):\n",
+ "\ta=values()\n",
+ "\ta.x=in1\n",
+ "\t\n",
+ "\ti=int(n)\n",
+ "\t\n",
+ "\tif i==n:\n",
+ "\t\ta.flag='i'\n",
+ "\telse:\n",
+ "\t\ta.flag='f'\n",
+ "\t\n",
+ "\tif a.flag=='i':\n",
+ "\t\ta.exp.nexp=i\n",
+ "\telse:\n",
+ "\t\ta.exp.fexp=n\n",
+ "\t\t\n",
+ "\tif a.flag=='f' and a.x<=0.0:\n",
+ "\t\tprint \"ERROR - Cannot raise a non-positive number to a floating point power\"\n",
+ "\telse:\n",
+ "\t\ty=power(a)\n",
+ "\t\tprint \"y=%.4f\" %y\n",
+ "\t\n",
+ "\treturn\n",
+ "\n",
+ "print \"\\nx=2\\nn=3\"\n",
+ "main(2,3)\n",
+ "print \"\\nx=-2\\nn=3\"\n",
+ "main(-2,3)\n",
+ "print \"\\nx=2.2\\nn=3.3\"\n",
+ "main(2.2,3.3)\n",
+ "print \"\\nx=-2.2\\nn=3.3\"\n",
+ "main(-2.2,3.3)\n",
+ "\n",
+ "\n",
+ "\t\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x=2\n",
+ "n=3\n",
+ "y=8.0000\n",
+ "\n",
+ "x=-2\n",
+ "n=3\n",
+ "y=-8.0000\n",
+ "\n",
+ "x=2.2\n",
+ "n=3.3\n",
+ "y=13.4895\n",
+ "\n",
+ "x=-2.2\n",
+ "n=3.3\n",
+ "ERROR - Cannot raise a non-positive number to a floating point power\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter13.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter13.ipynb new file mode 100755 index 00000000..fe4b86df --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter13.ipynb @@ -0,0 +1,180 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 13: File Handling<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.3, Page 12.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "fp=open(\"output.txt\",'w')\n",
+ "for k in range(65,91):\n",
+ "\tfp.write(str(k)+'\\n')\n",
+ "fp.close()\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.7, Page 13.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*40\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\t\n",
+ "class account(Structure):\n",
+ "\t_fields_=[('name',string),('street',string),('city',string),('acct_no',c_int),('oldbalance',c_float),('payment',c_float),('lastpayment',date),('acct_type',c_char),('newbalance',c_float)]\n",
+ "\n",
+ "\n",
+ "def writefile(obj,fpt):\n",
+ "\tfpt.write(obj.name+'\\n')\n",
+ "\tfpt.write(obj.city+'\\n')\n",
+ "\tfpt.write(str(obj.acct_no)+'\\n')\n",
+ "\tfpt.write(obj.acct_type+'\\n')\n",
+ "\tfpt.write(str(obj.oldbalance)+'\\n')\n",
+ "\tfpt.write(str(obj.newbalance)+'\\n')\n",
+ "\tfpt.write(str(obj.payment)+'\\n')\n",
+ "\tfpt.write(str(obj.lastpayment.month)+' ')\n",
+ "\tfpt.write(str(obj.lastpayment.day)+' ')\n",
+ "\tfpt.write(str(obj.lastpayment.year)+'\\n\\n')\n",
+ "\t\n",
+ "\treturn\t\n",
+ "\t\n",
+ "fpt=open('records.txt','w')\n",
+ "\n",
+ "\n",
+ "customer=[]\n",
+ "\n",
+ "name='Steve Johnson'\n",
+ "street='123 Mountainview Drive '\n",
+ "city='Denver . CO'\n",
+ "acct_no=4208\n",
+ "oldbalance=247.88\n",
+ "payment=25.00\n",
+ "lastpay=date(6,14,1998)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Susan Richards'\n",
+ "street='4383 Aligator Blvd'\n",
+ "city='Fort Lauderdale. FL'\n",
+ "acct_no=2219\n",
+ "oldbalance=135.00\n",
+ "payment=135.00\n",
+ "lastpay=date(8,10,2000)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Martin Peterson'\n",
+ "street='1787 Pacific Parkway'\n",
+ "city='San Diego. CA'\n",
+ "acct_no=8452\n",
+ "oldbalance=387.42\n",
+ "payment=35.00\n",
+ "lastpay=date(9,22,1999)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Phyllis Smith'\n",
+ "street='1000 Great White Way'\n",
+ "city='New York. NY'\n",
+ "acct_no=711\n",
+ "oldbalance=260.00\n",
+ "payment=0.00\n",
+ "lastpay=date(11,27,2001)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "\n",
+ "\n",
+ "for i in range(0,4):\n",
+ "\tif customer[i].payment>0:\n",
+ "\t\tif customer[i].payment<0.1*customer[i].oldbalance:\n",
+ "\t\t\tcustomer[i].acct_type='O'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\telse:\n",
+ "\t\tif customer[i].oldbalance>0:\n",
+ "\t\t\tcustomer[i].acct_type='D'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\t\n",
+ "\tcustomer[i].newbalance=customer[i].oldbalance-customer[i].payment\n",
+ "\t\n",
+ "\twritefile(customer[i],fpt)\n",
+ "\n",
+ "fpt.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.13, Page 13.28<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "fp=open('binval.txt','wb')\n",
+ "\n",
+ "for i in range(1000,11001):\n",
+ "\tfp.write(str(i))\n",
+ "\n",
+ "fp.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter14.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter14.ipynb new file mode 100755 index 00000000..d89e7c72 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter14.ipynb @@ -0,0 +1,232 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 14: Low-Level Programming<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 14.14, Page number: 14.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "a=0xf05a\n",
+ "\n",
+ "print \"%u\" %(a)\n",
+ "print \"%x\" %(a>>6)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "61530\n",
+ "3c1\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 14.16, Page number: 14.13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "a=1\n",
+ "nbits=16\n",
+ "m=0x1<<(nbits-1)\n",
+ "\n",
+ "def main(a):\n",
+ " mask=abs(m)\n",
+ " for count in range(1,nbits+1):\n",
+ " if a&mask:\n",
+ " b=1\n",
+ " else:\n",
+ " b=0\n",
+ "\n",
+ " print \"%x\" %(b),\n",
+ " if count%4==0:\n",
+ " print \"\\t\",\n",
+ "\n",
+ " mask>>=1\n",
+ " return\n",
+ "print \"\\n1-->\\n\"\n",
+ "main(1)\n",
+ "print \"\\n-1-->\\n\"\n",
+ "main(-1)\n",
+ "print \"\\n0-->\\n\"\n",
+ "main(0)\n",
+ "print \"\\n2-->\\n\"\n",
+ "main(2)\n",
+ "print \"\\n-2-->\\n\"\n",
+ "main(-2)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1-->\n",
+ "\n",
+ "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 0 1 \t\n",
+ "-1-->\n",
+ "\n",
+ "1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t\n",
+ "0-->\n",
+ "\n",
+ "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t\n",
+ "2-->\n",
+ "\n",
+ "0 0 0 0 \t0 0 0 0 \t0 0 0 0 \t0 0 1 0 \t\n",
+ "-2-->\n",
+ "\n",
+ "1 1 1 1 \t1 1 1 1 \t1 1 1 1 \t1 1 1 0 \t"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 14.23, Page Number: 14.20"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*50\n",
+ "\n",
+ "\n",
+ "def convert(mm,dd,yy):\n",
+ " yy-=1900\n",
+ " ndays=long(30.42*(mm-1)+dd)\n",
+ " if mm==2:\n",
+ " ndays+=1\n",
+ " if mm>2 and mm<8:\n",
+ " ndays-=1\n",
+ " if yy%4==0 and mm<2:\n",
+ " ndays+=1\n",
+ "\n",
+ " ncycles=yy/4\n",
+ " ndays+=ncycles*1461\n",
+ "\n",
+ " nyears=yy%4\n",
+ " if nyears>0:\n",
+ " ndays+=365*nyears+1\n",
+ " if ndays>59:\n",
+ " ndays-=1\n",
+ "\n",
+ " day=ndays%7\n",
+ " yy+=1900\n",
+ " if yy%4==0 and yy%400!=0:\n",
+ " day+=1\n",
+ " return day\n",
+ "\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\t\n",
+ "\n",
+ "class person(Structure):\n",
+ "\t_fields_=[('name',string),('birthdate',date)]\n",
+ "\t\t\n",
+ "student=[]\n",
+ "\n",
+ "weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']\n",
+ "\n",
+ "month=['January','February','March','April','May','June','July','August',\\\n",
+ " 'September','October','November','December']\n",
+ "\n",
+ "\n",
+ "name='Rob Smith'\n",
+ "birthdate=date(7,20,1972)\n",
+ "student.append(person(name,birthdate))\n",
+ "\n",
+ "name='Judy Thompson'\n",
+ "birthdate=date(11,27,1983)\n",
+ "student.append(person(name,birthdate))\n",
+ "\n",
+ "name='Jim Williams'\n",
+ "birthdate=date(12,29,1998)\n",
+ "student.append(person(name,birthdate))\n",
+ "\n",
+ "name='Mort Davis'\n",
+ "birthdate=date(6,10,2010)\n",
+ "student.append(person(name,birthdate))\n",
+ "\n",
+ "\n",
+ "\t\n",
+ "for count in xrange(0,4):\n",
+ " day_of_week=convert(student[count].birthdate.month,student[count].birthdate.day,student[count].birthdate.year)\n",
+ " print student[count].name,\n",
+ " print \" %s, %s %d %d \\n\" %(weekday[day_of_week],month[student[count].birthdate.month-1],student[count].birthdate.day,student[count].birthdate.year)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Rob Smith Thursday, July 20 1972 \n",
+ "\n",
+ "Judy Thompson Sunday, November 27 1983 \n",
+ "\n",
+ "Jim Williams Tuesday, December 29 1998 \n",
+ "\n",
+ "Mort Davis Thursday, June 10 2010 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter2.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter2.ipynb new file mode 100755 index 00000000..178b56ae --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter2.ipynb @@ -0,0 +1,117 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 2: Introduction to C Programming<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.1, Page number: 2.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "print \"Welcome to python programming\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome to python programming\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.5, Page number: 2.5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "sum=10+20\n",
+ "print sum\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2.6, Page number: 2.5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "num1,num2=50,25\n",
+ "Sum=num1+num2\n",
+ "print \"addition of %d and %d = %d\" %(num1,num2,Sum)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "addition of 50 and 25 = 75\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter3.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter3.ipynb new file mode 100755 index 00000000..fbf10f10 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter3.ipynb @@ -0,0 +1,132 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 3: Operators and Expressions<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 3.13, Page number: 3.9 <h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import ctypes as ct\n",
+ "\n",
+ "print \"interger : \",ct.sizeof(ct.c_int())\n",
+ "print \"float : \",ct.sizeof(ct.c_float())\n",
+ "print \"double : \",ct.sizeof(ct.c_double())\n",
+ "print \"character : \",ct.sizeof(ct.c_char())\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "interger : 4\n",
+ "float : 4\n",
+ "double : 8\n",
+ "character : 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 3.30, Page number: 3.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "print \"for the equation ax^2+bx+c \\n\"\n",
+ "\n",
+ "a=2\n",
+ "b=6\n",
+ "c=3\n",
+ "\n",
+ "root=(b*b-4*a*c)**0.5\n",
+ "x1=(-b + root)/(2*a)\n",
+ "x2=(-b - root)/(2*a)\n",
+ "\n",
+ "print \"x1=\",x1\n",
+ "print \"x2=\",x2\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "for the equation ax^2+bx+c \n",
+ "\n",
+ "x1= -0.633974596216\n",
+ "x2= -2.36602540378\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 3.31, Page number: 3.20<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "lower=\"l\"\n",
+ "upper=lower.upper()\n",
+ "print upper\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "L\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter4.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter4.ipynb new file mode 100755 index 00000000..21edac6f --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter4.ipynb @@ -0,0 +1,561 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 4: Data Input and Output<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.4, Page number: 4.4 <h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "ch='a'\n",
+ "print 'equivalent uppercase character is ',ch.upper()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "equivalent uppercase character is A\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.16, Page number: 4.14<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "i,j=2.0,3.0\n",
+ "print \"%f %f %f %f\" %(i,i,i+j,math.sqrt(i+j))\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2.000000 2.000000 5.000000 2.236068\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.18, Page number: 4.15<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x,y=5000.0,0.0025\n",
+ "\n",
+ "print \"%f %f %f %f\\n\\n\" %(x,y,x*y,x/y)\n",
+ "print \"%e %e %e %e\" %(x,y,x*y,x/y)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5000.000000 0.002500 12.500000 2000000.000000\n",
+ "\n",
+ "\n",
+ "5.000000e+03 2.500000e-03 1.250000e+01 2.000000e+06\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.19, Page number: 4.16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "line=\"The PITTSBURG STEELERS is one of America's favorite football teams!\"\n",
+ "print \"%s\" %(line)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The PITTSBURG STEELERS is one of America's favorite football teams!\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.20, Page number: 4.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "i=12345\n",
+ "x=345.678\n",
+ "\n",
+ "print \"%3d %5d %8d\\n\\n\" %(i,i,i)\n",
+ "print \"%3f %10f %13f\" %(x,x,x)\n",
+ "print \"%3e %13e %16e\" %(x,x,x)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "12345 12345 12345\n",
+ "\n",
+ "\n",
+ "345.678000 345.678000 345.678000\n",
+ "3.456780e+02 3.456780e+02 3.456780e+02\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.21, Page number: 4.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "i=12345\n",
+ "x=345.678\n",
+ "\n",
+ "print \"%3d %5d %8d\\n\\n\" %(i,i,i)\n",
+ "print \"%3g %10g %13g\" %(x,x,x)\n",
+ "print \"%3g %13g %16g\" %(x,x,x)\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "12345 12345 12345\n",
+ "\n",
+ "\n",
+ "345.678 345.678 345.678\n",
+ "345.678 345.678 345.678\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.22, Page number: 4.18<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "x=123.456\n",
+ "\n",
+ "print \"%7f %7.3f %7.1f\" %(x,x,x)\n",
+ "print \"%12e %12.5e %12.3e\" %(x,x,x)\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "123.456000 123.456 123.5\n",
+ "1.234560e+02 1.23456e+02 1.235e+02\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.23, Page number: 4.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "x=123.456\n",
+ "\n",
+ "print \"%f %.3f %.1f\" %(x,x,x)\n",
+ "print \"%e %.5e %.3e\" %(x,x,x)\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "123.456000 123.456 123.5\n",
+ "1.234560e+02 1.23456e+02 1.235e+02\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.24, Page number: 4.20<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "line=\"hexadecimal\"\n",
+ "\n",
+ "print \"%10s %15s %15.5s %.5s\" %(line,line,line,line)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "hexadecimal hexadecimal hexad hexad\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.26, Page number 4.21<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "a=0x80ec\n",
+ "b=0.3e-12\n",
+ "\n",
+ "print \"%4x %10.2e\\n\\n\" %(a,b)\n",
+ "print \"%4X %10.2E\" %(a,b)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "80ec 3.00e-13\n",
+ "\n",
+ "\n",
+ "80EC 3.00E-13\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.27, Page number: 4.22<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "i,x,y=123,12.0,-3.3\n",
+ "\n",
+ "print \":%6d %7.0f %10.1e: \\n\" %(i,x,y)\n",
+ "print \":%-6d %-7.0f %-10.1e: \\n\" %(i,x,y)\n",
+ "print \":%+6d %+7.0f %+10.1e: \\n\" %(i,x,y)\n",
+ "print \":%-+6d %-+7.0f %-+10.1e: \\n\" %(i,x,y)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ ": 123 12 -3.3e+00: \n",
+ "\n",
+ ":123 12 -3.3e+00 : \n",
+ "\n",
+ ": +123 +12 -3.3e+00: \n",
+ "\n",
+ ":+123 +12 -3.3e+00 : \n",
+ "\n",
+ ": 12 12. -3.3 -3.30000: \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.28, Page number: 4.23<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "i,j,k=1234,01777,0xa08c\n",
+ "\n",
+ "print \":%8u %8o %8x:\\n\" %(i,j,k)\n",
+ "print \":%-8u %-8o %-8x:\\n\" %(i,j,k)\n",
+ "print \":%08u %08o %08x:\\n\" %(i,j,k)\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ ": 1234 1777 a08c:\n",
+ "\n",
+ ":1234 1777 a08c :\n",
+ "\n",
+ ": 1234 01777 0xa08c:\n",
+ "\n",
+ ":00001234 00001777 0000a08c:\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.29, Page number: 4.24<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "line=\"lower-case\"\n",
+ "\n",
+ "print \":%15s %15.5s %.5s:\" %(line,line,line)\n",
+ "print \":%-15s %-15.5s %-.5s:\" %(line,line,line)\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ ": lower-case lower lower:\n",
+ ":lower-case lower lower:\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.30, Page number: 4.24<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "a,b,x1,x2=2.2,-6.2,0.005,-12.88\n",
+ "\n",
+ "print \"$%4.2f %7.1f%%\\n\" %(a,b)\n",
+ "print \"x1=%7.3f x2=%7.3f\" %(x1,x2)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "$2.20 -6.2%\n",
+ "\n",
+ "x1= 0.005 x2=-12.880\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.32, Page number: 4.26<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "name=\"Robert Smith\"\n",
+ "score1=88\n",
+ "score2=62.5\n",
+ "score3=90\n",
+ "\n",
+ "average=(float)(score1+score2+score3)/3\n",
+ "print \"Name: %-s\" %(name)\n",
+ "print \"Score1: %-5.1f\" %(score1)\n",
+ "print \"Score2: %-5.1f\" %(score2)\n",
+ "print \"Score3: %-5.1f\" %(score3)\n",
+ "print \"Average: %-5.1f\" %(average)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name: Robert Smith\n",
+ "Score1: 88.0 \n",
+ "Score2: 62.5 \n",
+ "Score3: 90.0 \n",
+ "Average: 80.2 \n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter5.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter5.ipynb new file mode 100755 index 00000000..ea09f2bd --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter5.ipynb @@ -0,0 +1,96 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Preparing And Running A Complete C Program<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 5.2, Page number: 5.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "p,r,n=10000,10,3\n",
+ "i=r/100.0\n",
+ "\n",
+ "f=p*math.pow(1+i,n)\n",
+ "print \"The final value (F) is: %.2f\" %f\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The final value (F) is: 13310.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 5.4, Page number: 5.7<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "a,b,c=2.0,5.0,3.0\n",
+ "d=math.sqrt(b*b-(4*a*c))\n",
+ "x1=(-b+d)/(2*a)\n",
+ "x2=(-b-d)/(2*a)\n",
+ "\n",
+ "print \"x1 = %e x2 = %e\" %(x1,x2)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x1 = -1.000000e+00 x2 = -1.500000e+00\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter6.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter6.ipynb new file mode 100755 index 00000000..5da628a1 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter6.ipynb @@ -0,0 +1,1297 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 6: Control Statements<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.4, Page number: 6.3<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "\n",
+ "inputs=[5,25,4,8]\n",
+ "Sum,SumSq,SumSqrt=0,0,0\n",
+ "\n",
+ "for x in inputs:\n",
+ "\n",
+ " Sum+=x\n",
+ " SumSq+=x*x\n",
+ " SumSqrt+=math.sqrt(x)\n",
+ "\n",
+ "\n",
+ "print \" Sum = %d \\n SumSq = %d \\n SumSqrt = %f\" %(Sum,SumSq,SumSqrt)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Sum = 42 \n",
+ " SumSq = 730 \n",
+ " SumSqrt = 12.064495\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.5, Page number: 6.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(first,second):\n",
+ " \n",
+ " if first>second:\n",
+ " print \"first number is bigger\"\n",
+ " if second>first:\n",
+ " print \"second number is bigger\"\n",
+ " if first==second:\n",
+ " print \"both are equal\"\n",
+ " return\n",
+ "\n",
+ "main(5,6)\n",
+ "main(6,5)\n",
+ "main(1,1)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "second number is bigger\n",
+ "first number is bigger\n",
+ "both are equal\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.7, Page naumber: 6.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(time):\n",
+ "\n",
+ " if time>=0. and time<12.:\n",
+ " print \"Good Morning\"\n",
+ " elif time>=12. and time<18.:\n",
+ " print \"Good Afternoon\"\n",
+ " elif time>=18. and time<=24.:\n",
+ " print \"Good Evening\"\n",
+ " else:\n",
+ " print \"time is out of range\"\n",
+ " return\n",
+ "\n",
+ "main(9)\n",
+ "main(15)\n",
+ "main(21)\n",
+ "main(36)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Good Morning\n",
+ "Good Afternoon\n",
+ "Good Evening\n",
+ "time is out of range\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.8, Page number: 6.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "digit=0\n",
+ "while digit<=9:\n",
+ " print digit\n",
+ " digit=digit+1\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.9, Page number: 6.9<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "count=0\n",
+ "tag=len(letter)\n",
+ "\n",
+ "while count<tag:\n",
+ " letter[count]=letter[count].upper()\n",
+ " count=count+1\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.10, Page number: 6.11<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "Sum,count=0,0\n",
+ "\n",
+ "numbers=[1,2,3,4,5,6]\n",
+ "n=6.0\n",
+ "\n",
+ "while count<n:\n",
+ " x=numbers[count]\n",
+ " print \"x = \",x\n",
+ " Sum+=x\n",
+ " count+=1\n",
+ "\n",
+ "average=Sum/n\n",
+ "\n",
+ "print 'Average = ',average\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.11, Page number: 6.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "digit=0\n",
+ "while True:\n",
+ " print digit\n",
+ " digit=digit+1\n",
+ " if digit>9:\n",
+ " break\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.12, Page number: 6.13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "\n",
+ "count=0\n",
+ "tag=len(letter)\n",
+ "\n",
+ "while True:\n",
+ " letter[count]=letter[count].upper()\n",
+ " count=count+1\n",
+ " if count>=tag:\n",
+ " break\n",
+ "\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.13, Page number: 6.14<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "Sum,count=0,0\n",
+ "\n",
+ "numbers=[1,2,3,4,5,6]\n",
+ "n=6.0\n",
+ "\n",
+ "while True:\n",
+ " x=numbers[count]\n",
+ " print \"x = \",x\n",
+ " Sum+=x\n",
+ " count+=1\n",
+ " if count>=n:\n",
+ " break\n",
+ "\n",
+ "average=Sum/n\n",
+ "\n",
+ "print 'Average = ',average\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1\n",
+ "x = 2\n",
+ "x = 3\n",
+ "x = 4\n",
+ "x = 5\n",
+ "x = 6\n",
+ "Average = 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.14, Page number: 6.16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "for digit in range(0,10):\n",
+ " print digit\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.16, Page number: 6.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
+ "letter=list(letter)\n",
+ "n=len(letter)\n",
+ "\n",
+ "for count in range(0,n):\n",
+ " \n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ "letter=''.join(letter)\n",
+ "print letter\n",
+ "\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.17, Page number: 6.17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "n=6\n",
+ "Sum=0.0\n",
+ "\n",
+ "for count in range(1,n+1):\n",
+ " Sum=Sum+count\n",
+ " \n",
+ "average=Sum/n\n",
+ "print \"the average is \",average\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "the average is 3.5\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.18, Page number: 6.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "numbers=[[1.5,2.5,6.2,3.0],[4,-2,7],[5.4,8.0,2.2,1.7,-3.9]]\n",
+ "loopcount=3\n",
+ "loop=0\n",
+ "\n",
+ "while loop<loopcount:\n",
+ "\n",
+ " Sum=0\n",
+ "\n",
+ " for x in numbers[loop]:\n",
+ "\n",
+ " print 'x = ',x\n",
+ " Sum+=x\n",
+ "\n",
+ " print 'The average is ',Sum/len(numbers[loop])\n",
+ " loop+=1\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 1.5\n",
+ "x = 2.5\n",
+ "x = 6.2\n",
+ "x = 3.0\n",
+ "The average is 3.3\n",
+ "x = 4\n",
+ "x = -2\n",
+ "x = 7\n",
+ "The average is 3\n",
+ "x = 5.4\n",
+ "x = 8.0\n",
+ "x = 2.2\n",
+ "x = 1.7\n",
+ "x = -3.9\n",
+ "The average is 2.68\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.19, Page number: 6.20<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth...','*']\n",
+ "\n",
+ "\n",
+ "loop=0\n",
+ "\n",
+ "while True:\n",
+ " letter=text[loop]\n",
+ " if letter=='*':\n",
+ " print \"\\nGood bye\"\n",
+ " break\n",
+ "\n",
+ " print '\\n',letter\n",
+ " letter=list(letter)\n",
+ " n=len(letter)\n",
+ "\n",
+ " for count in range(0,n):\n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ " letter=''.join(letter)\n",
+ " print letter\n",
+ " loop+=1\n",
+ " \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Now is the time for all good men to come to aid..\n",
+ "NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
+ "\n",
+ "Fourscore and seven years ago our fathers brought forth...\n",
+ "FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH...\n",
+ "\n",
+ "Good bye\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.20, Page number: 6.22<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "line=\"The White House. 1600 Pennsylvania Avenue. Washington. DC\"\n",
+ "line=list(line)\n",
+ "line2=[]\n",
+ "for count in line:\n",
+ " if (count>='0' and count<'9') or \\\n",
+ " (count>='a' and count<'z') or \\\n",
+ " (count>='A' and count<'Z'):\n",
+ "\n",
+ " line2.extend(chr(ord(count)+1))\n",
+ "\n",
+ " elif count=='9':\n",
+ " line2.extend('0')\n",
+ " elif count=='z':\n",
+ " line2.extend('a')\n",
+ " elif count=='Z':\n",
+ " line2.extend('A')\n",
+ " else:\n",
+ " line2.extend('.')\n",
+ "\n",
+ "line2=''.join(line2)\n",
+ "print line2\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Uif.Xijuf.Ipvtf..2711.Qfootzmwbojb.Bwfovf..Xbtijohupo..ED\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.21, Page number: 6.23<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def main(p,r,n):\n",
+ " \n",
+ "\n",
+ " if p<0 or r<0 or n<0:\n",
+ " print \"ERROR! Wrong input\"\n",
+ " return\n",
+ " \n",
+ "\n",
+ " i=r/100.0\n",
+ " f=p*((i+1)**n)\n",
+ "\n",
+ " print \"The Final value (F) is %.2f\" %f\n",
+ "\n",
+ " return\n",
+ "main(1000,6,20)\n",
+ "main(5000,-7.5,12)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Final value (F) is 3207.14\n",
+ "ERROR! Wrong input\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.22, Page number: 6.25<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "def main(guess):\n",
+ " count=0\n",
+ " flag=True\n",
+ "\n",
+ " while flag:\n",
+ " count=count+1\n",
+ " if count==50:\n",
+ " flag=False\n",
+ "\n",
+ " test=10.-3.*guess*guess\n",
+ " if test>0:\n",
+ " root=test**.2\n",
+ " print \"Iteration number : %2d x= %7.5f\" %(count,root)\n",
+ " error=math.fabs(root-guess)\n",
+ " if error>0.00001:\n",
+ " guess=root\n",
+ " else:\n",
+ " flag=False\n",
+ " print \"\\n\\n root= %7.5f number of iterations %2d\" %(root,count)\n",
+ "\n",
+ " else:\n",
+ " flag=False\n",
+ " print \"number out of range... try another initial guess\"\n",
+ "\n",
+ "\n",
+ "\n",
+ " if count==50 and error>0.00001:\n",
+ " print \"convergence not obtained after 50 iterations\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "print \"initial guess: 1\"\n",
+ "main(1)\n",
+ "print \"\\ninitial guess: 10\"\n",
+ "main(10)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "initial guess: 1\n",
+ "Iteration number : 1 x= 1.47577\n",
+ "Iteration number : 2 x= 1.28225\n",
+ "Iteration number : 3 x= 1.38344\n",
+ "Iteration number : 4 x= 1.33613\n",
+ "Iteration number : 5 x= 1.35951\n",
+ "Iteration number : 6 x= 1.34826\n",
+ "Iteration number : 7 x= 1.35375\n",
+ "Iteration number : 8 x= 1.35109\n",
+ "Iteration number : 9 x= 1.35238\n",
+ "Iteration number : 10 x= 1.35175\n",
+ "Iteration number : 11 x= 1.35206\n",
+ "Iteration number : 12 x= 1.35191\n",
+ "Iteration number : 13 x= 1.35198\n",
+ "Iteration number : 14 x= 1.35195\n",
+ "Iteration number : 15 x= 1.35196\n",
+ "Iteration number : 16 x= 1.35195\n",
+ "\n",
+ "\n",
+ " root= 1.35195 number of iterations 16\n",
+ "\n",
+ "initial guess: 10\n",
+ "number out of range... try another initial guess\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.23, Page number: 6.30<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main(ch):\n",
+ "\n",
+ " if ch=='r' or ch=='R':\n",
+ " print \"RED\"\n",
+ "\n",
+ " elif ch=='w' or ch=='W':\n",
+ " print \"WHITE\"\n",
+ "\n",
+ " elif ch=='b' or ch=='B':\n",
+ " print \"BLUE\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('r')\n",
+ "main('W')\n",
+ "main('b')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RED\n",
+ "WHITE\n",
+ "BLUE\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.24, Page number: 6.31<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main(ch):\n",
+ " ch=ch.upper()\n",
+ "\n",
+ " if ch=='R':\n",
+ " print \"RED\"\n",
+ "\n",
+ " elif ch=='W':\n",
+ " print \"WHITE\"\n",
+ "\n",
+ " elif ch=='B':\n",
+ " print \"BLUE\"\n",
+ "\n",
+ " else:\n",
+ " print \"ERROR\"\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('r')\n",
+ "main('W')\n",
+ "main('b')\n",
+ "main('y')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "RED\n",
+ "WHITE\n",
+ "BLUE\n",
+ "ERROR\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.25, Page number: 6.31<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import math\n",
+ "\n",
+ "def main(x,flag):\n",
+ "\n",
+ " if flag==-1:\n",
+ " y=math.fabs(x)\n",
+ " elif flag==0:\n",
+ " y=x**0.5\n",
+ " elif flag==1:\n",
+ " y=x\n",
+ " elif flag==2 or flag==3:\n",
+ " y=2*(x-1)\n",
+ " else:\n",
+ " y=0\n",
+ " print \"y= \",y\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main(36,-1)\n",
+ "main(36,0)\n",
+ "main(36,1)\n",
+ "main(36,2)\n",
+ "main(36,3)\n",
+ "main(36,5)\n",
+ "main(0,6)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y= 36.0\n",
+ "y= 6.0\n",
+ "y= 36\n",
+ "y= 70\n",
+ "y= 70\n",
+ "y= 0\n",
+ "y= 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.26, Page number: 6.32<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "choice=0\n",
+ "\n",
+ "def main(choice,val,n):\n",
+ " \n",
+ " if choice==1:\n",
+ "\n",
+ " print \"Straight Line Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " deprec=val/n\n",
+ " for year in range(1,n+1):\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ "\n",
+ " elif choice==2:\n",
+ " print \"Double Declining Balance Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " for year in range(1,n+1):\n",
+ " deprec=2*val/n\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ " elif choice==3:\n",
+ " print \"Sum of the years' -Digit Method\\n\\n\"\n",
+ " val=float(val)\n",
+ " tag=val\n",
+ "\n",
+ " for year in range(1,n+1):\n",
+ " deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
+ " val=val-deprec\n",
+ " print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
+ " %(year,deprec,val)\n",
+ "\n",
+ " else:\n",
+ " print \"incorrect data entry...\"\n",
+ "\n",
+ " return\n",
+ "\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 1 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(1,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 2 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(2,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 3 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(3,8000,10)\n",
+ "print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
+ "print \"choice: 5 \\nval: 8000 \\nNumber of years: 10\"\n",
+ "main(5,8000,10)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 1 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Straight Line Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 800.00 Current value: 7200.00\n",
+ "End of year 2 Depreciation: 800.00 Current value: 6400.00\n",
+ "End of year 3 Depreciation: 800.00 Current value: 5600.00\n",
+ "End of year 4 Depreciation: 800.00 Current value: 4800.00\n",
+ "End of year 5 Depreciation: 800.00 Current value: 4000.00\n",
+ "End of year 6 Depreciation: 800.00 Current value: 3200.00\n",
+ "End of year 7 Depreciation: 800.00 Current value: 2400.00\n",
+ "End of year 8 Depreciation: 800.00 Current value: 1600.00\n",
+ "End of year 9 Depreciation: 800.00 Current value: 800.00\n",
+ "End of year 10 Depreciation: 800.00 Current value: 0.00\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 2 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Double Declining Balance Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 1600.00 Current value: 6400.00\n",
+ "End of year 2 Depreciation: 1280.00 Current value: 5120.00\n",
+ "End of year 3 Depreciation: 1024.00 Current value: 4096.00\n",
+ "End of year 4 Depreciation: 819.20 Current value: 3276.80\n",
+ "End of year 5 Depreciation: 655.36 Current value: 2621.44\n",
+ "End of year 6 Depreciation: 524.29 Current value: 2097.15\n",
+ "End of year 7 Depreciation: 419.43 Current value: 1677.72\n",
+ "End of year 8 Depreciation: 335.54 Current value: 1342.18\n",
+ "End of year 9 Depreciation: 268.44 Current value: 1073.74\n",
+ "End of year 10 Depreciation: 214.75 Current value: 858.99\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 3 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "Sum of the years' -Digit Method\n",
+ "\n",
+ "\n",
+ "End of year 1 Depreciation: 1454.55 Current value: 6545.45\n",
+ "End of year 2 Depreciation: 1309.09 Current value: 5236.36\n",
+ "End of year 3 Depreciation: 1163.64 Current value: 4072.73\n",
+ "End of year 4 Depreciation: 1018.18 Current value: 3054.55\n",
+ "End of year 5 Depreciation: 872.73 Current value: 2181.82\n",
+ "End of year 6 Depreciation: 727.27 Current value: 1454.55\n",
+ "End of year 7 Depreciation: 581.82 Current value: 872.73\n",
+ "End of year 8 Depreciation: 436.36 Current value: 436.36\n",
+ "End of year 9 Depreciation: 290.91 Current value: 145.45\n",
+ "End of year 10 Depreciation: 145.45 Current value: 0.00\n",
+ "\n",
+ " Method: (1-SSL 2-DDB 3-SYD) \n",
+ "choice: 5 \n",
+ "val: 8000 \n",
+ "Number of years: 10\n",
+ "incorrect data entry...\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.31, Page number: 6.41<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "navg=0\n",
+ "Sum=0.0\n",
+ "n=6\n",
+ "\n",
+ "for count in range(-6,n+1):\n",
+ " x=count\n",
+ " if(x<0):\n",
+ " continue\n",
+ " Sum=Sum+x\n",
+ " navg=navg+1\n",
+ "\n",
+ "\n",
+ "average=Sum/navg\n",
+ "print \"The average is \",average\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average is 3.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.32, Page number: 6.42<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def main(letter):\n",
+ " flag=True\n",
+ "\n",
+ " letter=list(letter)\n",
+ "\n",
+ " tag=len(letter)-1\n",
+ " countback=tag\n",
+ "\n",
+ " for count in range(0,(tag/2)+1):\n",
+ " if letter[count]!=letter[countback]:\n",
+ " flag=False\n",
+ " break\n",
+ " countback=countback-1\n",
+ "\n",
+ " letter=''.join(letter)\n",
+ " if flag:\n",
+ " print \"\\n %s IS a palindrome\" %letter\n",
+ " else:\n",
+ " print \"\\n %s is NOT a palindrome\" %letter\n",
+ " \n",
+ " return\n",
+ "\n",
+ "main('TOOT')\n",
+ "main('FALSE')\n",
+ "main('PULLUP')\n",
+ "main('ABLE WAS I ERE I SAW ELBA')\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " TOOT IS a palindrome\n",
+ "\n",
+ " FALSE is NOT a palindrome\n",
+ "\n",
+ " PULLUP IS a palindrome\n",
+ "\n",
+ " ABLE WAS I ERE I SAW ELBA IS a palindrome\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6.34, Page number: 6.47<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth.$$..','*']\n",
+ "\n",
+ "\n",
+ "loop,flag=0,False\n",
+ "\n",
+ "while True:\n",
+ " letter=text[loop]\n",
+ " if letter=='*' or flag:\n",
+ " print \"\\nGood bye\"\n",
+ " break\n",
+ "\n",
+ " print '\\n',letter\n",
+ " letter=list(letter)\n",
+ " n=len(letter)\n",
+ "\n",
+ " for count in range(0,n):\n",
+ " letter[count]=letter[count].upper()\n",
+ "\n",
+ " letter2=''.join(letter)\n",
+ " print letter2\n",
+ "\n",
+ " for count in range(0,n-1):\n",
+ " if letter[count]=='$' and letter[count+1]=='$':\n",
+ " print 'BREAK CONDITION DETECTED - TERMINATE EXECUTION'\n",
+ " flag=True\n",
+ " break\n",
+ " \n",
+ " loop+=1\n",
+ " \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Now is the time for all good men to come to aid..\n",
+ "NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
+ "\n",
+ "Fourscore and seven years ago our fathers brought forth.$$..\n",
+ "FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH.$$..\n",
+ "BREAK CONDITION DETECTED - TERMINATE EXECUTION\n",
+ "\n",
+ "Good bye\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter7.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter7.ipynb new file mode 100755 index 00000000..89247348 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter7.ipynb @@ -0,0 +1,708 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 7: Functions<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.1, Page number: 7.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def lower_to_upper(c1):\n",
+ "\n",
+ " if c1>='a' and c1<='z':\n",
+ " c2=chr(ord('A')+ord(c1)-ord('a'))\n",
+ "\n",
+ " else:\n",
+ " c2=c1\n",
+ "\n",
+ " return c2\n",
+ "\n",
+ "lower='g'\n",
+ "upper=lower_to_upper(lower)\n",
+ "print \"the upper case equivalent is : \",upper\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "the upper case equivalent is : G\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.3, Page number: 7.5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def lower_to_upper(c1):\n",
+ "\n",
+ " if c1>='a' and c1<='z':\n",
+ " return (chr(ord('A')+ord(c1)-ord('a')))\n",
+ " else:\n",
+ " return c1\n",
+ "\n",
+ "lower='g'\n",
+ "upper=lower_to_upper(lower)\n",
+ "print \"The uppercase equivalent is : \",upper\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The uppercase equivalent is : G\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.4, Page number: 7.5<h3> "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def maximum(x,y):\n",
+ " if x>=y:\n",
+ " z=x\n",
+ " else:\n",
+ " z=y\n",
+ "\n",
+ " print \"Maximum value is : \",z\n",
+ "\n",
+ " return\n",
+ "\n",
+ "maximum(5,6)\n",
+ "maximum(6,5)\n",
+ "maximum(5,5)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Maximum value is : 6\n",
+ "Maximum value is : 6\n",
+ "Maximum value is : 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.5, Page number: 7.5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def factorial(n):\n",
+ " prod=1\n",
+ " if n>1:\n",
+ " for i in range(2,n+1):\n",
+ " prod=prod*i\n",
+ "\n",
+ " return prod\n",
+ "\n",
+ "fact=factorial(6)\n",
+ "print \"Its factorial is : \",fact\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Its factorial is : 720\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.7, Page number: 7.7<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def maximum(x,y):\n",
+ "\n",
+ " if x>=y:\n",
+ " z=x\n",
+ " else:\n",
+ " z=y\n",
+ "\n",
+ " print 'Maximum value is = ',z\n",
+ " return\n",
+ "\n",
+ "maximum(5,9)\n",
+ "maximum(8,3)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Maximum value is = 9\n",
+ "Maximum value is = 8\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.9, Page number: 7.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def maximum(x,y):\n",
+ " if x>=y:\n",
+ " z=x\n",
+ " else:\n",
+ " z=y\n",
+ "\n",
+ " return z\n",
+ "\n",
+ "a=5\n",
+ "b=4\n",
+ "c=7\n",
+ "\n",
+ "d=maximum(a,b)\n",
+ "print \"Maximum = \",maximum(c,d)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Maximum = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.10, Page number: 7.10<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def factorial(n):\n",
+ " prod=1\n",
+ " if n>1:\n",
+ " for i in range(2,n+1):\n",
+ " prod=prod*i\n",
+ "\n",
+ " return prod\n",
+ "\n",
+ "n=7\n",
+ "fact=factorial(n)\n",
+ "print \"Its factorial is : \",fact\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Its factorial is : 5040\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.11, Page number: 7.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def play():\n",
+ " print \"Throwing the dice....\"\n",
+ " score1=throw()\n",
+ " print \"%2d\" %(score1)\n",
+ "\n",
+ " if score1==7 or score1==11:\n",
+ " print \"Congratulations!! you WIN on the first throw\"\n",
+ "\n",
+ " elif score1==2 or score1==3 or score1==12:\n",
+ " print \"sorry!! you LOSE on the first throw\"\n",
+ "\n",
+ " else:\n",
+ " while(True):\n",
+ " print \"Throwing the dice again...\"\n",
+ " score2=throw()\n",
+ " print \"%2d\" %(score2)\n",
+ " if score2==score1 or score2==7:\n",
+ " break\n",
+ "\n",
+ " if score2==score1:\n",
+ " print \"You WIN by matching your first score\"\n",
+ " else:\n",
+ " print \"You LOSE by failing to match your first score\"\n",
+ "\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "def throw():\n",
+ "\n",
+ " n1=random.randrange(1,7)\n",
+ " n2=random.randrange(1,7)\n",
+ " return n1+n2\n",
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "\n",
+ "print \"Welcome to the Game of Craps \\n\\n\"\n",
+ "random.seed(2365)\n",
+ "play()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome to the Game of Craps \n",
+ "\n",
+ "\n",
+ "Throwing the dice....\n",
+ " 9\n",
+ "Throwing the dice again...\n",
+ " 4\n",
+ "Throwing the dice again...\n",
+ "11\n",
+ "Throwing the dice again...\n",
+ " 9\n",
+ "You WIN by matching your first score\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.12, Page number: 7.18<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def modify(a):\n",
+ " a=a*3\n",
+ " print \"a= %d (from the function, after being modified)\" %(a)\n",
+ " return\n",
+ "\n",
+ "a=2\n",
+ "print \"a=%d (from main, before calling the function)\" %(a)\n",
+ "modify(a)\n",
+ "print \"a=%d (from main, after calling the function)\" %(a)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a=2 (from main, before calling the function)\n",
+ "a= 6 (from the function, after being modified)\n",
+ "a=2 (from main, after calling the function)\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.13, Page number: 7.19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def sl(val,n):\n",
+ " deprec=val/n\n",
+ " print \"in \",n\n",
+ " \n",
+ " for year in range(1,n+1):\n",
+ " val=val-deprec\n",
+ " writeoutput(year,deprec,val)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "def ddb(val,n):\n",
+ " for year in range(1,n+1):\n",
+ " deprec=2*val/n\n",
+ " val=val-deprec\n",
+ " writeoutput(year,deprec,val)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "def syd(val,n):\n",
+ " tag=val\n",
+ " for year in range(1,n+1):\n",
+ " deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
+ " val=val-deprec\n",
+ " writeoutput(year,deprec,val)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "def writeoutput(year,depreciation,value):\n",
+ " print \"End of the year %2d Depreciation: %7.2f Current Value: %8.2f\" %(year,depreciation,value)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "\n",
+ "def main(choice,val,n):\n",
+ " \n",
+ " print \"Original value : \",val\n",
+ " val=float(val)\n",
+ " print \"Number of years : \",n\n",
+ "\n",
+ " if choice==1:\n",
+ " print \"Straight-Line Method\\n\\n\"\n",
+ " sl(val,n)\n",
+ " elif choice==2:\n",
+ " print \"Double-Declining-Balance Method \\n\\n\"\n",
+ " ddb(val,n)\n",
+ " elif choice==3:\n",
+ " print \"Sum-Of-The-Years'-Digits Method\"\n",
+ " syd(val,n)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
+ "main(1,8000,10)\n",
+ "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
+ "main(2,8000,10)\n",
+ "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
+ "main(3,8000,10)\n",
+ "print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
+ "main(1,5000,4)\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Method: (1-SL 2-DDB 3-SYD)\n",
+ "Original value : 8000\n",
+ "Number of years : 10\n",
+ "Straight-Line Method\n",
+ "\n",
+ "\n",
+ "in 10\n",
+ "End of the year 1 Depreciation: 800.00 Current Value: 7200.00\n",
+ "End of the year 2 Depreciation: 800.00 Current Value: 6400.00\n",
+ "End of the year 3 Depreciation: 800.00 Current Value: 5600.00\n",
+ "End of the year 4 Depreciation: 800.00 Current Value: 4800.00\n",
+ "End of the year 5 Depreciation: 800.00 Current Value: 4000.00\n",
+ "End of the year 6 Depreciation: 800.00 Current Value: 3200.00\n",
+ "End of the year 7 Depreciation: 800.00 Current Value: 2400.00\n",
+ "End of the year 8 Depreciation: 800.00 Current Value: 1600.00\n",
+ "End of the year 9 Depreciation: 800.00 Current Value: 800.00\n",
+ "End of the year 10 Depreciation: 800.00 Current Value: 0.00\n",
+ "\n",
+ "\n",
+ "Method: (1-SL 2-DDB 3-SYD)\n",
+ "Original value : 8000\n",
+ "Number of years : 10\n",
+ "Double-Declining-Balance Method \n",
+ "\n",
+ "\n",
+ "End of the year 1 Depreciation: 1600.00 Current Value: 6400.00\n",
+ "End of the year 2 Depreciation: 1280.00 Current Value: 5120.00\n",
+ "End of the year 3 Depreciation: 1024.00 Current Value: 4096.00\n",
+ "End of the year 4 Depreciation: 819.20 Current Value: 3276.80\n",
+ "End of the year 5 Depreciation: 655.36 Current Value: 2621.44\n",
+ "End of the year 6 Depreciation: 524.29 Current Value: 2097.15\n",
+ "End of the year 7 Depreciation: 419.43 Current Value: 1677.72\n",
+ "End of the year 8 Depreciation: 335.54 Current Value: 1342.18\n",
+ "End of the year 9 Depreciation: 268.44 Current Value: 1073.74\n",
+ "End of the year 10 Depreciation: 214.75 Current Value: 858.99\n",
+ "\n",
+ "\n",
+ "Method: (1-SL 2-DDB 3-SYD)\n",
+ "Original value : 8000\n",
+ "Number of years : 10\n",
+ "Sum-Of-The-Years'-Digits Method\n",
+ "End of the year 1 Depreciation: 1454.55 Current Value: 6545.45\n",
+ "End of the year 2 Depreciation: 1309.09 Current Value: 5236.36\n",
+ "End of the year 3 Depreciation: 1163.64 Current Value: 4072.73\n",
+ "End of the year 4 Depreciation: 1018.18 Current Value: 3054.55\n",
+ "End of the year 5 Depreciation: 872.73 Current Value: 2181.82\n",
+ "End of the year 6 Depreciation: 727.27 Current Value: 1454.55\n",
+ "End of the year 7 Depreciation: 581.82 Current Value: 872.73\n",
+ "End of the year 8 Depreciation: 436.36 Current Value: 436.36\n",
+ "End of the year 9 Depreciation: 290.91 Current Value: 145.45\n",
+ "End of the year 10 Depreciation: 145.45 Current Value: 0.00\n",
+ "\n",
+ "\n",
+ "Method: (1-SL 2-DDB 3-SYD)\n",
+ "Original value : 5000\n",
+ "Number of years : 4\n",
+ "Straight-Line Method\n",
+ "\n",
+ "\n",
+ "in 4\n",
+ "End of the year 1 Depreciation: 1250.00 Current Value: 3750.00\n",
+ "End of the year 2 Depreciation: 1250.00 Current Value: 2500.00\n",
+ "End of the year 3 Depreciation: 1250.00 Current Value: 1250.00\n",
+ "End of the year 4 Depreciation: 1250.00 Current Value: 0.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.14, Page number: 7.24<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def factorial(n):\n",
+ "\n",
+ " if n<=1:\n",
+ " return 1\n",
+ "\n",
+ " else:\n",
+ " return (n*factorial(n-1))\n",
+ "\n",
+ "\n",
+ "n=10\n",
+ "fact=factorial(n)\n",
+ "print \"Its factorial is \",fact\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Its factorial is 3628800\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.15, Page number: 7.26<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def reverse(text,n):\n",
+ "\n",
+ " if n<0:\n",
+ " return\n",
+ " else:\n",
+ " print text[n],\n",
+ " reverse(text,n-1)\n",
+ "\n",
+ "\n",
+ "\n",
+ "text='Now is the time for all good men to come to tje aid of their country!'\n",
+ "n=len(text)\n",
+ "reverse(text,n-1)\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "! y r t n u o c r i e h t f o d i a e j t o t e m o c o t n e m d o o g l l a r o f e m i t e h t s i w o N\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7.16, Page number: 7.16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def transfer(n,From,to,temp):\n",
+ "\n",
+ " if n>0:\n",
+ " transfer(n-1,From,temp,to)\n",
+ " print \"Move disk %d from %c to %c\" %(n,From,to)\n",
+ " transfer(n-1,temp,to,From)\n",
+ "\n",
+ " return\n",
+ "\n",
+ "print \"Welcome to the TOWERS OF HANOI \\n\\n\"\n",
+ "transfer(3,'L','R','C')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome to the TOWERS OF HANOI \n",
+ "\n",
+ "\n",
+ "Move disk 1 from L to R\n",
+ "Move disk 2 from L to C\n",
+ "Move disk 1 from R to C\n",
+ "Move disk 3 from L to R\n",
+ "Move disk 1 from C to L\n",
+ "Move disk 2 from C to R\n",
+ "Move disk 1 from L to R\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter8.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter8.ipynb new file mode 100755 index 00000000..e9a55558 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/chapter8.ipynb @@ -0,0 +1,555 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 8: Program Structure <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.2, Page number: 8.2<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def factorial(n):\n",
+ "\t\n",
+ "\tprod=1\n",
+ "\tif n>1:\n",
+ "\t\t\n",
+ "\t\tfor i in xrange(2,n+1):\n",
+ "\t\t\tprod*=i\n",
+ "\t\t\n",
+ "\t\n",
+ "\treturn prod\n",
+ "\t\n",
+ "n=7\n",
+ "print 'n!=%d' %factorial(n)\n",
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n!=5040\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.3, Page number: 8.3<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def linecount(text):\n",
+ "\t\n",
+ "\treturn len(text)\n",
+ "\n",
+ "Sum=0\n",
+ "lines=['Now is the time for all good men','to come to the aid of their country.']\n",
+ "for i in lines:\n",
+ "\tSum+=linecount(i)\n",
+ "\n",
+ "count=len(lines)\n",
+ "count=float(count)\n",
+ "avg=Sum/count\n",
+ "\n",
+ "print 'Average numbers of characters per line: ',avg\n",
+ "\t\n",
+ "\t"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average numbers of characters per line: 34.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.4, Page number: 8.5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001\n",
+ "\n",
+ "def reduce():\n",
+ "\n",
+ "\tglobal x1,xr,y1,yr,a,b\n",
+ "\t\n",
+ "\tx1 = a+0.5*(b-a-CNST)\n",
+ "\txr = x1 + CNST\n",
+ "\ty1 = curve(x1)\n",
+ "\tyr = curve(xr)\n",
+ "\t\n",
+ "\tif y1 > yr:\n",
+ "\t\tb = xr\n",
+ "\t\n",
+ "\telif y1 < yr:\n",
+ "\t\ta = x1\n",
+ "\t\t\n",
+ "\treturn\n",
+ "\n",
+ "def curve(x):\n",
+ "\t\n",
+ "\treturn x*math.cos(x)\n",
+ "\t\n",
+ "\t\n",
+ "a = 0\n",
+ "b = 3.141593\n",
+ "\n",
+ "while True:\n",
+ "\treduce()\n",
+ "\tif y1 == yr or b-a <= 3*CNST:\n",
+ "\t\tbreak\n",
+ "\t\t\n",
+ "\n",
+ "xmax = 0.5*(x1+xr)\n",
+ "ymax = curve(xmax)\n",
+ "print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "xmax = 0.860394 ymax = 0.561096\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.5, Page number: 8.9<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def linecount(text):\n",
+ "\t\n",
+ "\tglobal Sum\n",
+ "\tSum += len(text)\n",
+ "\n",
+ "Sum=0\n",
+ "lines=['Now is the time for all good men','to come to the aid of their country.']\n",
+ "for i in lines:\n",
+ "\tlinecount(i)\n",
+ "\n",
+ "count=len(lines)\n",
+ "count=float(count)\n",
+ "avg=Sum/count\n",
+ "\n",
+ "print 'Average numbers of characters per line: ',avg\n",
+ "\t\n",
+ "\t\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average numbers of characters per line: 34.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.7, Page number: 8.12<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def fibonacci(count):\n",
+ "\t\n",
+ "\tif count<3:\n",
+ "\t\tf=1\n",
+ "\telse:\n",
+ "\t\tf = fibonacci.f1 + fibonacci.f2\n",
+ "\t\tfibonacci.f2 = fibonacci.f1\n",
+ "\t\tfibonacci.f1 = f\n",
+ "\t\t\n",
+ "\treturn f\n",
+ "\t\t\n",
+ "fibonacci.f1,fibonacci.f2=1,1\n",
+ "n=30\n",
+ "for count in range(1,n+1):\n",
+ "\t\n",
+ "\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i = 1 F = 1\n",
+ "i = 2 F = 1\n",
+ "i = 3 F = 2\n",
+ "i = 4 F = 3\n",
+ "i = 5 F = 5\n",
+ "i = 6 F = 8\n",
+ "i = 7 F = 13\n",
+ "i = 8 F = 21\n",
+ "i = 9 F = 34\n",
+ "i = 10 F = 55\n",
+ "i = 11 F = 89\n",
+ "i = 12 F = 144\n",
+ "i = 13 F = 233\n",
+ "i = 14 F = 377\n",
+ "i = 15 F = 610\n",
+ "i = 16 F = 987\n",
+ "i = 17 F = 1597\n",
+ "i = 18 F = 2584\n",
+ "i = 19 F = 4181\n",
+ "i = 20 F = 6765\n",
+ "i = 21 F = 10946\n",
+ "i = 22 F = 17711\n",
+ "i = 23 F = 28657\n",
+ "i = 24 F = 46368\n",
+ "i = 25 F = 75025\n",
+ "i = 26 F = 121393\n",
+ "i = 27 F = 196418\n",
+ "i = 28 F = 317811\n",
+ "i = 29 F = 514229\n",
+ "i = 30 F = 832040\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3> Example 8.9, Page number: 8.18<h3> "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "def play():\n",
+ " print \"Throwing the dice....\"\n",
+ " score1=throw()\n",
+ " print \"%2d\" %(score1)\n",
+ "\n",
+ " if score1==7 or score1==11:\n",
+ " print \"Congratulations!! you WIN on the first throw\"\n",
+ "\n",
+ " elif score1==2 or score1==3 or score1==12:\n",
+ " print \"sorry!! you LOSE on the first throw\"\n",
+ "\n",
+ " else:\n",
+ " while(True):\n",
+ " print \"Throwing the dice again...\"\n",
+ " score2=throw()\n",
+ " print \"%2d\" %(score2)\n",
+ " if score2==score1 or score2==7:\n",
+ " break\n",
+ "\n",
+ " if score2==score1:\n",
+ " print \"You WIN by matching your first score\"\n",
+ " else:\n",
+ " print \"You LOSE by failing to match your first score\"\n",
+ "\n",
+ "\n",
+ " return\n",
+ "\n",
+ "\n",
+ "def throw():\n",
+ "\n",
+ " n1=random.randrange(1,7)\n",
+ " n2=random.randrange(1,7)\n",
+ " return n1+n2\n",
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "\n",
+ "print \"Welcome to the Game of Craps \\n\\n\"\n",
+ "random.seed(563)\n",
+ "play()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome to the Game of Craps \n",
+ "\n",
+ "\n",
+ "Throwing the dice....\n",
+ "10\n",
+ "Throwing the dice again...\n",
+ " 3\n",
+ "Throwing the dice again...\n",
+ "12\n",
+ "Throwing the dice again...\n",
+ " 9\n",
+ "Throwing the dice again...\n",
+ " 7\n",
+ "You LOSE by failing to match your first score\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.11, Page Number: 8.21<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001\n",
+ "\n",
+ "def reduce():\n",
+ "\n",
+ "\tglobal x1,xr,y1,yr,a,b\n",
+ "\t\n",
+ "\tx1 = a+0.5*(b-a-CNST)\n",
+ "\txr = x1 + CNST\n",
+ "\ty1 = curve(x1)\n",
+ "\tyr = curve(xr)\n",
+ "\t\n",
+ "\tif y1 > yr:\n",
+ "\t\tb = xr\n",
+ "\t\n",
+ "\telif y1 < yr:\n",
+ "\t\ta = x1\n",
+ "\t\t\n",
+ "\treturn\n",
+ "\n",
+ "def curve(x):\n",
+ "\t\n",
+ "\treturn x*math.cos(x)\n",
+ "\t\n",
+ "\t\n",
+ "a = 0\n",
+ "b = 3.141593\n",
+ "\n",
+ "while True:\n",
+ "\treduce()\n",
+ "\tif y1 == yr or b-a <= 3*CNST:\n",
+ "\t\tbreak\n",
+ "\t\t\n",
+ "\n",
+ "xmax = 0.5*(x1+xr)\n",
+ "ymax = curve(xmax)\n",
+ "print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "xmax = 0.860394 ymax = 0.561096\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3> Example 8.12, Page Number: 8.23<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def fibonacci(count):\n",
+ "\t\n",
+ "\tif count<3:\n",
+ "\t\tf=1\n",
+ "\telse:\n",
+ "\t\tf = fibonacci.f1 + fibonacci.f2\n",
+ "\t\tfibonacci.f2 = fibonacci.f1\n",
+ "\t\tfibonacci.f1 = f\n",
+ "\t\t\n",
+ "\treturn f\n",
+ "\t\t\n",
+ "fibonacci.f1,fibonacci.f2=1,1\n",
+ "n=40\n",
+ "for count in range(1,n+1):\n",
+ "\t\n",
+ "\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i = 1 F = 1\n",
+ "i = 2 F = 1\n",
+ "i = 3 F = 2\n",
+ "i = 4 F = 3\n",
+ "i = 5 F = 5\n",
+ "i = 6 F = 8\n",
+ "i = 7 F = 13\n",
+ "i = 8 F = 21\n",
+ "i = 9 F = 34\n",
+ "i = 10 F = 55\n",
+ "i = 11 F = 89\n",
+ "i = 12 F = 144\n",
+ "i = 13 F = 233\n",
+ "i = 14 F = 377\n",
+ "i = 15 F = 610\n",
+ "i = 16 F = 987\n",
+ "i = 17 F = 1597\n",
+ "i = 18 F = 2584\n",
+ "i = 19 F = 4181\n",
+ "i = 20 F = 6765\n",
+ "i = 21 F = 10946\n",
+ "i = 22 F = 17711\n",
+ "i = 23 F = 28657\n",
+ "i = 24 F = 46368\n",
+ "i = 25 F = 75025\n",
+ "i = 26 F = 121393\n",
+ "i = 27 F = 196418\n",
+ "i = 28 F = 317811\n",
+ "i = 29 F = 514229\n",
+ "i = 30 F = 832040\n",
+ "i = 31 F = 1346269\n",
+ "i = 32 F = 2178309\n",
+ "i = 33 F = 3524578\n",
+ "i = 34 F = 5702887\n",
+ "i = 35 F = 9227465\n",
+ "i = 36 F = 14930352\n",
+ "i = 37 F = 24157817\n",
+ "i = 38 F = 39088169\n",
+ "i = 39 F = 63245986\n",
+ "i = 40 F = 102334155\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8.13, Page Number: 8.25<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "\n",
+ "p,r,n=10000,10,3\n",
+ "i=r/100.0\n",
+ "\n",
+ "f=p*math.pow(1+i,n)\n",
+ "print \"The final value (F) is: %.2f\" %f\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The final value (F) is: 13310.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/Programming_With_C_by_Byron_S._Gottfried/chapter9.ipynb b/Programming_With_C_by_Byron_S._Gottfried/chapter9.ipynb new file mode 100755 index 00000000..00db2e74 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/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 diff --git a/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled1.png b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled1.png Binary files differnew file mode 100755 index 00000000..99318d7d --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled1.png diff --git a/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled2.png b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled2.png Binary files differnew file mode 100755 index 00000000..f39082e3 --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled2.png diff --git a/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled3.png b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled3.png Binary files differnew file mode 100755 index 00000000..1b33e38c --- /dev/null +++ b/Programming_With_C_by_Byron_S._Gottfried/screenshots/Untitled3.png |