diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /Programming_in_C | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'Programming_in_C')
-rw-r--r-- | Programming_in_C/Chapter_03.ipynb | 146 | ||||
-rw-r--r-- | Programming_in_C/Chapter_04.ipynb | 262 | ||||
-rw-r--r-- | Programming_in_C/Chapter_05.ipynb | 421 | ||||
-rw-r--r-- | Programming_in_C/Chapter_06.ipynb | 496 | ||||
-rw-r--r-- | Programming_in_C/Chapter_07.ipynb | 489 | ||||
-rw-r--r-- | Programming_in_C/Chapter_08.ipynb | 1061 | ||||
-rw-r--r-- | Programming_in_C/Chapter_09.ipynb | 594 | ||||
-rw-r--r-- | Programming_in_C/Chapter_10.ipynb | 768 | ||||
-rw-r--r-- | Programming_in_C/Chapter_11.ipynb | 842 | ||||
-rw-r--r-- | Programming_in_C/Chapter_12.ipynb | 258 | ||||
-rw-r--r-- | Programming_in_C/Chapter_13.ipynb | 201 | ||||
-rw-r--r-- | Programming_in_C/Chapter_14.ipynb | 92 | ||||
-rw-r--r-- | Programming_in_C/Chapter_16.ipynb | 304 | ||||
-rw-r--r-- | Programming_in_C/Chapter_17.ipynb | 110 | ||||
-rw-r--r-- | Programming_in_C/Chapter_18.ipynb | 472 | ||||
-rw-r--r-- | Programming_in_C/Chapter_19.ipynb | 70 | ||||
-rw-r--r-- | Programming_in_C/README.txt | 10 | ||||
-rw-r--r-- | Programming_in_C/screenshots/nalin-1.png | bin | 0 -> 37400 bytes | |||
-rw-r--r-- | Programming_in_C/screenshots/nalin-2.png | bin | 0 -> 40842 bytes | |||
-rw-r--r-- | Programming_in_C/screenshots/nalin-3.png | bin | 0 -> 52280 bytes |
20 files changed, 6596 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_03.ipynb b/Programming_in_C/Chapter_03.ipynb new file mode 100644 index 00000000..0f1f4347 --- /dev/null +++ b/Programming_in_C/Chapter_03.ipynb @@ -0,0 +1,146 @@ +{ + "metadata": { + "name": "Chapter III" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Chapter 3: Compiling and running your first program" + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.1, Page number: 11" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.1.py\n#first Python Program\n\n#Print Statement\nprint(\"Programming is Fun.\")", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Programming is Fun.\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.2, Page number: 14" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.2.py\n#Writing Your First C Program(Version2)\n\n#Print statement\nprint(\"Programming is Fun.\")\nprint(\"And Programming in Python is even more fun\")", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Programming is Fun.\nAnd Programming in Python is even more fun\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.3, Page number: 14" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.3.py\n#Displaying multiple Lines of output\n\n#Print statements using escape sequence\nprint(\"Testing...\\n..1\\n...2\\n....3\\n\")", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Testing...\n..1\n...2\n....3\n\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.4, Page number: 15" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.4.py\n#Displaying Variables\n \nsum=50+25\nprint(\"The sum of 50 & 25 is: {0} \".format(sum))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of 50 & 25 is: 75 \n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.5, Page number: 16" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.5.py\n#Displayig multiple values\n\n#Variable declaration\nvalue1=50\nvalue2=25\n\n#Calculation\nsum=value1+value2\n\n#Result\nprint(\"The sum of {0} & {1} is: {2}\".format(value1,value2,sum))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of 50 & 25 is: 75\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "Program 3.6, Page number: 17" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#3.6.py\n#Using Comment in a program\n\n#'#(Hash/Pound)' is used to display a comment in python\n#This is a comment\n\n#Variable declarations\nvalue1=50\nvalue2=25\n\n#Calculation\nsum=value1+value2\n\n#Result\nprint(\"The sum of {0} & {1} is {2}\".format(value1,value2,sum))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of 50 & 25 is 75\n" + } + ], + "prompt_number": 6 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_04.ipynb b/Programming_in_C/Chapter_04.ipynb new file mode 100644 index 00000000..f7f7405b --- /dev/null +++ b/Programming_in_C/Chapter_04.ipynb @@ -0,0 +1,262 @@ +{ + "metadata": { + "name": "Chapter IV" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Variables, data types, and arithmetic expressions" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.1, Page number: 26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.1.py\n", + "#Using the Basic Data Types\n", + "\n", + "#Variable Declarations\n", + "integerVar=100\n", + "floatingVar=331.79\n", + "doubleVar=8.44e+11\n", + "charVar='w'\n", + "boolVar=bool(0)\n", + "\n", + "#Result\n", + "print(\"integerVar={0}\".format(integerVar))\n", + "print(\"floatingVar={0}\".format(floatingVar))\n", + "print(\"doubleVar={0}\".format(doubleVar))\n", + "print(\"charVar={0}\".format(charVar))\n", + "print(\"boolVar={0}\".format(boolVar))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "integerVar=100\n", + "floatingVar=331.79\n", + "doubleVar=8.44e+11\n", + "charVar=w\n", + "boolVar=False\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.2, Page number: 30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.2.py\n", + "#Using the Arithmetic Operators\n", + "\n", + "#Variable declarations\n", + "a=100\n", + "b=2\n", + "c=25\n", + "d=4\n", + "\n", + "#Calculations/Results\n", + "result=a-b #subtraction\n", + "print(\"a-b={0}\".format(result)) #result\n", + "\n", + "result=b*c #multiplication\n", + "print(\"b*c={0}\".format(result)) #result\n", + "\n", + "result=a/c #division\n", + "print(\"a/c={0}\".format(result)) #result\n", + "\n", + "result=a+b*c #precedence\n", + "print(\"a+b*c={0}\".format(result)) #result \n", + "\n", + "print(\"a*b+c*d={0}\".format(a*b+c*d)) #direct calculation" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a-b=98\n", + "b*c=50\n", + "a/c=4\n", + "a+b*c=150\n", + "a*b+c*d=300\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.3, Page number: 33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.3.py\n", + "#More Examples with Arithmetic Operations\n", + "\n", + "#Variable Declarations\n", + "a=25\n", + "b=2\n", + "c=25.0\n", + "d=2.0\n", + "\n", + "#Calculations/Result\n", + "print(\"6 + a / 5 * b = {0}\".format(6+a/5*b))\n", + "print(\"a / b * b = {0}\".format(a/b*b))\n", + "print(\"c / d * d = {0}\".format(c/d*d))\n", + "print(\"-a={0}\".format(-a))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "6 + a / 5 * b = 16\n", + "a / b * b = 24\n", + "c / d * d = 25.0\n", + "-a=-25\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.4, Page number: 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.4.py\n", + "#Illustrating the Modulus Operator\n", + "\n", + "#Variable Declarations\n", + "a=25\n", + "b=5\n", + "c=10\n", + "d=7\n", + "\n", + "#Calculations/Result\n", + "print(\"a % b = {0}\".format(a%b))\n", + "print(\"a % c = {0}\".format(a%c))\n", + "print(\"a % d = {0}\".format(a%d))\n", + "print(\"a / d * d + a % d = {0}\".format(a/d*d+a%d))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a % b = 0\n", + "a % c = 5\n", + "a % d = 4\n", + "a / d * d + a % d = 25\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.5, Page number: 36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.5.py\n", + "#Converting Between Integers and Floats\n", + "\n", + "#Variable Declarations\n", + "f1=123.125\n", + "f2=1.0\n", + "i1=1\n", + "i2=-150\n", + "c='a'\n", + "\n", + "\n", + "i1=f1 #floating to integer conversion\n", + "print(\"{0} assigned to an int produces {1:.0f}\".format(f1,i1))\n", + "\n", + "f1=i2 #integer to floating conversion\n", + "print(\"{0} assigned to a float produces {1}\".format(i2,f1))\n", + "\n", + "f1=i2/100 #integer divided by integer\n", + "print(\"{0} divided by 100 produces {1}\".format(i2,f1))\n", + "\n", + "f2=i2/100.0 #integer divided by a float\n", + "print(\"{0} divided by 100.0 produces {1}\".format(i2,f2))\n", + "\n", + "f2=float(i2/100) #type cast operator\n", + "print(\"{0} divided by 100 produces {1}\".format(i2,f2))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "123.125 assigned to an int produces 123\n", + "-150 assigned to a float produces -150\n", + "-150 divided by 100 produces -2\n", + "-150 divided by 100.0 produces -1.5\n", + "-150 divided by 100 produces -2.0\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_05.ipynb b/Programming_in_C/Chapter_05.ipynb new file mode 100644 index 00000000..90be6c84 --- /dev/null +++ b/Programming_in_C/Chapter_05.ipynb @@ -0,0 +1,421 @@ +{ + "metadata": { + "name": "Chapter V" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 5: Program looping" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.1, Page number: 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.1.py\n", + "#Calculating the Eighth Triangular Number\n", + "\n", + "#Variable Declaration\n", + "triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;\n", + "\n", + "#Result\n", + "print(\"The eighth triangular number is {0}\".format(triangularNumber))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The eighth triangular number is 36\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.2, Page number: 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.2.py\n", + "#Calculating the 200th Triangular Number\n", + "\n", + "#Variable Declaration\n", + "triangularNumber=0\n", + "\n", + "#Calculation\n", + "for i in range (201):\n", + " triangularNumber=triangularNumber+i \n", + "\n", + "#Result\n", + "print(\"The 200th triangular number is {0}\".format(triangularNumber))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The 200th triangular number is 20100\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.3, Page number: 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.3.py\n", + "#Generating a Table of Triangular Numbers\n", + "\n", + "print(\"TABLE OF TRIANGULAR NUMBERS\\n\\n\")\n", + "print(\" n Sum from 1 to n\\n\")\n", + "print(\"--- -----------------\\n\")\n", + "\n", + "#Variable Declarations\n", + "triangularNumber=0\n", + "\n", + "#Calculation/Result\n", + "for i in range (1,11):\n", + " triangularNumber=triangularNumber+i\n", + " print(\" {0} {1}\\n\".format(i,triangularNumber))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "TABLE OF TRIANGULAR NUMBERS\n", + "\n", + "\n", + " n Sum from 1 to n\n", + "\n", + "--- -----------------\n", + "\n", + " 1 1\n", + "\n", + " 2 3\n", + "\n", + " 3 6\n", + "\n", + " 4 10\n", + "\n", + " 5 15\n", + "\n", + " 6 21\n", + "\n", + " 7 28\n", + "\n", + " 8 36\n", + "\n", + " 9 45\n", + "\n", + " 10 55\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.4, Page number: 51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.4.py\n", + "#Asking the User for Input\n", + "\n", + "#Variable Declaration/User Input\n", + "triangularNumber=0\n", + "number=10 #number=input(\"What triangular number do you want?\")\n", + "\n", + "#Calculations\n", + "for n in range (1,(number+1)):\n", + " triangularNumber+=n\n", + "\n", + "#Result\n", + "print(\"triangular number {0} is {1}\".format(number,triangularNumber))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "triangular number 10 is 55\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.5, Page number: 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.5.py\n", + "#Using Nested for Loops\n", + "\n", + "#Calculations\n", + "for counter in range(1,6): #Outer Loop\n", + " number=12 #input(\"what triangular number do you want? \")\n", + "\n", + " triangularNumber=0 #Variable Decaration\n", + "\n", + " for n in range (1,(number+1)): #Inner Loop\n", + " triangularNumber+=n\n", + " \n", + " #Result\n", + " print(\"Triangular Number {0} is {1}\".format(number,triangularNumber))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Triangular Number 12 is 78\n", + "Triangular Number 12 is 78\n", + "Triangular Number 12 is 78\n", + "Triangular Number 12 is 78\n", + "Triangular Number 12 is 78\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.6, Page number: 56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.6.py\n", + "#Introducing the while Statement\n", + "\n", + "#Variable Declaration\n", + "count=1\n", + "\n", + "#Calculation/Iteration `\n", + "while (count<=5):\n", + " print(\"{0}\\n\".format(count)) #Result\n", + " count+=1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.7, Page number: 58" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.7.py\n", + "#Finding the Greatest Common Divisor\n", + "\n", + "print(\"Please type in two nonnegative integers.\")\n", + "#Variable Declaration/User Input\n", + "u=8 #u=input()\n", + "v=14 #v=input()\n", + "\n", + "#Calculation\n", + "while(v!=0):\n", + " temp=u%v\n", + " u=v\n", + " v=temp\n", + "\n", + "#Result\n", + "print(\"Their greatest common divisor is {0}\\n\".format(u))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please type in two nonnegative integers.\n", + "Their greatest common divisor is 2\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.8, Page number: 60" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.8.py\n", + "#Reversing the Digits of a Number\n", + "\n", + "#Import Library\n", + "import sys\n", + "\n", + "#Variable declaration/User Input\n", + "number=113 #number=input(\"Enter your number.\\n\")\n", + "\n", + "#Calculation/Result\n", + "while(number!=0):\n", + " right_digit=number%10\n", + " sys.stdout.write(\"{0}\".format(right_digit))\n", + " number=number/10\n", + "\n", + "print(\"\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "311\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 5.9, Page number: 61" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#5.9.py\n", + "#Implementing a Revised Program to Reverse the Digits of a Number\n", + "#(do-while equivalent code)\n", + "\n", + "#Import Library\n", + "import sys\n", + "\n", + "#Variable Declaration/User Input\n", + "number=428 #number=input(\"Enter your number:\\n\")\n", + "\n", + "#Calculation/Result\n", + "while True: #Enter loop instantly for the first time\n", + " right_digit=number%10\n", + " sys.stdout.write(\"{0}\".format(right_digit))\n", + " number=number/10\n", + " if(number == 0): #Check for exit condition\n", + " break\n", + "\n", + "print(\"\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "824\n", + "\n" + ] + } + ], + "prompt_number": 14 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_06.ipynb b/Programming_in_C/Chapter_06.ipynb new file mode 100644 index 00000000..a612ce70 --- /dev/null +++ b/Programming_in_C/Chapter_06.ipynb @@ -0,0 +1,496 @@ +{ + "metadata": { + "name": "Chapter VI" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Making decisions" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.1, Page number: 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.1.py\n", + "#Calculating the Absolute Value of an Integer\n", + "\n", + "#Variable Declaration/User input\n", + "number=12 #number=int(raw_input(\"Type in your number: \"))\n", + "\n", + "#Calculation\n", + "try:\n", + " if( number < 0 ):\n", + " number=-number #change sign,if number is negative\n", + "except: \n", + " print \"not a number\" #Invalid input/value error\n", + " \n", + "#Result\n", + "print (\"The absolute Value is {0}\".format(number))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The absolute Value is 12\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.2, Page number: 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.2.py\n", + "#Calculating average & counting the number\n", + "#of failures from a set of grades\n", + "\n", + "#Variable Declarations\n", + "gradeTotal=0\n", + "failureCount=0\n", + "i=0\n", + "\n", + "#User Input\n", + "numberOfGrades=5 #numberOfGrades=int(raw_input(\"How many grades will you be entering? \"))\n", + "grade=[72,83,91,89,95]\n", + "\n", + "#Calculation\n", + "while(i<numberOfGrades):\n", + " gradeTotal=gradeTotal+grade[i]\n", + " if(grade<65):\n", + " failureCount=failureCount+1;\n", + " i=i+1\n", + "\n", + "average=float(gradeTotal/numberOfGrades)\n", + " \n", + "#Result\n", + "print(\"Grade average= {0:.2f}\".format(average))\n", + "print(\"Number of failures= {0}\".format(failureCount))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Grade average= 86.00\n", + "Number of failures= 0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.3, Page number: 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.3.py\n", + "#Determining if a Number Is Even or Odd\n", + "\n", + "#Variable declaration/User input\n", + "number_to_test=30 #number_to_test=int(raw_input(\"Enter your number to be tested: \"))\n", + "\n", + "#Calculation\n", + "remainder=number_to_test%2\n", + "\n", + "#Result\n", + "if(remainder==0):\n", + " print(\"The number is even\")\n", + "if(remainder!=0):\n", + " print(\"The number is odd\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The number is even\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.4, Page number: 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.4.py\n", + "#Program to determine if a number is even or odd (Ver. 2)\n", + "\n", + "#Variable declaration/User input\n", + "number_to_test=45 #number_to_test=int(raw_input(\"Enter your Number to be tested: \"))\n", + "\n", + "#Calculation\n", + "remainder=number_to_test%2\n", + "\n", + "#Result\n", + "if(remainder==0):\n", + " print(\"The number is even.\\n\")\n", + "else: #using 'else' instead of second 'if'\n", + " print(\"The number is odd.\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The number is odd.\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.5, Page number: 73" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.5.py\n", + "#Determining if a Year Is a Leap Year\n", + "\n", + "#Variable declaration/User input\n", + "year=1900 #year=int(raw_input(\"Enter the year to be tested: \"))\n", + "\n", + "#Calculations\n", + "rem_4=year%4\n", + "rem_100=year%100\n", + "rem_400=year%400\n", + "\n", + "#Result\n", + "if((rem_4==0 and rem_100!=0)or rem_400==0):\n", + " print(\"It's a leap year.\")\n", + "else:\n", + " print(\"Nope, it's not a leap year.\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nope, it's not a leap year.\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.6, Page number: 77" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.6.py\n", + "#Implementing the Sign Function\n", + "\n", + "#Variable declaration/User input\n", + "number=450 #number=int(raw_input(\"Please type in a number: \"))\n", + "\n", + "#Calculations\n", + "try:\n", + " if(number<0): #Negative Number\n", + " sign=-1\n", + " elif(number==0): #No sign\n", + " sign=0\n", + " else: #Positive Number\n", + " sign=1\n", + "except: #Value error\n", + " print(\"invalid input\")\n", + "\n", + "#Result\n", + "print(\"Sign= {0}\".format(sign))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sign= 1\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.7, Page number: 78" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.7.py\n", + "#Implementing the Sign Function\n", + "\n", + "#variable declaration/User input\n", + "ch='z' #ch=raw_input(\"Enter a single character: \")\n", + "\n", + "#Calculation/Result\n", + "if((ch>='a' and ch <='z') or (ch>='A' and ch<='Z')):\n", + " print(\"It's an alphabetic character.\")\n", + "elif(ch>='0'and ch<='9'):\n", + " print(\"It's a digit.\")\n", + "else:\n", + " print(\"It's a special character\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "It's an alphabetic character.\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.8, Page number: 80" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.8.py\n", + "#evaluate simple expressions of the form\n", + "#<number operator number>\n", + "\n", + "\n", + "try:\n", + "#Variable declaration/User input\n", + " print(\"Type in your expression(with spaces inbetween): \")\n", + " value1, operator, value2=\"5 + 2\".split()\n", + " #value1, operator, value2=raw_input().split()\n", + "\n", + "except:\n", + " print(\"err.. follow the syntax <value operator value>\") \n", + " print(\"with spaces inbetween\\n\")\n", + "\n", + "#Parsing\n", + "value1,value2=[float(value1),float(value2)]\n", + "\n", + "#Calculation/Result\n", + "if(operator=='+'):\n", + " print(\"answer: {0:.2f}\".format(value1+value2))\n", + "elif(operator=='-'):\n", + " print(\"answer: {0:.2f}\".format(value1-value2))\n", + "elif(operator=='*'):\n", + " print(\"answer: {0:.2f}\".format(value1*value2))\n", + "else:\n", + " print(\"answer: {0:.2f}\".format(value1/value2))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type in your expression(with spaces inbetween): \n", + "answer: 7.00\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.9, Page number: 85" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.9.py\n", + "#evaluate simple expressions of the form\n", + "#<number operator number> (Version 2)\n", + "\n", + "\n", + "try:\n", + "#Variable declaration/User input\n", + " print(\"Type in your expression(with spaces inbetween): \")\n", + " value1, operator, value2=\"3 * 5\".split()\n", + " #value1, operator, value2=raw_input().split()\n", + " \n", + " \n", + "except:\n", + " print(\"err.. follow the syntax <value operator value>\") \n", + " print(\"with spaces inbetween\\n\")\n", + "\n", + "#parsing\n", + "value1,value2=[float(value1), float(value2)]\n", + "\n", + "#Calculation/Result\n", + "if(operator=='+'):\n", + " print(\"Answer= {0:.2f}\".format(value1+value2))\n", + "elif(operator=='-'):\n", + " print(\"Answer= {0:.2f}\".format(value1-value2))\n", + "elif(operator=='*'):\n", + " print(\"Answer= {0:.2f}\".format(value1*value2))\n", + "elif(operator=='/'):\n", + " if(value2==0):\n", + " print(\"Whoops! divide by 0 issue\")\n", + " else:\n", + " print(\"Answer= {0:.2f}\".format(value1/value2))\n", + "else:\n", + " print(\"err.. Invalid operator\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type in your expression(with spaces inbetween): \n", + "Answer= 15.00\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 6.10, Page number: 87" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#6.10.py\n", + "#Generating a Table of Prime Numbers\n", + "\n", + "#Variable declarations\n", + "p=2\n", + "\n", + "#Calculations\n", + "while(p<=50): #Outer loop\n", + " isPrime=1 #Variable declaration\n", + " d=2 \n", + " while(d<p): #Inner loop\n", + " if(p%d==0):\n", + " isPrime=0\n", + " d=d+1 #End of inner loop\n", + " \n", + " if( isPrime!=0):\n", + "#Print Result\n", + " print \" \",p \n", + " p=p+1 #End of outer loop" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 2\n", + " 3\n", + " 5\n", + " 7\n", + " 11\n", + " 13\n", + " 17\n", + " 19\n", + " 23\n", + " 29\n", + " 31\n", + " 37\n", + " 41\n", + " 43\n", + " 47\n" + ] + } + ], + "prompt_number": 14 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_07.ipynb b/Programming_in_C/Chapter_07.ipynb new file mode 100644 index 00000000..de714e04 --- /dev/null +++ b/Programming_in_C/Chapter_07.ipynb @@ -0,0 +1,489 @@ +{ + "metadata": { + "name": "Chapter VII" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7: Working with arrays" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.1, Page number: 99" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.1.py\n", + "#Working with an Array(or list in python)\n", + "\n", + "#Variable/List declaration\n", + "values=[0]*10\n", + "\n", + "#Initialisation/Calculation\n", + "values[0]=197\n", + "values[2]=-100\n", + "values[5]=350\n", + "values[3]=values[0]+values[5]\n", + "values[9]=values[5]/10\n", + "values[2]-=1\n", + "i=0\n", + "\n", + "#Iteration/Result\n", + "for index in values:\n", + " print(\"values[{0}] = {1}\".format(i,index))\n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "values[0] = 197\n", + "values[1] = 0\n", + "values[2] = -101\n", + "values[3] = 547\n", + "values[4] = 0\n", + "values[5] = 350\n", + "values[6] = 0\n", + "values[7] = 0\n", + "values[8] = 0\n", + "values[9] = 35\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.2, Page number: 101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.2.py\n", + "#Demonstrating an Array of Counters\n", + "\n", + "#variable/List Declaration\n", + "ratingCounters=[0]*11\n", + "\n", + "#Loop-1\n", + "for i in range(1,11):\n", + " ratingCounters[i]=0\n", + "\n", + "print(\"Enter your Responses:\")\n", + "\n", + "#Loop-2\n", + "#Clculation\n", + "for i in range (1,21):\n", + " response=5 #score=5 for all iterations\n", + " #response=input()\n", + "\n", + " if(response<1 or response>10):\n", + " print(\"Bad response: {0}\".format(response))\n", + " else:\n", + " ratingCounters[response]+=1\n", + "\n", + "#Result\n", + "print(\"\\n\\n Rating | Number of Responses\\n\")\n", + "print(\" ----- -------------------\")\n", + "\n", + "#Loop-3\n", + "for i in range(1,11):\n", + " print(\" {0} {1}\".format(i,ratingCounters[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your Responses:\n", + "\n", + "\n", + " Rating | Number of Responses\n", + "\n", + " ----- -------------------\n", + " 1 0\n", + " 2 0\n", + " 3 0\n", + " 4 0\n", + " 5 20\n", + " 6 0\n", + " 7 0\n", + " 8 0\n", + " 9 0\n", + " 10 0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.3, Page number: 103" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.3.py\n", + "#Generating Fibonacci Numbers\n", + "\n", + "#Variable/List Declaration\n", + "Fibonacci=[0]*15\n", + "\n", + "Fibonacci[0]=0\n", + "Fibonacci[1]=1\n", + "\n", + "#Loop-1\n", + "for i in range(2,15):\n", + " Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] #Calculation\n", + "\n", + "#Loop-2\n", + "for i in range(15):\n", + " print(\"{0}\".format(Fibonacci[i])) #Print Result" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n", + "1\n", + "1\n", + "2\n", + "3\n", + "5\n", + "8\n", + "13\n", + "21\n", + "34\n", + "55\n", + "89\n", + "144\n", + "233\n", + "377\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.4, Page number: 105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.4.py\n", + "#Revising the Program to Generate Prime Numbers,Version 2\n", + "\n", + "#Variable Declaration\n", + "primeIndex=2\n", + "primes=[0]*50\n", + "\n", + "primes[0]=2\n", + "primes[1]=3\n", + "p=5\n", + "\n", + "#Calculations\n", + "while(p<=50): #Outer Loop-1\n", + " isPrime=True\n", + " \n", + " i=1\n", + " while(isPrime and p/primes[i]>=primes[i]): #Inner Loop\n", + " if(p%primes[i]==0):\n", + " isPrime=False\n", + " i+=1\n", + " \n", + "\n", + " if(isPrime==True):\n", + " primes[primeIndex]=p\n", + " primeIndex+=1\n", + " p=p+2\n", + "\n", + "#Results\n", + "for i in range (primeIndex): #Outer Loop-2\n", + " print(\"{0}\".format(primes[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n", + "3\n", + "5\n", + "7\n", + "11\n", + "13\n", + "17\n", + "19\n", + "23\n", + "29\n", + "31\n", + "37\n", + "41\n", + "43\n", + "47\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.5, Page number: 107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.5.py\n", + "#Initializing Arrays\n", + "\n", + "#Variable/List Declarations\n", + "array_values=[0]*10\n", + "array_values[0:4]=[0,1,4,9,16]\n", + "\n", + "#Calculations\n", + "for i in range(5,10):\n", + " array_values[i]=i*i\n", + "\n", + "#Results\n", + "for i in range(10):\n", + " print(\"array values[{0}] = {1}\".format(i,array_values[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "array values[0] = 0\n", + "array values[1] = 1\n", + "array values[2] = 4\n", + "array values[3] = 9\n", + "array values[4] = 16\n", + "array values[5] = 25\n", + "array values[6] = 36\n", + "array values[7] = 49\n", + "array values[8] = 64\n", + "array values[9] = 81\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.6, Page number: 108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.6.py\n", + "#Introducing Character Arrays\n", + "\n", + "#Import Library\n", + "import sys #For sys.stdout.write()\n", + "\n", + "#Variable/List Declaration\n", + "word=['H','e','l','l','o','!']\n", + "\n", + "#Iterator/Result\n", + "for i in word:\n", + " sys.stdout.write(\"{0} \".format(i))\n", + "print(\"\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "H e l l o ! \n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.7, Page number: 110" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.7.py\n", + "#Converting a Positive Integer to Another Base\n", + "\n", + "#import Library\n", + "import sys #for sys.stdout.write()\n", + "\n", + "#Variable Declarations\n", + "baseDigits=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']\n", + "convertedNumber=[0]*10\n", + "index=0\n", + "\n", + "numberToConvert=234 #numberToConvert=input(\"Number to be converted?: \")\n", + "base=2 #base=input(\"Base?: \")\n", + "\n", + "#Calculations\n", + "while True:\n", + " convertedNumber[index]=numberToConvert%base\n", + " index+=1\n", + " numberToConvert=numberToConvert/base\n", + " if(numberToConvert==0):\n", + " break\n", + "\n", + "#Result\n", + "print(\"Converted Number= \")\n", + "index-=1\n", + "while(index>=0):\n", + " nextDigit=convertedNumber[index]\n", + " sys.stdout.write(\"{0} \".format(baseDigits[nextDigit]))\n", + " index-=1\n", + "\n", + "print(\"\\n\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Converted Number= \n", + "1 1 1 0 1 0 1 0 \n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 7.8, Page number: 116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#7.8.py\n", + "#Generating Fibonacci Numbers Using Variable-Length Arrays\n", + "\n", + "#import Libraries\n", + "import sys #For sys.exit() \n", + " \n", + "#User Input \n", + "numFibs=18 #numFibs=input(\"How many Fibonacci numbers do you want (between 1 and 75)? \")\n", + "\n", + "if(numFibs<1 or numFibs>75):\n", + " print(\"bad number,sorry!\")\n", + " sys.exit()\n", + "\n", + "#Variable Declarations \n", + "Fibonacci=[0]*numFibs\n", + "Fibonacci[0]=0\n", + "Fibonacci[1]=1\n", + "\n", + "#Calculation\n", + "for i in range(2,numFibs):\n", + " Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] \n", + " \n", + "#Result\n", + "for i in range(numFibs):\n", + " print(\"{0}\".format(Fibonacci[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n", + "1\n", + "1\n", + "2\n", + "3\n", + "5\n", + "8\n", + "13\n", + "21\n", + "34\n", + "55\n", + "89\n", + "144\n", + "233\n", + "377\n", + "610\n", + "987\n", + "1597\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_08.ipynb b/Programming_in_C/Chapter_08.ipynb new file mode 100644 index 00000000..a4113d97 --- /dev/null +++ b/Programming_in_C/Chapter_08.ipynb @@ -0,0 +1,1061 @@ +{ + "metadata": { + "name": "Chapter VIII" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Chapter 8: Working with functions" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.1, Page number: 120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.1.py\n", + "#Writing a Function in python\n", + "\n", + "\n", + "def printMessage(): #Function to Print Message\n", + " print(\"Programming is Fun.\\n\") #Print Statement\n", + "\n", + "\n", + "def main(): #Main() function\n", + " printMessage()\n", + "\n", + "\n", + "if __name__ =='__main__': #Setting Top-level conditional script\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Programming is Fun.\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.2, Page number: 121" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.2.py\n", + "#Calling Functions\n", + "\n", + "\n", + "def printMessage(): #Function to Print Message\n", + " print(\"Programming is Fun.\\n\") #Print Statement\n", + "\n", + "\n", + "def main(): #Main() function\n", + " printMessage() #First Function call\n", + " printMessage() #Second Function Call\n", + "\n", + "if __name__ =='__main__': #Setting Top-level conditional script\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Programming is Fun.\n", + "\n", + "Programming is Fun.\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.3, Page number: 122" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.3.py\n", + "#More on Calling Functions\n", + "\n", + "\n", + "def printMessage(): #Function to Print Message\n", + " print(\"Programming is Fun.\\n\") #Print Statement\n", + "\n", + "\n", + "def main(): \n", + " for i in range (1,6): #Main() function\n", + " printMessage()\n", + "\n", + "\n", + "if __name__ =='__main__': #Setting Top-level script environment\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Programming is Fun.\n", + "\n", + "Programming is Fun.\n", + "\n", + "Programming is Fun.\n", + "\n", + "Programming is Fun.\n", + "\n", + "Programming is Fun.\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.4, Page number: 123" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.4.py\n", + "#Calculating the nth Triangular Number\n", + "\n", + "\n", + "def calculateTriangularNumber(n): #Function to calculate triangular number\n", + " triangularNumber=0 #Variable Dclaration\n", + " \n", + " for i in range (1,n+1): #Calculation/Iteration\n", + " triangularNumber+=i\n", + " \n", + " print(\"Triangular Number {0} is {1}\".format(n,triangularNumber))\n", + "\n", + "\n", + "def main():\n", + " calculateTriangularNumber(10) #Function call-1\n", + " calculateTriangularNumber(20) #Function call-2\n", + " calculateTriangularNumber(50) #Function call-3\n", + " \n", + "\n", + "if __name__=='__main__': #Setting top level conditional script\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Triangular Number 10 is 55\n", + "Triangular Number 20 is 210\n", + "Triangular Number 50 is 1275\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.5, Page number: 125" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.5.py\n", + "#Revising the Program to Find the Greatest Common Divisor\n", + "\n", + "#Import Library \n", + "import sys \n", + "\n", + "def gcd(u,v): #Function to calculate gcd\n", + " sys.stdout.write(\"gcd of {0} and {1} is: \".format(u,v))\n", + " while(v!=0):\n", + " temp=u%v\n", + " u=v\n", + " v=temp\n", + " sys.stdout.write(\"{0}\\n\".format(u))\n", + "\n", + "\n", + "\n", + "def main(): #Main() function\n", + " gcd(150,35) \n", + " gcd(1026,405)\n", + " gcd(83,240)\n", + "\n", + "if __name__=='__main__': #Setting Top level conditional script \n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "gcd of 150 and 35 is: 5\n", + "gcd of 1026 and 405 is: 27\n", + "gcd of 83 and 240 is: 1\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.6, Page number: 127" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.6.py\n", + "#Finding the Greatest Common Divisor and Returning the Results\n", + "\n", + "#Import Library \n", + "import sys \n", + "\n", + "def gcd(u,v): #Function to calculate gcd\n", + "\n", + " while(v!=0):\n", + " temp=u%v\n", + " u=v\n", + " v=temp\n", + " return u\n", + "\n", + "def main(): #Main() function\n", + " result= gcd(150,35) \n", + " print(\"the gcd of 150 and 35 is: {0}\".format(result))\n", + " result=gcd(1026,405)\n", + " print(\"the gcd of 1026 and 405 is: {0}\".format(result)) \n", + " result= gcd(83,240)\n", + " print(\"the gcd of 83 and 240 is: {0}\".format(result))\n", + "\n", + "if __name__=='__main__': #Top level conditional script \n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the gcd of 150 and 35 is: 5\n", + "the gcd of 1026 and 405 is: 27\n", + "the gcd of 83 and 240 is: 1\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.7, Page number: 129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.7.py\n", + "#Calculating the Absolute Value\n", + "\n", + "#yourstory.in\n", + "\n", + "#Calculations\n", + "def absoluteValue(x): #Function to calculate & return absolute values\n", + " if(x<0):\n", + " x=-x\n", + " return x\n", + "\n", + "def main(): #Main() Function\n", + " \n", + " f1=-15.5\n", + " f2=20.0\n", + " f3=-5.0\n", + " il=-716\n", + "#Result \n", + " result=absoluteValue(f1)\n", + " print(\"result= {0:.2f}\".format(result))\n", + " print(\"f1={0:.2f}\".format(f1))\n", + "\n", + " result=absoluteValue(f2)+absoluteValue(f3)\n", + " print(\"result= {0:.2f}\".format(result))\n", + "\n", + " result=absoluteValue(float(il))\n", + " print(\"result= {0:.2f}\".format(result))\n", + " \n", + " result=absoluteValue(il)\n", + " print(\"resut= {0:.2f}\".format(result))\n", + "\n", + " print(\"{0:.2f}\".format(absoluteValue((-6.0)/4)))\n", + "\n", + "\n", + "#End of Main()\n", + " \n", + "if __name__=='__main__': #Setting Top level conditional script\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "result= 15.50\n", + "f1=-15.50\n", + "result= 25.00\n", + "result= 716.00\n", + "resut= 716.00\n", + "1.50\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.8, Page number: 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.8.py\n", + "#Calculating the Square Root of a Number using Newton-Raphson Method \n", + "\n", + "\n", + "#Function to return absolute value\n", + "def absoluteValue(x): \n", + " if(x<0):\n", + " x=-x\n", + " return x\n", + "\n", + "#function to calculate square root\n", + "def squareRoot(x):\n", + " epsilon=0.0001\n", + " guess=1.0\n", + " while(absoluteValue(guess*guess-x)>=epsilon):\n", + " guess=(x/guess+guess)/2.0\n", + " return guess\n", + "\n", + "#Main()\n", + "def main():\n", + " print(\"squareRoot (2.0) = {0}\".format(squareRoot (2.0)));\n", + " print(\"squareRoot (144.0) = {0}\".format(squareRoot (144.0)));\n", + " print(\"squareRoot (17.5) = {0}\".format(squareRoot (17.5)));\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "squareRoot (2.0) = 1.41421568627\n", + "squareRoot (144.0) = 12.0000000124\n", + "squareRoot (17.5) = 4.18330153622\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.9, Page number: 138" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.9.py\n", + "#Finding the Minimum Value in an Array\n", + "\n", + "#Function to return minimum value\n", + "def minimum(values):\n", + " minValue=values[0] #Variable Declaration\n", + " for i in range (1,10): #Calculation\n", + " if(values[i]<minValue):\n", + " minValue=values[i]\n", + "\n", + " return minValue\n", + " \n", + "#Main()\n", + "def main():\n", + " scores=[] #Variable Declaration\n", + " print(\"Enter 10 scores:\")\n", + " for i in range (0,10):\n", + " scores.append(5) #score=5 for all cases\n", + " #scores.append(input()) \n", + "\n", + " minScore=minimum(scores) #Function call & assignment\n", + " print(\"\\nMinimum score is {0}\\n\".format(minScore)) #Result\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 10 scores:\n", + "\n", + "Minimum score is 5\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.10, Page number: 141" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.10.py\n", + "#Revising the Function to Find the Minimum Value in an Array\n", + "\n", + "#Function to return minimum value\n", + "def minimum(values,numberOfElements): \n", + " minValue=values[0] #Variable Declaration\n", + " for i in range(1,numberOfElements): #Iteration/Calculations\n", + " if(values[i]<minValue):\n", + " minValue=values[i]\n", + " \n", + " return minValue\n", + "\n", + "#Main()\n", + "def main():\n", + " #List/Variable Delcaration\n", + " array1 = [ 157, -28, -37, 26, 10 ] \n", + " array2 = [ 12, 45, 1, 10, 5, 3, 22 ]\n", + " \n", + " #Result\n", + " print(\"array1 minimum: {0}\".format(minimum (array1, 5)));\n", + " print(\"array2 minimum: {0}\".format(minimum (array2, 7)));\n", + "\n", + "#Setting top level conditional script \n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "array1 minimum: -37\n", + "array2 minimum: 1\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.11, Page number: 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.11.py\n", + "#Changing Array Elements in Functions\n", + "\n", + "#Import System Library\n", + "import sys\n", + "\n", + "#Function to multiply elements by 2\n", + "def multiplyBy2(array,n):\n", + " for i in range (0,n): #Iteration/Calculation\n", + " array[i]*=2\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List/Variable Declaration\n", + " floatVals=[1.2, -3.7, 6.2, 8.55] \n", + " multiplyBy2(floatVals,4)\n", + "\n", + " for i in range (0,4):\n", + " sys.stdout.write(\"{0:.2f} \".format(floatVals[i])) #Result\n", + " print(\"\\n\")\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.40 -7.40 12.40 17.10 \n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.12, Page number: 145" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.12.py\n", + "#\n", + "\n", + "#Import system Library \n", + "import sys\n", + "\n", + "#Function to sort the array\n", + "def sort(a,n):\n", + "\n", + " for i in range (0,n-1): #Calculations\n", + " for j in range (i+1,n):\n", + " if(a[i]>a[j]): #Conditional swapping\n", + " temp=a[i]\n", + " a[i]=a[j]\n", + " a[j]=temp\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List/Variable Declaration \n", + " array= [ 34, -5, 6, 0, 12, 100, 56, 22,\\\n", + " 44, -3, -9, 12, 17, 22, 6, 11 ]\n", + " \n", + " print(\"The array before the sort:\\n\");\n", + " for i in range (0,16):\n", + " sys.stdout.write(\"{0} \".format(array[i]))\n", + " \n", + " sort (array, 16); #Function Call\n", + " \n", + " print(\"\\n\\n\\nThe array after the sort:\\n\")\n", + " for i in range (0,16):\n", + " sys.stdout.write(\"{0} \".format(array[i]))\n", + " print(\"\\n\")\n", + "\n", + "\n", + "#Setting top level conditional script \n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The array before the sort:\n", + "\n", + "34 -5 6 0 12 100 56 22 44 -3 -9 12 17 22 6 11 \n", + "\n", + "\n", + "The array after the sort:\n", + "\n", + "-9 -5 -3 0 6 6 11 12 12 17 22 22 34 44 56 100 \n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.13, Page number: 147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.13.py\n", + "#Using Multidimensional Arrays and Functions\n", + "\n", + "#Import system Library\n", + "import sys\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List/Variable Declaration\n", + " sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n", + " [ 12, 10, 52, 0, 7 ],\n", + " [ -2, 1, 2, 4, 9 ] ]\n", + "\n", + "\n", + " print(\"Original matrix:\\n\")\n", + " displayMatrix(sampleMatrix) #Function call-1\n", + "\n", + " scalarMultiply(sampleMatrix, 2) #Function Call-2\n", + " print(\"\\nMultiplied by 2:\\n\")\n", + " \n", + " displayMatrix(sampleMatrix); #Function call-3\n", + " scalarMultiply(sampleMatrix, -1) #Function call-4\n", + " \n", + " print(\"\\nThen multiplied by -1:\\n\")\n", + " displayMatrix(sampleMatrix) #Function call-5\n", + "\n", + "\n", + "#Function to multiply matrix by a scalar quantity \n", + "def scalarMultiply(matrix,scalar):\n", + " for row in range(0,3): #Calculation\n", + " for column in range(0,5):\n", + " matrix[row][column]*=scalar \n", + "\n", + "\n", + "#Function to display the matrix\n", + "def displayMatrix(matrix):\n", + " for row in range(0,3): #Result\n", + " for column in range(0,5):\n", + " sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n", + " sys.stdout.write(\"\\n\")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original matrix:\n", + "\n", + " 7 16 55 13 12\n", + " 12 10 52 0 7\n", + " -2 1 2 4 9\n", + "\n", + "Multiplied by 2:\n", + "\n", + " 14 32 110 26 24\n", + " 24 20 104 0 14\n", + " -4 2 4 8 18\n", + "\n", + "Then multiplied by -1:\n", + "\n", + " -14 -32 -110 -26 -24\n", + " -24 -20 -104 0 -14\n", + " 4 -2 -4 -8 -18\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.13A, Page number: 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.13A.py\n", + "#Multidimensional Variable-Length Arrays\n", + "\n", + "#Import system Library\n", + "import sys\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List/Variable Declaration\n", + " sampleMatrix =[ [ 7, 16, 55, 13, 12 ],\n", + " [ 12, 10, 52, 0, 7 ],\n", + " [ -2, 1, 2, 4, 9 ] ]\n", + "\n", + "\n", + " print(\"Original matrix:\\n\")\n", + " displayMatrix(3,5,sampleMatrix) #Function call-1\n", + "\n", + " scalarMultiply(3,5,sampleMatrix, 2) #Function Call-2\n", + " print(\"\\nMultiplied by 2:\\n\")\n", + " \n", + " displayMatrix(3,5,sampleMatrix); #Function call-3\n", + " scalarMultiply(3,5,sampleMatrix, -1) #Function call-4\n", + " \n", + " print(\"\\nThen multiplied by -1:\\n\")\n", + " displayMatrix(3,5,sampleMatrix) #Function call-5\n", + "\n", + "\n", + "#Function to multiply matrix by a scalar quantity \n", + "def scalarMultiply(nRows,nCols,matrix,scalar):\n", + " for row in range(0,nRows): #Calculation\n", + " for column in range(0,nCols):\n", + " matrix[row][column]*=scalar \n", + "\n", + "\n", + "#Function to display the matrix\n", + "def displayMatrix(nRows,nCols,matrix):\n", + " for row in range(0,nRows): #Result\n", + " for column in range(0,nCols):\n", + " sys.stdout.write(\"{0:7}\".format(matrix[row][column]))\n", + " sys.stdout.write(\"\\n\")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original matrix:\n", + "\n", + " 7 16 55 13 12\n", + " 12 10 52 0 7\n", + " -2 1 2 4 9\n", + "\n", + "Multiplied by 2:\n", + "\n", + " 14 32 110 26 24\n", + " 24 20 104 0 14\n", + " -4 2 4 8 18\n", + "\n", + "Then multiplied by -1:\n", + "\n", + " -14 -32 -110 -26 -24\n", + " -24 -20 -104 0 -14\n", + " 4 -2 -4 -8 -18\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.14, Page number: 153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.14.py\n", + "#Converting a Positive Integer to Another Base\n", + "\n", + "#Import system Libraries\n", + "import sys\n", + "\n", + "#Function to get Number & Base\n", + "def getNumberAndBase():\n", + " \n", + " #Global Reference\n", + " global digit\n", + " global numberToConvert\n", + " global base\n", + " \n", + " #Variable Declaration\n", + " digit=0\n", + " numberToConvert=420 #numberToConvert=input(\"Number to be converted ?\")\n", + " base=8 #base=input(\"Base?\")\n", + " if(base<2 or base>16):\n", + " print(\"Bad base - must be between 2 and 16\\n\");\n", + " base = 10;\n", + "\n", + "#Conversion Function\n", + "def convertNumber():\n", + " \n", + " #Global Reference\n", + " global numberToConvert\n", + " global base\n", + " global convertedNumber\n", + " global digit\n", + " convertedNumber=[0]*64 #List declaration\n", + " \n", + " while(numberToConvert!=0):\n", + " convertedNumber[digit]=numberToConvert%base #Calculations\n", + " digit=digit+1\n", + " numberToConvert/=base\n", + "\n", + "\n", + "#Function to display\n", + "def displayConvertedNumber():\n", + "\n", + " #Global reference \n", + " global baseDigits\n", + " global digit \n", + "\n", + " #List/Variable Declaration\n", + " baseDigits=[ '0', '1', '2', '3', '4', '5', '6', '7',\\\n", + " '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]\n", + " sys.stdout.write(\"Converted number = \")\n", + " \n", + " digit=digit-1\n", + "\n", + " for i in range(digit,-1,-1):\n", + " nextDigit = convertedNumber[i];\n", + " sys.stdout.write(\"{0}\".format(baseDigits[nextDigit])); #Result\n", + "\n", + " sys.stdout.write(\"\\n\")\n", + "\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + " getNumberAndBase() #Function call-1\n", + " convertNumber() #Function call-2\n", + " displayConvertedNumber() #Function call-3\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Converted number = 644\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.15, Page number: 157" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.15.py\n", + "#Illustrating Static and Automatic Variables\n", + "\n", + "\n", + "#Function to display variables\n", + "def auto_static():\n", + " global staticVar #Global reference\n", + " autoVar=1 #variable Declaration\n", + " print(\"automatic = {0}, static = {1}\\n\".format(autoVar,staticVar)); #Result\n", + " \n", + " #Calculation \n", + " autoVar+=1\n", + " staticVar+=1\n", + "\n", + "#Main()\n", + "def main():\n", + " global staticVar\n", + " staticVar=1 #Variable Declaration\n", + " for i in range(0,5):\n", + " auto_static()\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "automatic = 1, static = 1\n", + "\n", + "automatic = 1, static = 2\n", + "\n", + "automatic = 1, static = 3\n", + "\n", + "automatic = 1, static = 4\n", + "\n", + "automatic = 1, static = 5\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 8.16, Page number: 159" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#8.16.py\n", + "#Calculating Factorials Recursively\n", + "\n", + "#Function to calculate factorial\n", + "def factorial(n):\n", + " if( n == 0 ): #Calculation\n", + " result = 1\n", + " else:\n", + " result = n * factorial (n - 1)\n", + " return result;\n", + "\n", + "#Main()\n", + "def main():\n", + " for j in range (0,11):\n", + " print(\"{0:3}! = {1}\\n\".format(j,factorial (j)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 0! = 1\n", + "\n", + " 1! = 1\n", + "\n", + " 2! = 2\n", + "\n", + " 3! = 6\n", + "\n", + " 4! = 24\n", + "\n", + " 5! = 120\n", + "\n", + " 6! = 720\n", + "\n", + " 7! = 5040\n", + "\n", + " 8! = 40320\n", + "\n", + " 9! = 362880\n", + "\n", + " 10! = 3628800\n", + "\n" + ] + } + ], + "prompt_number": 17 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_09.ipynb b/Programming_in_C/Chapter_09.ipynb new file mode 100644 index 00000000..684317a5 --- /dev/null +++ b/Programming_in_C/Chapter_09.ipynb @@ -0,0 +1,594 @@ +{ + "metadata": { + "name": "Chapter IX" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9: Working with structures" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.1, Page number: 167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.1.py\n", + "#Illustrating a Structure/Class in python\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Class Declaration\n", + " class date:\n", + " 'python classes are equivalent to C structures'\n", + " def __init__(self): #Class Constructor\n", + " #Set default values\n", + " self.month=0\n", + " self.day=0\n", + " self.year=0\n", + "\n", + "\n", + " #Creating instance\n", + " today=date() \n", + " \n", + " #Modifying values\n", + " today.month=9 \n", + " today.day=25\n", + " today.year=2004\n", + "\n", + " #Result\n", + " print(\"Today's date is {0}/{1}/{2}\".format(today.month,today.day,today.year%100));\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Today's date is 9/25/4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.2, Page number: 169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.2.py\n", + "#Determining Tomorrows Date\n", + "\n", + "#Main()\n", + "def main():\n", + " #Class Declaration\n", + " class date:\n", + " def __init__(self): #Class Constructor\n", + " #Default values\n", + " month=0\n", + " day=0\n", + " year=0\n", + " #creating instances\n", + " today=date()\n", + " tomorrow=date()\n", + "\n", + " #List Declaration\n", + " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", + " \n", + " print(\"Enter today's date (mm/dd/yyyy):\")\n", + " today.month,today.day,today.year=map(int,\"12/17/2004\".split('/'))\n", + " #today.month,today.day,today.year=map(int,raw_input().split('/'))\n", + "\n", + " #Calculations\n", + " if( today.day != daysPerMonth[today.month - 1] ):\n", + " tomorrow.day = today.day + 1;\n", + " tomorrow.month = today.month;\n", + " tomorrow.year = today.year;\n", + " elif( today.month == 12 ):\n", + " tomorrow.day = 1;\n", + " tomorrow.month = 1;\n", + " tomorrow.year = today.year + 1;\n", + " else:\n", + " tomorrow.day = 1;\n", + " tomorrow.month = today.month + 1;\n", + " tomorrow.year = today.year;\n", + "\n", + " #Result\n", + " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(tomorrow.month,tomorrow.day,tomorrow.year));\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter today's date (mm/dd/yyyy):\n", + "Tomorrow's date is 12/18/2004\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.3, Page number: 171" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.3.py\n", + "#Revising the Program to Determine Tomorrow's Date\n", + "\n", + "#Class Declaration\n", + "class date:\n", + " def __init__(self): #Class Constructor\n", + " #Default values\n", + " month=0\n", + " day=0\n", + " year=0\n", + " \n", + "\n", + "#Main()\n", + "def main():\n", + " #creating instances\n", + " today=date()\n", + " tomorrow=date()\n", + "\n", + " print(\"Enter today's date (mm/dd/yyyy):\")\n", + " today.month,today.day,today.year=map(int,\"2/28/2004\".split('/'))\n", + " #today.month,today.day,today.year=map(int,raw_input().split('/'))\n", + "\n", + " #Calculations\n", + " if( today.day != numberOfDays(today) ):\n", + " tomorrow.day = today.day + 1;\n", + " tomorrow.month = today.month;\n", + " tomorrow.year = today.year;\n", + " elif( today.month == 12 ):\n", + " tomorrow.day = 1;\n", + " tomorrow.month = 1;\n", + " tomorrow.year = today.year + 1;\n", + " else:\n", + " tomorrow.day = 1;\n", + " tomorrow.month = today.month + 1;\n", + " tomorrow.year = today.year;\n", + "\n", + " #Result\n", + " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(tomorrow.month,tomorrow.day,tomorrow.year));\n", + "\n", + "\n", + "#Function to find the number of days in a month \n", + "def numberOfDays(d):\n", + " \n", + " #List Declaration\n", + " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", + " if(isLeapYear(d)==True and d.month==2):\n", + " days=29\n", + " else:\n", + " days = daysPerMonth[d.month - 1];\n", + "\n", + " return days\n", + "\n", + "\n", + "#Function to determine if it's a leap year\n", + "def isLeapYear(d):\n", + " if ( (d.year % 4 == 0 and d.year % 100 != 0) or d.year % 400 == 0 ):\n", + " leapYearFlag = True # Its a leap year\n", + " else:\n", + " leapYearFlag = False # Not a leap year\n", + " \n", + " return leapYearFlag;\n", + "\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter today's date (mm/dd/yyyy):\n", + "Tomorrow's date is 2/29/2004\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.4, Page number: 174" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.4.py\n", + "#Revising the Program to Determine Tomorrow's Date--Version 2\n", + "\n", + "#Class Declaration\n", + "class date:\n", + " def __init__(self): #Class Constructor\n", + " #Default values\n", + " month=0\n", + " day=0\n", + " year=0\n", + " \n", + " def dateUpdate(self,today):\n", + " #Calculations \n", + " tomorrow=date()\n", + " if( today.day != numberOfDays(today) ):\n", + " tomorrow.day = today.day + 1;\n", + " tomorrow.month = today.month;\n", + " tomorrow.year = today.year;\n", + " elif( today.month == 12 ):\n", + " tomorrow.day = 1;\n", + " tomorrow.month = 1;\n", + " tomorrow.year = today.year + 1;\n", + " else:\n", + " tomorrow.day = 1;\n", + " tomorrow.month = today.month + 1;\n", + " tomorrow.year = today.year;\n", + " \n", + " return tomorrow\n", + "\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + " #creating instances\n", + " thisDay=date()\n", + " nextDay=date()\n", + " \n", + " print(\"Enter today's date (mm/dd/yyyy):\")\n", + " thisDay.month,thisDay.day,thisDay.year=map(int,\"2/22/2004\".split('/'))\n", + " #thisDay.month,thisDay.day,thisDay.year=map(int,raw_input().split('/'))\n", + " nextDay=thisDay.dateUpdate(thisDay)\n", + "\n", + " #Result\n", + " print(\"Tomorrow's date is {0}/{1}/{2}\\n\".format(nextDay.month,nextDay.day,nextDay.year));\n", + "\n", + "\n", + "\n", + "\n", + "#Function to find the number of days in a month \n", + "def numberOfDays(d):\n", + " \n", + " #List Declaration\n", + " daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]\n", + " if(isLeapYear(d)==True and d.month==2):\n", + " days=29\n", + " else:\n", + " days = daysPerMonth[d.month - 1];\n", + "\n", + " return days\n", + "\n", + "\n", + "\n", + "#Function to determine if it's a leap year\n", + "def isLeapYear(d):\n", + " if ( (d.year % 4 == 0 and d.year % 100 != 0) or d.year % 400 == 0 ):\n", + " leapYearFlag = True # Its a leap year\n", + " else:\n", + " leapYearFlag = False # Not a leap year\n", + " \n", + " return leapYearFlag;\n", + "\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter today's date (mm/dd/yyyy):\n", + "Tomorrow's date is 2/23/2004\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.5, Page number: 178" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.5.py\n", + "#Updating the Time by One Second\n", + "\n", + "#Class Declaration\n", + "class time:\n", + " def __init__(self):\n", + " hour=0\n", + " minutes=0\n", + " seconds=0\n", + " def timeUpdate(self,now):\n", + " #Calculation\n", + " now.seconds+=1\n", + " if ( now.seconds == 60): #next minute\n", + " now.seconds = 0\n", + " now.minutes+=1 \n", + " if ( now.minutes == 60 ): #next hour\n", + " now.minutes = 0\n", + " now.hour+=1;\n", + " if ( now.hour == 24 ): # midnight\n", + " now.hour = 0\n", + " \n", + "\n", + " return now\n", + "\n", + "#Main()\n", + "def main():\n", + " #Creating instances\n", + " currentTime=time()\n", + " nextTime=time()\n", + " #User Input\n", + " print(\"Enter the time (hh:mm:ss):\")\n", + " currentTime.hour,currentTime.minutes,currentTime.seconds=map(int,\"16:14:59\".split(':'))\n", + " #currentTime.hour,currentTime.minutes,currentTime.seconds=map(int,raw_input().split(':'))\n", + " \n", + " nextTime =currentTime.timeUpdate (currentTime);\n", + " \n", + " #Result\n", + " print(\"Updated time is {0}:{1}:{2}\\n\".format(nextTime.hour,nextTime.minutes,nextTime.seconds))\n", + "\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the time (hh:mm:ss):\n", + "Updated time is 16:15:0\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.6, Page number: 183" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.6.py\n", + "#Illustrating Arrays of Structures\n", + "\n", + "#Import sys Library\n", + "import sys\n", + "\n", + "#Class Declaration\n", + "class time:\n", + " def __init__(self,h,m,s):\n", + " #Variable Declarations\n", + " self.hour=h\n", + " self.minutes=m\n", + " self.seconds=s\n", + " def timeUpdate(self,now):\n", + " #Calculation\n", + " now.seconds+=1\n", + " if ( now.seconds == 60): #next minute\n", + " now.seconds = 0\n", + " now.minutes+=1 \n", + " if ( now.minutes == 60 ): #next hour\n", + " now.minutes = 0\n", + " now.hour+=1;\n", + " if ( now.hour == 24 ): # midnight\n", + " now.hour = 0\n", + " \n", + "\n", + " return now\n", + "\n", + "#Main()\n", + "def main():\n", + " #Creating instances\n", + " testTimes = []\n", + " testTimes.append(time(11,59,59))\n", + " testTimes.append(time(12,0,0))\n", + " testTimes.append(time(1,29,59))\n", + " testTimes.append(time(23,59,59))\n", + " testTimes.append(time(19,12,27))\n", + "\n", + " #Result\n", + " for i in range(0,5):\n", + " \n", + " sys.stdout.write(\"Time is {0:2}:{1:2}:{2:2} \".format(testTimes[i].hour,testTimes[i].minutes,testTimes[i].seconds))\n", + "\n", + " nextTime =testTimes[i].timeUpdate(testTimes[i])\n", + " \n", + " \n", + " sys.stdout.write(\"...one second later it's {0:2}:{1:2}:{2:2}\\n\".format(testTimes[i].hour,testTimes[i].minutes,testTimes[i].seconds))\n", + "\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Time is 11:59:59 ...one second later it's 12: 0: 0\n", + "Time is 12: 0: 0 ...one second later it's 12: 0: 1\n", + "Time is 1:29:59 ...one second later it's 1:30: 0\n", + "Time is 23:59:59 ...one second later it's 0: 0: 0\n", + "Time is 19:12:27 ...one second later it's 19:12:28\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 9.7, Page number: 188" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#9.7.py\n", + "#Illustrating Structures and Arrays\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + " #Class Declaration\n", + " class month:\n", + " def __init__(self,n,na): #Class Constructor\n", + " self.numberOfDays=n\n", + " self.name=na\n", + "\n", + " #List Declaration\n", + " months=[]\n", + " months.append(month(31,['j','a','n']))\n", + " months.append(month(28,['f','e','b']))\n", + " months.append(month(31,['m','a','r']))\n", + " months.append(month(30,['a','p','r']))\n", + " months.append(month(31,['m','a','y']))\n", + " months.append(month(30,['j','u','n']))\n", + " months.append(month(31,['j','u','l']))\n", + " months.append(month(31,['a','u','g']))\n", + " months.append(month(30,['s','e','p']))\n", + " months.append(month(31,['o','c','t']))\n", + " months.append(month(30,['n','o','v']))\n", + " months.append(month(31,['d','e','c']))\n", + " \n", + " \n", + " #Result\n", + " print(\"Month NumberOfDays\")\n", + " print(\"----- ------------\")\n", + " for i in range (0,12):\n", + " print(\"{0}{1}{2} {3}\\n\".format(months[i].name[0],months[i].name[1],months[i].name[2], months[i].numberOfDays))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month NumberOfDays\n", + "----- ------------\n", + "jan 31\n", + "\n", + "feb 28\n", + "\n", + "mar 31\n", + "\n", + "apr 30\n", + "\n", + "may 31\n", + "\n", + "jun 30\n", + "\n", + "jul 31\n", + "\n", + "aug 31\n", + "\n", + "sep 30\n", + "\n", + "oct 31\n", + "\n", + "nov 30\n", + "\n", + "dec 31\n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_10.ipynb b/Programming_in_C/Chapter_10.ipynb new file mode 100644 index 00000000..1ec5bf12 --- /dev/null +++ b/Programming_in_C/Chapter_10.ipynb @@ -0,0 +1,768 @@ +{ + "metadata": { + "name": "Chapter X" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 10: Character strings" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.1, Page number: 197" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.1.py\n", + "#Concatenating Character Arrays\n", + "\n", + "#Import system library\n", + "import sys\n", + "\n", + "#Function to concatinate strings\n", + "def concat(result,str1,n1,str2,n2): #Calculations\n", + " \n", + " #copy str1 to result\n", + " for i in range(0,n1):\n", + " result[i]=str1[i]\n", + " \n", + " #copy str2 to result \n", + " for j in range(0,n2):\n", + " result[n1+j]=str2[j]\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List Declaration\n", + " s1 = [ 'T', 'e', 's', 't', ' ']\n", + " s2 = [ 'w', 'o', 'r', 'k', 's', '.' ]\n", + " s3=[0]*11\n", + "\n", + " concat(s3,s1,5,s2,6) #Fucntion call\n", + "\n", + " for i in range(0,11): #Result\n", + " sys.stdout.write(\"{0}\".format(s3[i]))\n", + "\n", + " sys.stdout.write(\"\\n\")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Test works.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.2, Page number: 199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.2.py\n", + "#Counting the Characters in a String\n", + "\n", + "\n", + "#FUnction to count number of characters ina string\n", + "def stringLength(string):\n", + " count = 0\n", + " while(string[count]!='\\0'): #Calculation\n", + " count+=1\n", + " return count\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List Declaration\n", + " word1 = [ 'a', 's', 't', 'e', 'r', '\\0' ]\n", + " word2 = [ 'a', 't', '\\0' ]\n", + " word3 = [ 'a', 'w', 'e', '\\0' ]\n", + "\n", + " #Result\n", + " print(\"{0} {1} {2}\\n\".format(stringLength (word1),stringLength (word2),stringLength (word3)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 2 3\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.3, Page number: 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.3.py\n", + "#Concatenating Character Strings\n", + "\n", + "\n", + "#Function to concatinate strings\n", + "def concat(result,str1,str2): #Calculations\n", + " \n", + " #copy str1 to result\n", + " for i in str1:\n", + " result.append(i)\n", + " \n", + " #copy str2 to result \n", + " for j in str2:\n", + " result.append(j)\n", + " \n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #String Declaration\n", + " s1 = \"Test \"\n", + " s2 = \"works.\"\n", + " s3=[]\n", + "\n", + " concat(s3,s1,s2) #Fucntion call\n", + " print(''.join(s3)) #Result\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Test works.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.4, Page number: 204" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.4.py\n", + "#Testing Strings for Equality\n", + "\n", + "#Function to compare 2 strings\n", + "def equalStrings(s1,s2):\n", + " \n", + " #Calculation\n", + " if(s1==s2):\n", + " areEqual=True\n", + " else:\n", + " areEqual=False\n", + " return areEqual\n", + "\n", + "\n", + "\n", + "def main():\n", + "\n", + " #String Declaration\n", + " stra = \"string compare test\";\n", + " strb = \"string\";\n", + "\n", + " #Result\n", + " print(\"{0}\".format(equalStrings (stra, strb)))\n", + " print(\"{0}\".format(equalStrings (stra, stra)))\n", + " print(\"{0}\".format(equalStrings (strb, \"string\")))\n", + "\n", + "\n", + "#Setting top level confditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "False\n", + "True\n", + "True\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.5, Page number: 207" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.5.py\n", + "#Reading Strings\n", + "\n", + "#Main()\n", + "def main():\n", + " print(\"Enter Text:\\n\")\n", + "\n", + " #User Input\n", + " s1,s2,s3=map(str,\"System expansion bus\".split())\n", + " #s1,s2,s3=map(str,raw_input().split())\n", + " \n", + " \n", + " #Result\n", + " print(\"\\ns1={0}\\ns2={1}\\ns3={2}\\n\".format(s1,s2,s3))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Text:\n", + "\n", + "\n", + "s1=System\n", + "s2=expansion\n", + "s3=bus\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.6, Page number: 209" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.6.py\n", + "#Reading Lines of Data\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List declaration\n", + " line=['sample text']\n", + " for i in range(0,3):\n", + " readLine(line) #Function call\n", + " print(\"{0}\\n\".format(''.join(line))) #Result\n", + "\n", + "\n", + "#Function to read user input\n", + "def readLine(line):\n", + " \n", + " line.pop()\n", + " line.append(\"This is a sample line of text\"+\" \")\n", + " #line.append(raw_input()+\" \")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a sample line of text \n", + "\n", + "This is a sample line of text \n", + "\n", + "This is a sample line of text \n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.7, Page number: 211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.7.py\n", + "#Counting Words\n", + "\n", + "#Function to determine if a character is alphabetic\n", + "def alphabetic(c):\n", + " if ( (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') ):\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "\n", + "#Function to count the number of words in a string\n", + "def countWords(string):\n", + " \n", + " #Variable Declaration\n", + " wordCount=0\n", + " lookingForWord=True\n", + " \n", + " #Calculations\n", + " for i in string:\n", + " if(alphabetic(i)):\n", + " if(lookingForWord):\n", + " wordCount+=1\n", + " lookingForWord=False\n", + " else:\n", + " lookingForWord=True\n", + " return wordCount\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #String Declaration\n", + " text1 = \"Well, here goes.\"\n", + " text2 = \"And here we go... again.\"\n", + " \n", + " #Result\n", + " print(\"{0} - words = {1}\\n\".format(text1,countWords(text1)))\n", + " print(\"{0} - words = {1}\\n\".format(text2,countWords(text2)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Well, here goes. - words = 3\n", + "\n", + "And here we go... again. - words = 5\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.8, Page number: 214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.8.py\n", + "#Counting Words in a Piece of Text\n", + "\n", + "\n", + "#Function to read a piece of text\n", + "def readLine(line):\n", + " \n", + " line.pop()\n", + " line.append(\"This is a dummy text to replace raw_input()\"+\" \")\n", + " #line.append(raw_input()+\" \")\n", + "\n", + "\n", + "#Function to determine if a character is alphabetic\n", + "def alphabetic(c):\n", + " if ( (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') ):\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "\n", + "#Function to count the number of words in a string\n", + "def countWords(string):\n", + " \n", + " #Variable Declaration\n", + " wordCount=0\n", + " lookingForWord=True\n", + " \n", + " #Calculations\n", + " for i in string:\n", + " if(alphabetic(i)):\n", + " if(lookingForWord):\n", + " wordCount+=1\n", + " lookingForWord=False\n", + " else:\n", + " lookingForWord=True\n", + " return wordCount\n", + "\n", + "\n", + "\n", + "def main():\n", + " #Variable Declaration\n", + " totalWords=0\n", + " endOfText=False\n", + " text=['sample']\n", + "\n", + " print(\"Type in your text.\\n\")\n", + " print(\"When you are done, press 'RETURN'.\\n\\n\")\n", + " \n", + " readLine (text)\n", + " \n", + " #Increment Counter\n", + " totalWords += countWords(''.join(text))\n", + " \n", + " #Result\n", + " print(\"\\nThere are {0} words in the above text.\\n\".format(totalWords))\n", + "\n", + "\n", + "#Top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type in your text.\n", + "\n", + "When you are done, press 'RETURN'.\n", + "\n", + "\n", + "\n", + "There are 9 words in the above text.\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.9, Page number: 220" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.9.py\n", + "#Using the Dictionary Lookup Program\n", + "\n", + "#Class Declaration\n", + "class entry:\n", + " def __init__(self,w,d):\n", + " self.word=w\n", + " self.definition=d\n", + "\n", + "\n", + "#Function to check equality of two strings\n", + "def equalStrings(str1,str2):\n", + " if(str1==str2):\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "#Lookup function to return dictionary entry\n", + "def lookup(dictionary,search,entries):\n", + " for i in range(0,entries):\n", + " if ( equalStrings(search, dictionary[i].word) ):\n", + " return i;\n", + " return -1;\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " entries=10\n", + " global entry\n", + " dictionary=[]\n", + " \n", + " dictionary.append(entry(\"aardvark\",\"a burrowing African mammal\"))\n", + " dictionary.append(entry(\"abyss\",\"a bottomless pit\"))\n", + " dictionary.append(entry(\"acumen\",\"mentally sharp keen\"))\n", + " dictionary.append(entry(\"addle\",\"to become confused\"))\n", + " dictionary.append(entry(\"aerie\",\"a high nest\"))\n", + " dictionary.append(entry(\"affix\",\"to append; attach\"))\n", + " dictionary.append(entry(\"agar\",\"a jelly made from seaweed\"))\n", + " dictionary.append(entry(\"ahoy\",\"nautical call of greeting\"))\n", + " dictionary.append(entry(\"aigrette\",\"an ornamental cluster of feathers\"))\n", + " dictionary.append(entry(\"ajar\",\"partially opened\"))\n", + "\n", + "\n", + " print(\"Enter word:\")\n", + " word='ajar' #word=raw_input()\n", + " entry=lookup(dictionary,word,entries)\n", + "\n", + " #Result\n", + " if(entry!=-1):\n", + " print(\"{0}\".format(dictionary[entry].definition))\n", + " else:\n", + " print(\"Sorry, the word {0} is not in my dictionary.\\n\".format(word))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter word:\n", + "partially opened\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.10, Page number: 224" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.10.py\n", + "#Modifying the Dictionary Lookup Using Binary Search\n", + "\n", + "#Class Declaration\n", + "class entry:\n", + " def __init__(self,w,d): #Class constructor\n", + " self.word=w\n", + " self.definition=d\n", + "\n", + "\n", + "#Function to compare two strings\n", + "def compareStrings(s1,s2):\n", + "\n", + " #Calculations\n", + " if( s1 < s2 ):\n", + " answer = -1;\n", + " elif( s1 == s2 ):\n", + " answer = 0;\n", + " else:\n", + " answer=1\n", + "\n", + " return answer\n", + "\n", + "\n", + "\n", + "#Lookup function to return dictionary entry\n", + "def lookup(dictionary,search,entries):\n", + " \n", + " #Variable Declaration\n", + " low=0\n", + " high=entries-1\n", + "\n", + " #Calculations\n", + " while(low<=high):\n", + " mid=(low+high)/2\n", + " result=compareStrings(dictionary[mid].word,search)\n", + "\n", + " if(result==-1):\n", + " low=mid+1\n", + " elif(result==1):\n", + " high=mid-1\n", + " else:\n", + " return mid\n", + "\n", + " return -1\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " entries=10\n", + " global entry\n", + " dictionary=[]\n", + " \n", + " dictionary.append(entry(\"aardvark\",\"a burrowing African mammal\"))\n", + " dictionary.append(entry(\"abyss\",\"a bottomless pit\"))\n", + " dictionary.append(entry(\"acumen\",\"mentally sharp keen\"))\n", + " dictionary.append(entry(\"addle\",\"to become confused\"))\n", + " dictionary.append(entry(\"aerie\",\"a high nest\"))\n", + " dictionary.append(entry(\"affix\",\"to append; attach\"))\n", + " dictionary.append(entry(\"agar\",\"a jelly made from seaweed\"))\n", + " dictionary.append(entry(\"ahoy\",\"nautical call of greeting\"))\n", + " dictionary.append(entry(\"aigrette\",\"an ornamental cluster of feathers\"))\n", + " dictionary.append(entry(\"ajar\",\"partially opened\"))\n", + "\n", + " print(\"Enter word:\")\n", + " word='ahoy' #word=raw_input()\n", + " entry=lookup(dictionary,word,entries)\n", + "\n", + " #Result\n", + " if(entry!=-1):\n", + " print(\"{0}\".format(dictionary[entry].definition))\n", + " else:\n", + " print(\"Sorry, the word {0} is not in my dictionary.\\n\".format(word))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter word:\n", + "nautical call of greeting\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 10.11, Page number: 229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#10.11.py\n", + "#Converting a String to its Integer Equivalent\n", + "\n", + "\n", + "#Function to convert a string to an integer\n", + "def strToInt(string):\n", + " \n", + " return int(string)\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Result\n", + " print(\"{0}\\n\".format(strToInt(\"245\")))\n", + " print(\"{0}\\n\".format(strToInt(\"100\") + 25))\n", + " print(\"{0}\\n\".format(strToInt(\"13\")))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "245\n", + "\n", + "125\n", + "\n", + "13\n", + "\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_11.ipynb b/Programming_in_C/Chapter_11.ipynb new file mode 100644 index 00000000..a010bcdb --- /dev/null +++ b/Programming_in_C/Chapter_11.ipynb @@ -0,0 +1,842 @@ +{ + "metadata": { + "name": "Chapter XI" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Pointers" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.1, Page number: 237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.1.py\n", + "#Illustrating Pointers\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " count=10\n", + " \n", + " int_ptr=count\n", + " x=int_ptr\n", + " \n", + " #Result\n", + " print(\"count={0} , x={1}\".format(count,x))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "count=10 , x=10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.2, Page number: 238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.2.py\n", + "#More Pointer Basics\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " c='Q'\n", + " char_pointer=c\n", + " print(\"{0} {1}\".format(c,char_pointer))\n", + "\n", + " c='/'\n", + " char_pointer=c\n", + " print(\"{0} {1}\".format(c,char_pointer))\n", + "\n", + " char_pointer='('\n", + " c=char_pointer\n", + " print(\"{0} {1}\".format(c,char_pointer))\n", + "\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Q Q\n", + "/ /\n", + "( (\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.3, Page number: 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.3\n", + "#Using Pointers in Expressions\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " i1=5\n", + " p1=i1\n", + " i2=p1/2+10\n", + " p2=p1\n", + "\n", + " #Result\n", + " print(\"i1 = {0}, i2 = {1}, p1 = {2}, p2 = {3}\\n\".format(i1, i2, p1, p2))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i1 = 5, i2 = 12, p1 = 5, p2 = 5\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.4, Page number: 242" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.4.py\n", + "#Using Pointers to classes\n", + "\n", + "#Main()\n", + "def main():\n", + " class date:\n", + " def __init__(self): #Class constructor\n", + " self.month=0\n", + " self.day=0\n", + " self.year=0\n", + "\n", + " #Creating instances\n", + " today=date()\n", + " datePtr=today\n", + "\n", + " datePtr.month=9\n", + " datePtr.day=25\n", + " datePtr.year=2004\n", + "\n", + " #Result\n", + " print(\"Today's date is {0}/{1}/{2}\".format(datePtr.month, datePtr.day, datePtr.year))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Today's date is 9/25/2004\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.5, Page number: 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.5.py\n", + "#Using classes Containing pointer variables\n", + "\n", + "#Main()\n", + "def main():\n", + " class intPtrs:\n", + " def __init__(self): #Class constructor\n", + " self.p1=0\n", + " self.p2=0\n", + "\n", + " #Variable Declarations & Instance creation\n", + " i1=100\n", + " pointers=intPtrs()\n", + " pointers.p1=i1\n", + " i2=-97\n", + " pointers.p2=i2\n", + "\n", + " #Result\n", + " print(\"i1 = {0}, pointers.p1 = {1}\\n\".format(i1, pointers.p1))\n", + " print(\"i2 = {0}, pointers.p2 = {1}\\n\".format(i2, pointers.p2))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i1 = 100, pointers.p1 = 100\n", + "\n", + "i2 = -97, pointers.p2 = -97\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.6, Page number: 246" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.6.py\n", + "#Using Linked Lists\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + " class entry:\n", + " def __init(self): #Class constructor\n", + " self.value=0\n", + " self.nxt=0\n", + "\n", + " #Variable declarations & instance creation\n", + " n1=entry()\n", + " n1.value=100\n", + " n2=entry()\n", + " n2.value=200\n", + " n3=entry()\n", + " n3.value=300\n", + "\n", + " n1.nxt=n2\n", + " n2.nxt=n3\n", + "\n", + " i=n1.nxt.value\n", + "\n", + " #Result\n", + " print(\"{0}\".format(i))\n", + " print(\"{0}\".format(n2.nxt.value))\n", + "\n", + "\n", + "\n", + "#setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "200\n", + "300\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.7, Page number: 250" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.7.py\n", + "#Traversing a linked list\n", + "\n", + "#Main()\n", + "def main():\n", + " class entry:\n", + " def __init__(self): #Class constructor\n", + " self.value=0\n", + " self.nxt=0\n", + "\n", + " #Creating class instances\n", + " n1=entry()\n", + " n2=entry()\n", + " n3=entry()\n", + " list_pointer=n1\n", + "\n", + " #Variable Declaration\n", + " n1.value=100\n", + " n2.value=200\n", + " n3.value=300\n", + " n1.nxt=n2\n", + " n2.nxt=n3\n", + " n3.nxt=0\n", + "\n", + " while(list_pointer!=0): #Calculation\n", + " print(\"{0}\\n\".format(list_pointer.value))\n", + " list_pointer = list_pointer.nxt;\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n", + "\n", + "200\n", + "\n", + "300\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.8, Page number: 254" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.8.py\n", + "#Using Pointers and Functions\n", + "\n", + "#Function to change incoming varible\n", + "def test(int_pointer):\n", + " int_pointer=100\n", + " return int_pointer\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable declaration\n", + " i=50\n", + " p=i\n", + " print(\"Before the call to test i = {0}\".format(p))\n", + " \n", + " p=test(i) #Function call\n", + "\n", + " print(\"After the call to test i = {0}\".format(p))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before the call to test i = 50\n", + "After the call to test i = 100\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.9, Page number: 255" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.9.py\n", + "#Using Pointers to Exchange Values\n", + "\n", + "#Function to exchange & return values\n", + "def exchange(pint1,pint2):\n", + " temp=pint1\n", + " pint1=pint2\n", + " pint2=temp\n", + " return pint1,pint2\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " i1=-5\n", + " i2=66\n", + " p1=i1\n", + " p2=i2\n", + "\n", + " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", + " \n", + " i1,i2=exchange(p1,p2) #Fucntion call-1\n", + "\n", + " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", + "\n", + " i1,i2=exchange(i1,i2) #Fucntion call-2\n", + "\n", + " print(\"i1 ={0}, i2 ={1}\\n\".format(i1, i2))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i1 =-5, i2 =66\n", + "\n", + "i1 =66, i2 =-5\n", + "\n", + "i1 =-5, i2 =66\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.10, Page number: 257" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.10.py\n", + "#Returning a Pointer from a Function\n", + "\n", + "#Class Declaration\n", + "class entry:\n", + " def __init__(self): #Class constructor\n", + " self.value=0\n", + " selfnxt=0\n", + "\n", + " def FindEntry(listPtr,match): #Function to traverse list\n", + "\n", + " #Calculation\n", + " while(listPtr!=0):\n", + " if(int(listPtr.value)==int(match)):\n", + " return listPtr\n", + " else:\n", + " listPtr=listPtr.nxt\n", + "\n", + " return 0\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Creating instance\n", + " n1=entry()\n", + " n2=entry()\n", + " n3=entry()\n", + " listPtr=entry()\n", + "\n", + " #Variable declaration\n", + " n1.value=100\n", + " n2.value=200\n", + " n3.value=300\n", + "\n", + " n1.nxt=n2\n", + " n2.nxt=n3\n", + " n3.nxt=0\n", + " listStart=n1\n", + " \n", + " print(\"Enter value to locate: \")\n", + " search=200 #search=raw_input()\n", + " listPtr = entry.FindEntry (listStart, search) #Function call\n", + "\n", + " #Result\n", + " if ( listPtr != 0 ):\n", + " print(\"Found {0}.\\n\".format(listPtr.value))\n", + " else:\n", + " print(\"Not found.\\n\")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to locate: \n", + "Found 200.\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.11, Page number: 262" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.11.py\n", + "#Working with Pointers to Arrays/Lists\n", + "\n", + "#Function to evaluate sum of array elements\n", + "def arraySum (array,n):\n", + "\n", + " #Variable Declaration\n", + " sum = 0 \n", + " arrayEnd = n\n", + " #Calculation\n", + " for i in range(0,arrayEnd):\n", + " sum += array[i]\n", + " return sum\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List Declaration\n", + " values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]\n", + " #Result\n", + " print(\"The sum is {0}\\n\".format(arraySum (values, 10)))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum is 21\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.12, Page number: 265" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.12.py\n", + "#Summing the Elements of an Array--version 2\n", + "\n", + "#Function to evaluate sum of array elements\n", + "def arraySum (array,n):\n", + " #Variable Declaration\n", + " sum = 0 \n", + " #Calculation\n", + " for i in array:\n", + " sum += i\n", + " return sum\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #List Declaration\n", + " values = [ 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 ]\n", + " #Result\n", + " print(\"The sum is {0}\\n\".format(arraySum(values,10)))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum is 21\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.13, Page number: 266" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.13.py\n", + "#Version of copyString\n", + "\n", + "#Function to copy string from source to destinaton\n", + "def copyString(to, frm):\n", + " global string2\n", + " to=frm\n", + " string2=to\n", + "\n", + "#Main()\n", + "def main():\n", + " \n", + " global string2\n", + " #String declaration\n", + " string1=\"A string to be copied\"\n", + " string2=\"\"\n", + " copyString(string2,string1) #Functon call\n", + "\n", + " print(\"{0}\\n\".format(string2))\n", + " copyString (string2, \"So is this.\") #Function call\n", + " print(\"{0}\\n\".format(string2))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A string to be copied\n", + "\n", + "So is this.\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.14, Page number: 271" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.14.py\n", + "#Revised version of copyString\n", + "\n", + "#Function to copy string from source to destinaton\n", + "def copyString(to, frm):\n", + " global string2\n", + " to=frm\n", + " string2=to\n", + "\n", + "#Main()\n", + "def main():\n", + " \n", + " global string2\n", + " #String declaration\n", + " string1=\"A string to be copied\"\n", + " string2=\"\"\n", + " copyString(string2,string1) #Functon call\n", + "\n", + " print(\"{0}\\n\".format(string2))\n", + " copyString (string2, \"So is this.\") #Function call\n", + " print(\"{0}\\n\".format(string2))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A string to be copied\n", + "\n", + "So is this.\n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 11.15, Page number: 272" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#11.15.PY\n", + "#Find the Length of a String\n", + "\n", + "#Function to return string length\n", + "def stringLength(string):\n", + " return len(string) #Calculation\n", + "\n", + "#Main()\n", + "def main():\n", + " print(\"{0} \".format(stringLength (\"stringLength test\")))\n", + " print(\"{0} \".format(stringLength (\"\")))\n", + " print(\"{0}\".format(stringLength (\"complete\")))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "17 \n", + "0 \n", + "8\n" + ] + } + ], + "prompt_number": 15 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_12.ipynb b/Programming_in_C/Chapter_12.ipynb new file mode 100644 index 00000000..7f498638 --- /dev/null +++ b/Programming_in_C/Chapter_12.ipynb @@ -0,0 +1,258 @@ +{ + "metadata": { + "name": "Chapter XII" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 12: Operations on bits" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 12.1, Page number: 282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#12.1.py\n", + "#The bitwise AND operator\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " word1=77\n", + " word2=150\n", + " word3=210\n", + " \n", + " #Result\n", + " print(word1&word2)\n", + " print(word1&word1)\n", + " print(word1&word2&word3)\n", + " print(word1&1)\n", + "\n", + "#Setting top level conditioanl script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n", + "77\n", + "0\n", + "1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 12.2, Page number: 286" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#12.2.py\n", + "#Illustrate Bitwise Opertors\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #variable Declarations\n", + " w1=525\n", + " w2=707\n", + " w3=122\n", + " \n", + " print(\"{0:5} {1:5} {2:5}\".format(w1&w2,w1|w2,w1^w2))\n", + " print(\"{0:5} {1:5} {2:5}\".format(~w1,~w2,~w3))\n", + " print(\"{0:5} {1:5} {2:5}\".format(w1^w1,w1&~w2,w1|w2|w3))\n", + " print(\"{0:5} {1:5}\".format(w2&w3,w1|w2&~w3))\n", + " print(\"{0:5} {1:5}\".format(~(~w1&~w2),~(~w1|~w2)))\n", + "\n", + " #Exchange variables\n", + " w1^=w2\n", + " w2^=w1\n", + " w1^=w2\n", + " print(\"w1={0:3} w2={1:3}\".format(w1,w2))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 513 719 206\n", + " -526 -708 -123\n", + " 0 12 767\n", + " 66 653\n", + " 719 513\n", + "w1=707 w2=525\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 12.3, Page number: 288" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#12.3.py\n", + "#Implementing a Shift Function\n", + "\n", + "#Function to perform bitwise shift\n", + "def shift(value,n):\n", + " if(n>0):\n", + " value <<=n #Left shift\n", + " else:\n", + " value >>=-n #Right Shift\n", + "\n", + " return value\n", + "\n", + "#Main()\n", + "def main():\n", + " #Variable Declaration\n", + " w1=177777\n", + " w2=444\n", + "\n", + " #Result\n", + " print(\"{0:7} {1:7}\".format(shift(w1,5),w1<<5))\n", + " print(\"{0:7} {1:7}\".format(shift(w1,-6),w1>>6))\n", + " print(\"{0:7} {1:7}\".format(shift(w2,0),w2>>0))\n", + " print(\"{0:7} \".format(shift(shift(w1,-3),3)))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5688864 5688864\n", + " 2777 2777\n", + " 444 444\n", + " 177776 \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 12.4, Page number: 290" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#12.4.py\n", + "#Implementing a Rotate Function\n", + "\n", + "#Function to perform rotation\n", + "def rotate(value,n):\n", + "\n", + " #Calculation\n", + " if(n>0):\n", + " n=n%32\n", + " else:\n", + " n=-(-n%32)\n", + "\n", + " if(n==0):\n", + " result=value\n", + " elif(n>0):\n", + " bits=value>>(32-n)\n", + " result=value<<n|bits\n", + " else:\n", + " n=-n\n", + " bits=value<<(32-n)\n", + " result=value>>n|bits\n", + "\n", + " return result\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "#Variable Declaration\n", + " w1=0xabcdef00\n", + " w2=0xffff1122\n", + "\n", + " print(\"%x\"%rotate(w1,8))\n", + " print(\"%x\"%rotate(w1,-16))\n", + " print(\"%x\"%rotate(w2,4))\n", + " print(\"%x\"%rotate(w2,-2))\n", + " print(\"%x\"%rotate(w1,0))\n", + " print(\"%x\"%rotate(w1,44))\n", + " \n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "abcdef00ab\n", + "abcdef00abcd\n", + "ffff1122f\n", + "3fffc448bfffc448\n", + "abcdef00\n", + "abcdef00abc\n" + ] + } + ], + "prompt_number": 4 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_13.ipynb b/Programming_in_C/Chapter_13.ipynb new file mode 100644 index 00000000..0ccf61c4 --- /dev/null +++ b/Programming_in_C/Chapter_13.ipynb @@ -0,0 +1,201 @@ +{ + "metadata": { + "name": "Chapter XIII" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 13: The preprocessor" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 13.1, Page number: 300" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#13.1.py\n", + "#defining constants\n", + "\n", + "#Variable Decarations\n", + "global YES\n", + "global NO\n", + "YES=1\n", + "NO=0\n", + "\n", + "\n", + "#Function to check if a number is even or odd\n", + "def isEven(number):\n", + " global YES\n", + " global NO\n", + "\n", + " if(number%2==0):\n", + " answer=YES\n", + " else:\n", + " answer=NO\n", + "\n", + " return answer\n", + "\n", + "#Main()\n", + "def main():\n", + " global YES\n", + " global NO\n", + "\n", + " #Calculation/Result\n", + " if(isEven(17)==YES):\n", + " print(\"YES\")\n", + " else:\n", + " print(\"NO\")\n", + "\n", + " \n", + " if(isEven(20)==YES):\n", + " print(\"YES\")\n", + " else:\n", + " print(\"NO\")\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "NO\n", + "YES\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 13.2, Page number: 302" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#13.2.py\n", + "#Working more with constants\n", + "\n", + "global PI\n", + "PI= 3.141592654\n", + "\n", + "\n", + "#Function to calculate area\n", + "def area(r):\n", + " global PI\n", + " return (PI*r*r)\n", + "\n", + "#Function to calculate circumference\n", + "def circumference(r):\n", + " global PI\n", + " return (2*PI*r)\n", + "\n", + "#Function to calculate volume\n", + "def volume(r):\n", + " global PI\n", + " return (1.33 * PI*r*r*r)\n", + " \n", + "\n", + "#Main()\n", + "def main():\n", + " print(\"radius=1 {0:5} {1:5} {2:5}\".format(area(1),circumference(1),volume(1)))\n", + " print(\"radius=4.98 {0:5} {1:5} {2:5}\".format(area(4.98),circumference(4.98),volume(4.98)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "radius=1 3.141592654 6.283185308 4.17831822982\n", + "radius=4.98 77.9127544563 31.2902628338 516.047337866\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 13.3, Page number: 314" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#13.3.py\n", + "#Defining global constants--advance\n", + "\n", + "#Variable Declaration\n", + "global QUARTS_PER_LITER\n", + "QUARTS_PER_LITER=1.05687\n", + "\n", + "#Main()\n", + "def main():\n", + " global QUARTS_PER_LITER #Global Reference\n", + "\n", + " print(\"***Liters to galons***\")\n", + " print(\"Enter the number of Litres\")\n", + " liters=12 #liters=raw_input()\n", + "\n", + " gallons=float(liters)*QUARTS_PER_LITER/4.0 #calculation\n", + "\n", + " print(\"{0} Liters = {1} gallons\".format(liters,gallons))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "***Liters to galons***\n", + "Enter the number of Litres\n", + "12 Liters = 3.17061 gallons\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_14.ipynb b/Programming_in_C/Chapter_14.ipynb new file mode 100644 index 00000000..4b34f1d0 --- /dev/null +++ b/Programming_in_C/Chapter_14.ipynb @@ -0,0 +1,92 @@ +{ + "metadata": { + "name": "Chapter XIV" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 14: More on data types" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 14.1, Page number: 323" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#14.1.py\n", + "#Using Enumerated Data Type\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Declaring an Enumerator \n", + " def enum(**enums):\n", + " return type('Enum',(),enums)\n", + "\n", + " #Defining an Enumerator\n", + " month=enum(january=1,february=2,march=3,april=4,may=5,june=6,july=7,\\\n", + " august=8,september=9,october=10,november=11,december=12)\n", + " days=0\n", + "\n", + " print(\"Enter month number:\")\n", + " aMonth=8 #aMonth=raw_input()\n", + "\n", + " #Calculations\n", + " if(int(aMonth)==month.january or int(aMonth)==month.march or \\\n", + " int(aMonth)==month.may or int(aMonth)==month.july or \\\n", + " int(aMonth)==month.august or int(aMonth)==month.october or int(aMonth)==month.december):\n", + " days=31\n", + "\n", + " elif(int(aMonth)==month.april or int(aMonth)==month.june or \\\n", + " int(aMonth)==month.september or int(aMonth)==month.november):\n", + " days=30\n", + " elif(int(aMonth)==month.february):\n", + " days=28\n", + " else:\n", + " print(\"bad month number\")\n", + " days=0\n", + "\n", + " #Result\n", + " if(days!=0):\n", + " print(\"Number of days is {0}\".format(days))\n", + " if(int(aMonth)==month.february):\n", + " print(\"...or 29 if it's a leap year\")\n", + "\n", + "\n", + "#Top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter month number:\n", + "Number of days is 31\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_16.ipynb b/Programming_in_C/Chapter_16.ipynb new file mode 100644 index 00000000..f6d06ea2 --- /dev/null +++ b/Programming_in_C/Chapter_16.ipynb @@ -0,0 +1,304 @@ +{ + "metadata": { + "name": "Chapter 16" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 16: Input and Output Operations in Python" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.1, Page number: 350" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.1.py\n", + "#Illustrating print function\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " c='X'\n", + " s=\"abcdefghijklmnopqrstuvwxyz\"\n", + " i=425\n", + " j=17\n", + " u=0xf179\n", + " l=75000\n", + " L=0x1234567812345678\n", + " f=12.978\n", + " d=-97.4583\n", + " cp=c\n", + " ip=i\n", + " \n", + "\n", + " #Print Integers\n", + " print(\"Integers:\\n\")\n", + " print(\"%i %o %x %u\" %(i,i,i,i))\n", + " print(\"%x %X %#x %#X\" %(i,i,i,i))\n", + " print(\"%+i % i %07i %.7i\" %(i,i,i, i))\n", + " print(\"%i %o %x %u\" %(j,j,j,j))\n", + " print(\"%i %o %x %u\" %(u,u,u,u))\n", + " print(\"%ld %lo %lx %lu\" %(l,l,l,l))\n", + " print(\"%li %lo %lx %lu\" %(L,L,L, L))\n", + "\n", + " #Print Floats & Doubles\n", + " print(\"\\nFloats and Doubles:\")\n", + " print(\"%f %e %g\"%( f, f, f))\n", + " print(\"%.2f %.2e\"%( f, f))\n", + " print(\"%.0f %.0e\"%( f, f))\n", + " print(\"%7.2f %7.2e\"%( f, f))\n", + " print(\"%f %e %g\"%( d, d, d))\n", + " print(\"%.*f\"%( 3, d))\n", + " print(\"%*.*f\" %(8, 2, d))\n", + "\n", + " #Print Characters\n", + " print(\"\\nCharacters:\")\n", + " print(\"%c\"%(c))\n", + " print(\"%3c%3c\"%( c, c))\n", + " print(\"{0:x} \".format(ord(c))) \n", + "\n", + " #Print Strings\n", + " print(\"\\nStrings:\")\n", + " print(\"%s\"%(s))\n", + " print(\"%.5s\"%(s))\n", + " print(\"%30s\"%(s))\n", + " print(\"%20.5s\"%(s))\n", + " print(\"%-20.5s\"%(s))\n", + "\n", + " #Print variables pointers\n", + " print(\"\\nPointers:\")\n", + " print(\"{0:x} {1:x}\\n\".format(int(ip),ord(cp)))\n", + "\n", + " print(\"This%n is fun.\")\n", + " print(\"c1 = %i, c2 = %i\\n\"%(4, 12))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integers:\n", + "\n", + "425 651 1a9 425\n", + "1a9 1A9 0x1a9 0X1A9\n", + "+425 425 0000425 0000425\n", + "17 21 11 17\n", + "61817 170571 f179 61817\n", + "75000 222370 124f8 75000\n", + "1311768465173141112 110642547402215053170 1234567812345678 1311768465173141112\n", + "\n", + "Floats and Doubles:\n", + "12.978000 1.297800e+01 12.978\n", + "12.98 1.30e+01\n", + "13 1e+01\n", + " 12.98 1.30e+01\n", + "-97.458300 -9.745830e+01 -97.4583\n", + "-97.458\n", + " -97.46\n", + "\n", + "Characters:\n", + "X\n", + " X X\n", + "58 \n", + "\n", + "Strings:\n", + "abcdefghijklmnopqrstuvwxyz\n", + "abcde\n", + " abcdefghijklmnopqrstuvwxyz\n", + " abcde\n", + "abcde \n", + "\n", + "Pointers:\n", + "1a9 58\n", + "\n", + "This%n is fun.\n", + "c1 = 4, c2 = 12\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.2, Page number: 362" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.2.py\n", + "#Copying Characters from Standard Input to Standard Output\n", + "\n", + "#Import system library\n", + "import sys\n", + "\n", + "#Main()\n", + "def main():\n", + " \n", + " while (True):\n", + " #c=raw_input() #User Input\n", + " c=\"this is a sample text\" #Dummy text\n", + " print(c); #Display Result\n", + " \n", + " #Un-comment this while executing from terminal/command line\n", + " #if(c==\"EOF\"): #Check Exit condition\n", + " sys.exit()\n", + " \n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SystemExit", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "this is a sample text\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D." + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 16.3, Page number: 366" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#16.3.py\n", + "#Program to copy one file to another\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration/ User Input\n", + " print(\"Enter name of the file to be copied: \")\n", + " inName=\"source.txt\"\n", + " #inName=raw_input()\n", + "\n", + " print(\"Enter name of the output file: \")\n", + " outName=\"target.txt\"\n", + " #outName=raw_input()\n", + "\n", + " \n", + "#Try to open a file for reading\n", + " try:\n", + " inn=open(inName,\"r\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for reading\".format(inName))\n", + " sys.exit()\n", + "\n", + "#try to open a file for writing\n", + " try:\n", + " out=open(outName,\"w\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for writing\".format(outName))\n", + " sys.exit()\n", + "\n", + " string=inn.read() #Read content from File-1\n", + " out.write(string) #Write content to File-2\n", + "\n", + "#Close Files\n", + " inn.close()\n", + " out.close()\n", + "\n", + " print(\"File has been copied.\\n\");\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "global name 'source' is not defined", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/jovina/TBC/nalin/Stephen G Kochan--Programming in C/<ipython-input-3-388aacca0b5d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 40\u001b[0m \u001b[1;31m#Setting top level conditional script\u001b[0m\n\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 41\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 42\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 43\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jovina/TBC/nalin/Stephen G Kochan--Programming in C/<ipython-input-3-388aacca0b5d>\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0minn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtxt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;31m# Exception:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"cant open {0} for reading\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtxt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 22\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: global name 'source' is not defined" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name of the file to be copied: \n", + "Enter name of the output file: \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_in_C/Chapter_17.ipynb b/Programming_in_C/Chapter_17.ipynb new file mode 100644 index 00000000..192aef85 --- /dev/null +++ b/Programming_in_C/Chapter_17.ipynb @@ -0,0 +1,110 @@ +{ + "metadata": { + "name": "Chapter XVII" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 17: Miscellaneous and Advanced Features" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 17.1, Page number: 382" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#17.1.py\n", + "#File Copy Program Using Command-Line Arguments\n", + "\n", + "#Import Libraries\n", + "import sys\n", + "\n", + "def main():\n", + " \n", + " #Un-Comment this while execution from terminal/Command Line\n", + " #if(len(sys.argv)!=3):\n", + " #print(\"need two file names!\")\n", + " #sys.exit()\n", + " \n", + "#Read Command Line Arguments\n", + " inName=\"source.txt\" #str(sys.argv[1])\n", + " outName=\"target.txt\" #str(sys.argv[2])\n", + "\n", + " #Try to open a file for reading\n", + " try:\n", + " inn=open(inName,\"r\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for reading\".format(inName))\n", + " sys.exit()\n", + "\n", + "#try to open a file for writing\n", + " try:\n", + " out=open(outName,\"w\") \n", + " except:# Exception:\n", + " print(\"cant open {0} for writing\".format(outName))\n", + " sys.exit()\n", + "\n", + " string=inn.read() #Read content from File-1\n", + " out.write(string) #Write content to File-2\n", + "\n", + "#Close Files\n", + " inn.close()\n", + " out.close()\n", + "\n", + "\n", + " print(\"File has been copied.\\n\");\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SystemExit", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[0;31mSystemExit\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "cant open source.txt for reading\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D." + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_18.ipynb b/Programming_in_C/Chapter_18.ipynb new file mode 100644 index 00000000..675ba691 --- /dev/null +++ b/Programming_in_C/Chapter_18.ipynb @@ -0,0 +1,472 @@ +{ + "metadata": { + "name": "Chapter 18" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 18: Debugging Programs" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 18.1, Page number: 389" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#18.1.py\n", + "#Adding Debug statements in program\n", + "\n", + "#Program logs will be saved in file- 18.1_logFile.txt\n", + "\n", + "#Import library\n", + "import logging\n", + "\n", + "#Function to return sum of 3 numbers\n", + "def process(i,j,k):\n", + " return i+j+k\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " logger = logging.getLogger()\n", + " logger.setLevel(logging.DEBUG)\n", + "\n", + " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n", + "\n", + " fh = logging.FileHandler('18.1_logFile.txt')\n", + " fh.setLevel(logging.DEBUG)\n", + " fh.setFormatter(formatter)\n", + " logger.addHandler(fh)\n", + "\n", + " ch = logging.StreamHandler()\n", + " ch.setLevel(logging.DEBUG)\n", + " ch.setFormatter(formatter)\n", + " logger.addHandler(ch)\n", + "\n", + " arr=[]\n", + " arr=map(int,\"1 2 3\".split())\n", + " #arr=map(int,raw_input().split())\n", + " logger.debug(\"Number of integers read= {0}\".format(len(arr))) #Debug statement\n", + " logger.debug(\"i = {0}, j = {1}, k = {2}\\n\".format(arr[0],arr[1],arr[2])) #Debug statement\n", + "\n", + " print(\"sum= %i\\n\"%process(arr[0],arr[1],arr[2]))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 01:20:39,300 - DEBUG - Number of integers read= 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 01:20:39,303 - DEBUG - i = 1, j = 2, k = 3\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum= 6\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 18.2, Page number: 391" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#18.2.py\n", + "#Compiling in Debug \n", + "\n", + "#Program logs will be saved in file- 18.2_logFile.txt\n", + "\n", + "#Import Libraries\n", + "import logging,sys\n", + "\n", + "#Function to return product of two numbers\n", + "def process(i1,i2):\n", + "\n", + " global logger \n", + " logger.debug(\"process (%i, %i)\\n\"%(i1, i2))\n", + " val=i1*i2\n", + " logger.debug(\"return %i\\n\"%(val))\n", + " return val\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " global logger\n", + " logging.basicConfig(filename='18.2_logFile.txt', filemode='w', level=logging.DEBUG)\n", + " logger = logging.getLogger()\n", + " logger.setLevel(logging.DEBUG)\n", + " filemode = 'w'\n", + " \n", + " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n", + "\n", + " fh = logging.FileHandler('18.2_logFile.txt')\n", + " fh.setLevel(logging.DEBUG)\n", + " fh.setFormatter(formatter)\n", + " logger.addHandler(fh)\n", + "\n", + " ch = logging.StreamHandler()\n", + " ch.setLevel(logging.DEBUG)\n", + " ch.setFormatter(formatter)\n", + " logger.addHandler(ch)\n", + "\n", + "\n", + " arg1=0\n", + " arg2=0\n", + " #Un-Comment this while executing from terminal/command line\n", + " #if(len(sys.argv)>1):\n", + " #arg1=int(sys.argv[1])\n", + " #if(len(sys.argv)==3):\n", + " #arg2=int(sys.argv[2])\n", + "\n", + " #Dummy command-line-parameters\n", + " #execute as \"python <filename>.py arg1 arg2\" while executing from terminal and un/comment respective statements\n", + " a=['3','5'] \n", + " logger.debug(\"processed %i arguments\\n\"%(len(a)))\n", + " #logger.debug(\"processed %i arguments\\n\"%(len(sys.argv)))\n", + "\n", + " logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(int(a[0]), int(a[1])))\n", + " print(\"product = %i\\n\" %( process (int(a[0]),int( a[1]))))\n", + "\n", + " #logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(arg1, arg2))\n", + " #print(\"product = %i\\n\" %( process (arg1, arg2)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:50:53,392 - DEBUG - processed 2 arguments\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:50:53,397 - DEBUG - arg1 = 3, arg2 = 5\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:50:53,404 - DEBUG - process (3, 5)\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:50:53,409 - DEBUG - return 15\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "product = 15\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 18.3, Page number: 393" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#18.3.py\n", + "#Compiling in Debug--version2\n", + "\n", + "#Program logs will be saved in file- 18.3_logFile.txt\n", + "\n", + "#Using different logging levels\n", + "#DEBUG Detailed information, typically of interest only when diagnosing problems.\n", + "#INFO Confirmation that things are working as expected.\n", + "#WARNING An indication of some problem in the near future (e.g.'disk space low')\n", + "#ERROR Due to a more serious problem, the software has not been able to perform some function.\n", + "#CRITICAL A serious error, indicating that the program itself may be unable to continue running.\n", + "\n", + "\n", + "#Import Libraries\n", + "import logging,sys\n", + "\n", + "#Function to return product of two numbers\n", + "def process(i1,i2):\n", + "\n", + " global logger \n", + " logger.warning(\"process (%i, %i)\\n\"%(i1, i2))\n", + " val=i1*i2\n", + " logger.error(\"return %i\\n\"%(val))\n", + " return val\n", + "\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " global logger\n", + " logging.basicConfig(filename='18.3_logFile.txt', filemode='w', level=logging.DEBUG)\n", + " logger = logging.getLogger()\n", + " logger.setLevel(logging.DEBUG)\n", + "\n", + " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n", + "\n", + " fh = logging.FileHandler('18.3_logFile.txt')\n", + " fh.setLevel(logging.DEBUG)\n", + " fh.setFormatter(formatter)\n", + " logger.addHandler(fh)\n", + "\n", + " ch = logging.StreamHandler()\n", + " ch.setLevel(logging.DEBUG)\n", + " ch.setFormatter(formatter)\n", + " logger.addHandler(ch)\n", + "\n", + "\n", + " arg1=0\n", + " arg2=0\n", + " #Un-Comment this while executing from terminal/command line\n", + "\n", + " #if(len(sys.argv)>1):\n", + " #arg1=int(sys.argv[1])\n", + " #if(len(sys.argv)==3):\n", + " #arg2=int(sys.argv[2])\n", + "\n", + " #Dummy command-line-parameters\n", + " #execute as \"python <filename>.py arg1 arg2\" while executing from terminal and un/comment respective statements\n", + " a=['3','5']\n", + " logger.debug(\"processed %i arguments\\n\"%(len(a)))\n", + " #logger.debug(\"processed %i arguments\\n\"%(len(sys.argv)))\n", + "\n", + " logger.info(\"arg1 = %i, arg2 = %i\\n\"%(int(a[0]), int(a[1])))\n", + " print(\"product = %i\\n\" %( process (int(a[0]),int( a[1]))))\n", + "\n", + " #logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(arg1, arg2))\n", + " #print(\"product = %i\\n\" %( process (arg1, arg2)))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:54:41,136 - DEBUG - processed 2 arguments\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:54:41,142 - INFO - arg1 = 3, arg2 = 5\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:54:41,149 - WARNING - process (3, 5)\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "2013-08-27 17:54:41,154 - ERROR - return 15\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "product = 15\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 18.4, Page number: 396" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#18.4.py\n", + "#A simple program for use with pdb(gdb like python debugger)\n", + "\n", + "#Main()\n", + "def main():\n", + " \n", + " #Variable Declaration\n", + " sum=0\n", + " data = [1, 2, 3, 4, 5]\n", + "\n", + " #Calculation\n", + " for i in range (0,5):\n", + " sum += data[i]\n", + " \n", + " #Result\n", + " print(\"sum = %i\\n\"%(sum))\n", + " \n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum = 15\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 18.5, Page number: 401" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#18.5.py\n", + "#Working more with pdb\n", + "\n", + "#Class Declaration\n", + "class date:\n", + " def __init__(self,m,d,y): #Class constructor\n", + " self.month=m\n", + " self.day=d\n", + " self.year=y\n", + "\n", + " def foo(x):\n", + " x.day+=1\n", + " return x\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Variable Declaration\n", + " today=date(10,11,2004)\n", + " array=[1,2,3,4,5]\n", + " string=\"test string\"\n", + " i=3\n", + " newdate=date(11,15,2004)\n", + " today=date.foo(today)\n", + "\n", + " #Result\n", + " print(\"today= %d/%d/%d\"%(today.day,today.month,today.year))\n", + "\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "today= 12/10/2004\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/Chapter_19.ipynb b/Programming_in_C/Chapter_19.ipynb new file mode 100644 index 00000000..d92a3f41 --- /dev/null +++ b/Programming_in_C/Chapter_19.ipynb @@ -0,0 +1,70 @@ +{ + "metadata": { + "name": "Chapter XIX" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 19: Object-oriented programming" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 19.1, Page number: 414" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#19.1.py\n", + "#Working with fractions in python\n", + "\n", + "#Class declaration\n", + "class Fraction:\n", + " def __init__(self): #Class constructor\n", + " self.numerator=0\n", + " self.denominator=0\n", + "\n", + "#Main()\n", + "def main():\n", + "\n", + " #Creating instance\n", + " myFract=Fraction()\n", + " myFract.numerator=1\n", + " myFract.denominator=3\n", + "\n", + " print(\"The Fraction is {0}/{1}\".format(myFract.numerator,myFract.denominator))\n", + "\n", + "#Setting top level conditional script\n", + "if __name__=='__main__':\n", + " main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Fraction is 1/3\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Programming_in_C/README.txt b/Programming_in_C/README.txt new file mode 100644 index 00000000..0f721b5b --- /dev/null +++ b/Programming_in_C/README.txt @@ -0,0 +1,10 @@ +Contributed By: Nalin Chibber +Course: btech +College/Institute/Organization: MSIT - Indraprastha University, New Delhi +Department/Designation: Computer Science engineering +Book Title: Programming in C +Author: Stephen G. Kochan +Publisher: Sams Publishing +Year of publication: 2005 +Isbn: 0-672-32666-3 +Edition: 3rd
\ No newline at end of file diff --git a/Programming_in_C/screenshots/nalin-1.png b/Programming_in_C/screenshots/nalin-1.png Binary files differnew file mode 100644 index 00000000..83b0e0a3 --- /dev/null +++ b/Programming_in_C/screenshots/nalin-1.png diff --git a/Programming_in_C/screenshots/nalin-2.png b/Programming_in_C/screenshots/nalin-2.png Binary files differnew file mode 100644 index 00000000..44dceb57 --- /dev/null +++ b/Programming_in_C/screenshots/nalin-2.png diff --git a/Programming_in_C/screenshots/nalin-3.png b/Programming_in_C/screenshots/nalin-3.png Binary files differnew file mode 100644 index 00000000..74db9f60 --- /dev/null +++ b/Programming_in_C/screenshots/nalin-3.png |