summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_15_1.ipynb
blob: 34a86a6756d224a056cddbb67523a441a3deb039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
 "metadata": {
  "name": "",
  "signature": "sha256:23b3891085deb8af52412d44380b6187a4110d7e026531ec90b3b2ae79d61d0f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 15: Debugging and optimization"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.1, Page number: 275"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.6, Page number: 288"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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]\n",
        "Threes 5 Sevens 0\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.7, Page number: 292"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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": [
      {
       "ename": "IOError",
       "evalue": "[Errno 2] No such file or directory: 'numbers.dat'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-5-ce867c3a4ec5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0min_file\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'numbers.dat'\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[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'numbers.dat'"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.8, Page number: 300"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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": [
      {
       "ename": "IOError",
       "evalue": "[Errno 2] No such file or directory: 'numbers.dat'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-6-8cd3b79f72a3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0min_file\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'numbers.dat'\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[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'numbers.dat'"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.10, Page number: 304"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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": [
      {
       "ename": "ZeroDivisionError",
       "evalue": "integer division or modulo by zero",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-7-289ff2a55d4d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'Starting\\n'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'Before divide...'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'After\\n'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Starting\n",
        "\n",
        "Before divide...\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 15.11, Page number: 304"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "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"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Before divide...\n"
       ]
      },
      {
       "ename": "ZeroDivisionError",
       "evalue": "integer division or modulo by zero",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-8-eedd2fda3e82>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'Before divide...'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[0mi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     16\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'After\\n'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
       ]
      }
     ],
     "prompt_number": 8
    }
   ],
   "metadata": {}
  }
 ]
}