summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter9.ipynb
blob: bc87a6c707b3f1f9ff8a4c65755dd47d87fa7172 (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
{
 "metadata": {
  "name": "",
  "signature": "sha256:6212a1681da4032325cbede0a51614855911a11a6baad9f5e6fd0a0ecd4b5da9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9: Functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example average.c, Page 185"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def average(a,b): #function to calculate average\n",
      "    return (a+b)/2\n",
      "nums = raw_input(\"Enter three numbers: \") #input numbers from user\n",
      "list1 = map(float, nums.split())  \n",
      "x=list1[0]\n",
      "y=list1[1]\n",
      "z=list1[2]\n",
      "print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n",
      "print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n",
      "print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter three numbers: 3.5 9.6 10.2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Average of 3.5 and 9.6: 6.55\n",
        "Average of 9.6 and 10.2: 9.90\n",
        "Average of 3.5 and 10.2: 6.85\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example countdown.c, Page 186"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def print_count(n): #function definition\n",
      "    print \"T minus %d and counting\" % n\n",
      "for i in range (10,0,-1):\n",
      "    print_count(i) #print using function\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "T minus 10 and counting\n",
        "T minus 9 and counting\n",
        "T minus 8 and counting\n",
        "T minus 7 and counting\n",
        "T minus 6 and counting\n",
        "T minus 5 and counting\n",
        "T minus 4 and counting\n",
        "T minus 3 and counting\n",
        "T minus 2 and counting\n",
        "T minus 1 and counting\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example pun2.c, Page 187"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def print_pun(): #function definition\n",
      "    print \"To C, or not to C: that is the question.\"\n",
      "print_pun() #function call"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "To C, or not to C: that is the question.\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example prime.c, Page 190"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def is_prime(n): #function to check for prime number\n",
      "    if (n<=1):\n",
      "        return False\n",
      "    divisor=2\n",
      "    while(divisor*divisor<=n):\n",
      "        if(n%divisor==0):\n",
      "            return False\n",
      "    return True\n",
      "n=int(raw_input(\"Enter a number: \")) #input number\n",
      "if(is_prime(n)): #check if prime using function\n",
      "    print \"Prime\" #print result of check\n",
      "else:\n",
      "    print \"Not prime\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number: 34\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Not prime\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example on Page 192"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    nums = raw_input(\"Enter three numbers: \") #input numbers from user\n",
      "    list1 = map(float, nums.split())  \n",
      "    x=list1[0]\n",
      "    y=list1[1]\n",
      "    z=list1[2]\n",
      "    \n",
      "    #function usage before definition\n",
      "    \n",
      "    print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n",
      "    print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n",
      "    print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n",
      "    \n",
      "if __name__==\"__main__\":\n",
      "    main()\n",
      "    \n",
      "def average(a,b): #function to calculate average\n",
      "    return (a+b)/2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter three numbers: 3.5 9.6 10.2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Average of 3.5 and 9.6: 6.55\n",
        "Average of 9.6 and 10.2: 9.90\n",
        "Average of 3.5 and 10.2: 6.85\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example on Page 195"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x=3.0\n",
      "print \"Square: %d\" % square(x) #gives error since function prototype doesnt exist before\n",
      "def square(n):\n",
      "    return n*n\n",
      "\n",
      "\"\"\"\n",
      "def main():\n",
      "    x=3.0\n",
      "    print \"Square: %d\" % square(x) \n",
      "    \n",
      "#Now wouldn't give an error since writing the line below loads up all functions before starting main()\n",
      "\n",
      "if __name__==\"__main__\":\n",
      "    main()\n",
      "    \n",
      "def square(n):\n",
      "    return n*n\n",
      "\"\"\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'square' 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<ipython-input-3-2c761040ea35>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Square: %d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#gives error since function prototype doesnt exist before\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mNameError\u001b[0m: name 'square' is not defined"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example on Page 197"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def store_zeroes(a,n):\n",
      "    for i in range(0,n):\n",
      "        a[i]=0\n",
      "b=[None]*200\n",
      "store_zeroes(b,100) #First 100 elements of b are now 100.\n",
      "print b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example qsort.c, Page 207"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N=10 #number of elements to sort\n",
      "def split(a,low,high): #function to split list while sorting\n",
      "    part_element=a[low]\n",
      "    while(1):\n",
      "        while(low<high and part_element<=a[high]):\n",
      "            high=high-1\n",
      "        if(low>=high):\n",
      "            break\n",
      "        a[low+1]=a[high]\n",
      "        \n",
      "        while(low<high and a[low]<=part_element):\n",
      "            low=low+1\n",
      "        if(low>=high):\n",
      "            break\n",
      "        a[high-1]=a[low]\n",
      "        \n",
      "    a[high]=part_element\n",
      "    return high\n",
      "        \n",
      "def quicksort(a,low,high): #recursive function for quicksort\n",
      "    if(low>=high):\n",
      "        return\n",
      "    middle=split(a,low,high)\n",
      "    quicksort(a,low,(middle-1))\n",
      "    quicksort(a,(middle+1),high)\n",
      "    \n",
      "nums = raw_input(\"Enter %d numbers to be sorted: \"%N) #input numbers to sort\n",
      "a = map(int, nums.split())\n",
      "a.sort()\n",
      "quicksort(a,0,(N-1))       #call quicksort function\n",
      "print \"In sorted order: \", #print sorted result\n",
      "for i in a:\n",
      "    print i,\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter 10 numbers to be sorted: 9 16 47 82 4 66 12 3 25 51\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "In sorted order:  3 4 9 12 16 25 47 51 66 82\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}