summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/chapter9.ipynb
blob: ae4771e3145c83fbf3bcc8f08e6ebc2f369c4a55 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
{
 "metadata": {
  "name": "",
  "signature": "sha256:13a3347e13ed5ee09ae2833e49a517873801778756103a4da905a96a5997fa89"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9, User-Defined Functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-1 , Page number: 187"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Using a function as an abbreviation\n",
      "\n",
      "# In python, the user defined functions have to defined before making a call to them\n",
      "\n",
      "def chorus() :\n",
      "\tprint \"%60s\"%\"With a knick-knack-paddy-whack,\"\n",
      "\tprint \"%60s\"%\"Give the dog a bone,\"\n",
      "\tprint \"%60s\\n\"%\"This old man went rolling home.\"\n",
      "\n",
      "print \"This old man, he played two,\"\n",
      "print \"He played knick-knack on my thumb,\"\n",
      "chorus()\n",
      "\n",
      "print \"This old man, he played one,\"\n",
      "print \"He played knick-knack on my shoe,\"\n",
      "chorus()\n",
      "\n",
      "print \"This old man, he played three,\"\n",
      "print \"He played knick-knack on my knee,\"\n",
      "chorus()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This old man, he played two,\n",
        "He played knick-knack on my thumb,\n",
        "                             With a knick-knack-paddy-whack,\n",
        "                                        Give the dog a bone,\n",
        "                             This old man went rolling home.\n",
        "\n",
        "This old man, he played one,\n",
        "He played knick-knack on my shoe,\n",
        "                             With a knick-knack-paddy-whack,\n",
        "                                        Give the dog a bone,\n",
        "                             This old man went rolling home.\n",
        "\n",
        "This old man, he played three,\n",
        "He played knick-knack on my knee,\n",
        "                             With a knick-knack-paddy-whack,\n",
        "                                        Give the dog a bone,\n",
        "                             This old man went rolling home.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-2 , Page number: 189"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Demonstration of one use of a function\n",
      "\n",
      "def get_an_int() :\n",
      "\t\"\"\"RETURNS AN INTEGER\"\"\"\t\t# This statement is just a description of the function (specific feature of python)\n",
      "\treturn 5\n",
      "\n",
      "print \"Enter the length of a rectangle:\",\n",
      "length = get_an_int()\n",
      "print length\n",
      "\n",
      "print \"Enter the width:\",\n",
      "width = get_an_int()\n",
      "print width\n",
      "\n",
      "print \"\\nThe area is %d\"%(length*width)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the length of a rectangle: 5\n",
        "Enter the width: 5\n",
        "\n",
        "The area is 25\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-3 , Page number: 191"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# An attempt at communication between functions\n",
      "\n",
      "def modify_i() :\n",
      "\ti = 3\n",
      "\t\n",
      "i = 1\n",
      "print \"i is now equal to %d\"%i\n",
      "\n",
      "modify_i()\n",
      "print \"i is now equal to %d\"%i\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "i is now equal to 1\n",
        "i is now equal to 1\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-4 , Page number: 192"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Demonstration of the use of a global variable\n",
      "\n",
      "# Making variable global\n",
      "\n",
      "def get_an_int() :\n",
      "\treturn 5\n",
      "\n",
      "def calc_area() :\n",
      "\tarea = length*width\n",
      "\n",
      "globals()\t\t\t# The command simply makes all the variables declared so far as global variables\n",
      "print \"Enter the length of a rectangle:\",\n",
      "length = get_an_int()\n",
      "print length\n",
      "\n",
      "print \"Enter the width:\",\n",
      "width = get_an_int()\n",
      "print width\n",
      "\n",
      "calc_area()\n",
      "print \"\\nThe area is %d\"%(length*width)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the length of a rectangle: 5\n",
        "Enter the width: 5\n",
        "\n",
        "The area is 25\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-5 , Page number: 194"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Introduction to parameters\n",
      "\n",
      "def print_an_int(i) :\n",
      "\t\"\"\"The function prints an integer passed as a parameter\"\"\"\n",
      "\tprint i\n",
      "\n",
      "\n",
      "n = 3\n",
      "\n",
      "print \"Passing a constant:\",\n",
      "print_an_int(5)\n",
      "\n",
      "print \"\\n\\nPassing a variable's value:\",\n",
      "print_an_int(n)\n",
      "\n",
      "print \"\\n\\nPassing the value of an expression:\",\n",
      "print_an_int(n * 4 + 1)\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Passing a constant: 5\n",
        "\n",
        "\n",
        "Passing a variable's value: 3\n",
        "\n",
        "\n",
        "Passing the value of an expression: 13\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-6 , Page number: 196"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Demonstration of how a procedure cannot modify the contents of a variable passed as a parameter\n",
      "\n",
      "def modify(i) :\n",
      "\ti = 3\n",
      "\n",
      "i = 1\n",
      "\n",
      "print \"i is now equal to %d\"%i\n",
      "\n",
      "modify(i)\n",
      "\n",
      "print \"i is now equal to %d\"%i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "i is now equal to 1\n",
        "i is now equal to 1\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-7 , Page number: 197 "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Hailstones, demonstrating the use of a parameter\n",
      "\n",
      "def step(i) :\n",
      "\t\"\"\"This function returns its parameter i divided by 2 if i is even else it returns i multiplied by 3 and incremented by 1\"\"\"\n",
      "\tif (i % 2) :\n",
      "\t\treturn (i * 3 + 1)\n",
      "\telse : \n",
      "\t\treturn (i / 2)\n",
      "\n",
      "counter = 0\n",
      "\n",
      "print \"Enter a positive integer\",\n",
      "n = 25\n",
      "print n\n",
      "\n",
      "nsave = n\n",
      "\n",
      "while n > 1 :\n",
      "\tn = step(n)\n",
      "\tcounter += 1\n",
      "\tprint \"Step %4d: n = %4d\"%(counter,n)\n",
      "\n",
      "print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a positive integer 25\n",
        "Step    1: n =   76\n",
        "Step    2: n =   38\n",
        "Step    3: n =   19\n",
        "Step    4: n =   58\n",
        "Step    5: n =   29\n",
        "Step    6: n =   88\n",
        "Step    7: n =   44\n",
        "Step    8: n =   22\n",
        "Step    9: n =   11\n",
        "Step   10: n =   34\n",
        "Step   11: n =   17\n",
        "Step   12: n =   52\n",
        "Step   13: n =   26\n",
        "Step   14: n =   13\n",
        "Step   15: n =   40\n",
        "Step   16: n =   20\n",
        "Step   17: n =   10\n",
        "Step   18: n =    5\n",
        "Step   19: n =   16\n",
        "Step   20: n =    8\n",
        "Step   21: n =    4\n",
        "Step   22: n =    2\n",
        "Step   23: n =    1\n",
        "\n",
        "\n",
        "25 went to 1 in 23 steps.\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-8 , Page number: 200"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Square root program demonstrating a non-integer function and parameter\n",
      "\n",
      "def EPSILON() :\n",
      "\t\"\"\"replacement of #define constant\"\"\"\n",
      "\treturn 0.0001\n",
      "\n",
      "def sqrt(n) :\n",
      "\t\"\"\"returns the square root of the number supplied as parameter using Newton-Raphson method\"\"\"\n",
      "\tif n > 0.0 :\n",
      "\t\t# The for loop statement given in the example(from the book) is not possible to code in python\n",
      "\t\t# So, breaking it down to while loop\n",
      "\t\tguess = n / 2.0\n",
      "\t\twhile (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :\n",
      "\t\t\tguess = (guess + n/guess) / 2.0\n",
      "\t\treturn guess\n",
      "\telse :\n",
      "\t\treturn (-1.0)\n",
      "\n",
      "print \"Enter the number whose square root you want:\",\n",
      "x = 17.1\n",
      "print x\n",
      "\n",
      "print \"The square root of %f is %f\"%(x,sqrt(x))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number whose square root you want: 17.1\n",
        "The square root of 17.100000 is 4.135215\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-9 , Page number: 201"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Testing for leap years using a boolean function\n",
      "\n",
      "def is_leap(year) :\n",
      "\t\"\"\"A function that returns true if the given year is a leap year else returns false\"\"\"\n",
      "\treturn (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)\n",
      "\n",
      "print \"Please enter a year:\",\n",
      "year = 2000\n",
      "print year\n",
      "\n",
      "if (is_leap(year)) :\n",
      "\tprint \"%d is a leap year.\"%year\n",
      "else :\n",
      "\tprint \"%d is not a leap year.\"%year"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Please enter a year: 2000\n",
        "2000 is a leap year.\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-10 , Page number: 203"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Demonstration of the use of multiple parameters\n",
      "\n",
      "def get_an_int() :\n",
      "\t\"\"\"returns a random integer\"\"\"\n",
      "\treturn 7\n",
      "\n",
      "def area(length,width) :\n",
      "\t\"\"\"returns the area of the rectangle using its parameters length and width\"\"\"\n",
      "\treturn length * width\n",
      "\n",
      "print \"Enter the length of a rectangle:\",\n",
      "length = get_an_int()\n",
      "print length\n",
      "\n",
      "print \"Enter the width:\",\n",
      "width = get_an_int()\n",
      "print width\n",
      "\n",
      "print \"\\nThe area is %d.\"%area(length,width)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the length of a rectangle: 7\n",
        "Enter the width: 7\n",
        "\n",
        "The area is 49.\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 9-11 , Page number: 204"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Illustration of a 2-parameter function: raising a number to a power \n",
      "\n",
      "def power(i,x) :\n",
      "\t\"\"\"returns the values of i to the power of x\"\"\"\n",
      "\taccum = 1\n",
      "\tif x < 0 :\n",
      "\t\treturn 0\n",
      "\tfor index in range (0,x,1) :\n",
      "\t\taccum *= i\n",
      "\treturn accum\n",
      "\n",
      "answer = \"y\"\n",
      "\n",
      "while answer == \"y\" or answer == \"Y\" :\n",
      "\tprint \"\\nPlease enter an integer:\",\n",
      "\tnumber = 4\n",
      "\tprint number\n",
      "\n",
      "\tprint \"Please enter a non-negative integral power\"\n",
      "\tprint \"     to which the first number is to be raised\",\n",
      "\texponent = 2\n",
      "\tprint exponent\n",
      "\n",
      "\twhile exponent < 0 :\n",
      "\t\tprint \"***\\7Please make that a non-negative integer\"\n",
      "\t\texponent = 2 \t\t\t\t# randomly making the exponent non-negative\n",
      "\n",
      "\tprint \"%d raised to the power %d is %ld.\"%(number,exponent,power(number,exponent))\n",
      "\n",
      "\t# see if the user wants to keep going\n",
      "\n",
      "\tprint \"\\nAnother calculation? (y/n)\",\n",
      "\tanswer = \"n\"\n",
      "\tprint answer"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Please enter an integer: 4\n",
        "Please enter a non-negative integral power\n",
        "     to which the first number is to be raised 2\n",
        "4 raised to the power 2 is 16.\n",
        "\n",
        "Another calculation? (y/n) n\n"
       ]
      }
     ],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}