diff options
Diffstat (limited to 'Practical_C_Programming/Chapter_15_1.ipynb')
-rw-r--r-- | Practical_C_Programming/Chapter_15_1.ipynb | 232 |
1 files changed, 213 insertions, 19 deletions
diff --git a/Practical_C_Programming/Chapter_15_1.ipynb b/Practical_C_Programming/Chapter_15_1.ipynb index 72f7aaee..34a86a67 100644 --- a/Practical_C_Programming/Chapter_15_1.ipynb +++ b/Practical_C_Programming/Chapter_15_1.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 15" + "name": "", + "signature": "sha256:23b3891085deb8af52412d44380b6187a4110d7e026531ec90b3b2ae79d61d0f" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,25 +12,53 @@ "cell_type": "heading", "level": 1, "metadata": {}, - "source": "Chapter 15: Debugging and optimization" + "source": [ + "Chapter 15: Debugging and optimization" + ] }, { "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.1, Page number: 275" + "source": [ + "Example 15.1, Page number: 275" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.1.py\n# A database program to look up names in a hardcoded list\n\n\n# Function declaration\ndef lookup (name) :\n lists = ['John', 'Jim', 'Jane', 'Clyde']\n result = 0\n\n for index in range (0, 4) :\n if (lists[index] == name) :\n result = 1\n break\n \n return result\n\n# Calculation and result\nname = 'John'\n\nif (lookup (name)) :\n print ('%s is in the list\\n' % name)\nelse :\n print ('%s is not in the list\\n' % name)", + "input": [ + "\n", + "# Function declaration\n", + "def lookup (name) :\n", + " lists = ['John', 'Jim', 'Jane', 'Clyde']\n", + " result = 0\n", + "\n", + " for index in range (0, 4) :\n", + " if (lists[index] == name) :\n", + " result = 1\n", + " break\n", + " \n", + " return result\n", + "\n", + "# Calculation and result\n", + "name = 'John'\n", + "\n", + "if (lookup (name)) :\n", + " print ('%s is in the list\\n' % name)\n", + "else :\n", + " print ('%s is not in the list\\n' % name)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "John is in the list\n\n" + "text": [ + "John is in the list\n", + "\n" + ] } ], "prompt_number": 2 @@ -38,19 +67,48 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.6, Page number: 288" + "source": [ + "Example 15.6, Page number: 288" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.6.py\n# To count the number of 3's and 7's in an array\n\n\n# Variable and function declaration\nseven_count = 0\nthree_count = 0\ndata = []\n\ndef get_data (data) :\n for i in range (0, 5) :\n x = 3\n data.append(int(x))\n print (data)\n\n# Calculation\nget_data (data)\nfor index in range (0, 5) :\n if data[index] == 3 :\n three_count += 1\n\n if data[index] == 7 :\n seven_count += 1\n\n# Result\nprint ('Threes %d Sevens %d' % (three_count, seven_count))", + "input": [ + "\n", + "# Variable and function declaration\n", + "seven_count = 0\n", + "three_count = 0\n", + "data = []\n", + "\n", + "def get_data (data) :\n", + " for i in range (0, 5) :\n", + " x = 3\n", + " data.append(int(x))\n", + " print (data)\n", + "\n", + "# Calculation\n", + "get_data (data)\n", + "for index in range (0, 5) :\n", + " if data[index] == 3 :\n", + " three_count += 1\n", + "\n", + " if data[index] == 7 :\n", + " seven_count += 1\n", + "\n", + "# Result\n", + "print ('Threes %d Sevens %d' % (three_count, seven_count))" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "[3, 3, 3, 3, 3]\nThrees 5 Sevens 0\n" + "text": [ + "[3, 3, 3, 3, 3]\n", + "Threes 5 Sevens 0\n" + ] } ], "prompt_number": 4 @@ -59,12 +117,60 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.7, Page number: 292" + "source": [ + "Example 15.7, Page number: 292" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.7.py\n# To perform binary search on a number in a file 'numbers.dat'\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('numbers.dat', 'r')\n\ndata = []\nmax_count = 0\n \n# Calculation and result\nif not os.path.exists ('numbers.dat') :\n print ('Error:Unable to open numbers.dat\\n')\n sys.exit(1)\n\nfor line in in_file :\n data.append(line)\n max_count += 1\n\nwhile (1) :\n search = 6\n \n if (search == -1) :\n break\n\n low = 0\n high = max_count\n\n while (1) :\n mid = (low + high) / 2\n middle = int (mid) \n\n if (int (data[middle]) == search) :\n print ('Found at index %d\\n' % (middle + 1))\n break\n\n if (low == high) :\n print ('Not found\\n')\n break\n\n if (int (data[middle]) < search) :\n low = middle\n else :\n high = middle \n\nin_file.close()", + "input": [ + "\n", + "# Variable declaration\n", + "import sys\n", + "import os\n", + "in_file = open ('numbers.dat', 'r')\n", + "\n", + "data = []\n", + "max_count = 0\n", + " \n", + "# Calculation and result\n", + "if not os.path.exists ('numbers.dat') :\n", + " print ('Error:Unable to open numbers.dat\\n')\n", + " sys.exit(1)\n", + "\n", + "for line in in_file :\n", + " data.append(line)\n", + " max_count += 1\n", + "\n", + "while (1) :\n", + " search = 6\n", + " \n", + " if (search == -1) :\n", + " break\n", + "\n", + " low = 0\n", + " high = max_count\n", + "\n", + " while (1) :\n", + " mid = (low + high) / 2\n", + " middle = int (mid) \n", + "\n", + " if (int (data[middle]) == search) :\n", + " print ('Found at index %d\\n' % (middle + 1))\n", + " break\n", + "\n", + " if (low == high) :\n", + " print ('Not found\\n')\n", + " break\n", + "\n", + " if (int (data[middle]) < search) :\n", + " low = middle\n", + " else :\n", + " high = middle \n", + "\n", + "in_file.close()" + ], "language": "python", "metadata": {}, "outputs": [ @@ -85,12 +191,61 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.8, Page number: 300" + "source": [ + "Example 15.8, Page number: 300" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.8.py\n# To perform binary search on a number in a file 'numbers.dat'\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('numbers.dat', 'r')\n\ndata = []\nmax_count = 0\n \n# Calculation and result\nif not os.path.exists ('numbers.dat') :\n print ('Error:Unable to open numbers.dat\\n')\n sys.exit(1)\n\nfor line in in_file :\n data.append(line)\n max_count += 1\n\nwhile (1) :\n search = 14\n \n if (search == -1) :\n break\n\n low = 0\n high = max_count\n\n while (1) :\n mid = (low + high) / 2\n middle = int (mid) \n\n if (int (data[middle]) == search) :\n print ('Found at index %d\\n' % (middle + 1))\n break\n\n if (low == high) :\n print ('Not found\\n')\n break\n\n if (int (data[middle]) < search) :\n low = middle\n else :\n high = middle \n\nin_file.close()", + "input": [ + "\n", + "\n", + "# Variable declaration\n", + "import sys\n", + "import os\n", + "in_file = open ('numbers.dat', 'r')\n", + "\n", + "data = []\n", + "max_count = 0\n", + " \n", + "# Calculation and result\n", + "if not os.path.exists ('numbers.dat') :\n", + " print ('Error:Unable to open numbers.dat\\n')\n", + " sys.exit(1)\n", + "\n", + "for line in in_file :\n", + " data.append(line)\n", + " max_count += 1\n", + "\n", + "while (1) :\n", + " search = 14\n", + " \n", + " if (search == -1) :\n", + " break\n", + "\n", + " low = 0\n", + " high = max_count\n", + "\n", + " while (1) :\n", + " mid = (low + high) / 2\n", + " middle = int (mid) \n", + "\n", + " if (int (data[middle]) == search) :\n", + " print ('Found at index %d\\n' % (middle + 1))\n", + " break\n", + "\n", + " if (low == high) :\n", + " print ('Not found\\n')\n", + " break\n", + "\n", + " if (int (data[middle]) < search) :\n", + " low = middle\n", + " else :\n", + " high = middle \n", + "\n", + "in_file.close()" + ], "language": "python", "metadata": {}, "outputs": [ @@ -111,12 +266,25 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.10, Page number: 304" + "source": [ + "Example 15.10, Page number: 304" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.10.py\n# To illustrate divide by zero error\n\n\n# Variable declaration\ni = 1\nj = 0\n\n# Calculation and result\nprint ('Starting\\n')\nprint ('Before divide...')\ni = i / j\nprint ('After\\n')", + "input": [ + "\n", + "# Variable declaration\n", + "i = 1\n", + "j = 0\n", + "\n", + "# Calculation and result\n", + "print ('Starting\\n')\n", + "print ('Before divide...')\n", + "i = i / j\n", + "print ('After\\n')" + ], "language": "python", "metadata": {}, "outputs": [ @@ -133,7 +301,11 @@ { "output_type": "stream", "stream": "stdout", - "text": "Starting\n\nBefore divide...\n" + "text": [ + "Starting\n", + "\n", + "Before divide...\n" + ] } ], "prompt_number": 7 @@ -142,24 +314,46 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 15.11, Page number: 304" + "source": [ + "Example 15.11, Page number: 304" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 15.11.py\n# To illustrate divide by zero error with flush() function\n\n\n# Variable declaration\nimport sys\ni = 1\nj = 0\n\n# Calculation and result\nprint ('Starting\\n')\nsys.stdout.flush()\nprint ('Before divide...')\nsys.stdout.flush()\ni = i / j\nprint ('After\\n')\nsys.stdout.flush()", + "input": [ + "\n", + "# Variable declaration\n", + "import sys\n", + "i = 1\n", + "j = 0\n", + "\n", + "# Calculation and result\n", + "print ('Starting\\n')\n", + "sys.stdout.flush()\n", + "print ('Before divide...')\n", + "sys.stdout.flush()\n", + "i = i / j\n", + "print ('After\\n')\n", + "sys.stdout.flush()" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Starting\n\n" + "text": [ + "Starting\n", + "\n" + ] }, { "output_type": "stream", "stream": "stdout", - "text": "Before divide...\n" + "text": [ + "Before divide...\n" + ] }, { "ename": "ZeroDivisionError", |