summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/Chapter11.ipynb
blob: 241e1c1e45fd803c3a99e53f5a71a75aa75d7657 (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
{
 "metadata": {
  "name": "",
  "signature": "sha256:3a9b6dffdf9d20cf5158c028c4012a0a2cf4ee76994f18e9b81499931df7cb5d"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 11, Pointers and Indirection"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-1, Page Number : 278"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Introduction to pointers\n",
      "\n",
      "# There is no concept of pointers in python\n",
      "i = 3\n",
      "\n",
      "j = id(i)\t# id() returns the address of the variable supplied\n",
      "j = i \t\t# assign value of i to j such that it seems like j points to i\n",
      "k = j\n",
      "j = 4\n",
      "\n",
      "print \"i = %d, *j = %d, k = %d\"%(i,j,k)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "i = 3, *j = 4, k = 3\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-2, Page Number: 280"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Demonstration of passing by reference for a function to be able to modify its parameters\n",
      "\n",
      "def modify(i) :\n",
      "\ti = 3\n",
      "\treturn i\n",
      "\n",
      "i = 1\n",
      "print \"i is now equal to %d\"%i\n",
      "\n",
      "i = modify(i)\t\t\t\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 3\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-3, Page Number: 281"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Pointer parameters in action: the swap routine\n",
      "\n",
      "def swap(i,j) :\n",
      "\t\"\"\" exchange i with j and return \"\"\"\n",
      "\ttemp = i   \t\t\t# temp variable can be avoided using i,j = j,i\n",
      "\ti = j\n",
      "\tj = temp\n",
      "\treturn i,j\n",
      "\n",
      "i = 1\n",
      "j = 9\n",
      "print \"i = %d and j = %d\"%(i,j)\n",
      "\n",
      "# Now exchange them\n",
      "i, j = swap(i,j)\n",
      "print \"\\nnow i = %d and j = %d\"%(i,j)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "i = 1 and j = 9\n",
        "\n",
        "now i = 9 and j = 1\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-4, Page Number: 282"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Array names are constant pointers\n",
      "\n",
      "an_array = [ 3, 7 ]\n",
      "\n",
      "def main() :\n",
      "\talso = an_array\n",
      "\talso[0] = 14\n",
      "\tprint \"an_array = { %d, %d }\"%(an_array[0],an_array[1])\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "an_array = { 14, 7 }\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-5, Page Number: 284"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Sorting an array revisited\n",
      "# Passing an array as parameter\n",
      "\n",
      "# Macro definition\n",
      "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n",
      "# Message flags for \"print array\"\n",
      "ORIGINAL = 0\t# Display original array\n",
      "SORTED = 1\t\t# Display sorted array\n",
      "\n",
      "numbers = [ 10, 42, 7, 0, 923, 12, -5, 6, 19, 3 ]\n",
      "array_size = 0\n",
      "\n",
      "def get_size(size)\t:\n",
      "\t\"\"\" reads the size of the array (until a valid value is entered) \"\"\"\n",
      "\tglobal array_size\n",
      "\tprint \"How many numbers?\",\n",
      "\tsize = 10\n",
      "\tprint size\n",
      "\twhile size < 1 or size > MAX_ARRAY_SIZE\t:\n",
      "\t\tprint \"How many numbers?\",\n",
      "\t\tsize = 10\n",
      "\t\tprint size\n",
      "\tarray_size = size\n",
      "\n",
      "def get_array(array, size) :\n",
      "\t\"\"\" read in the array called 'array' of size 'size' \"\"\"\n",
      "\tprint \"Please enter the integers:\"\n",
      "\tfor i in array :\n",
      "\t\tprint i,\n",
      "\tprint\n",
      "\n",
      "def print_array(array, size, message) :\n",
      "\t\"\"\" print out the array on a single line. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n",
      "\tif message == ORIGINAL :\n",
      "\t\tprint \"\\nThe original array is:\"\n",
      "\telse :\n",
      "\t\tprint \"\\nThe sorted array is:\"\n",
      "\tfor i in array :\n",
      "\t\tprint i,\n",
      "\tprint\n",
      "\n",
      "def sort_array(array,size) :\n",
      "\t\"\"\" sort 'array' of size 'size' \"\"\"\n",
      "\tfor i in range(size - 1) :\n",
      "\t\tfor j in range(i+1, size) :\n",
      "\t\t\tif array[i] > array[j] :\n",
      "\t\t\t\telement_swap(array, i, j)\n",
      "\n",
      "def element_swap(array, i, j) :\n",
      "\t\"\"\" swap elements 'i' and 'j' of 'array' \"\"\"\n",
      "\tglobal numbers\n",
      "\tnumbers[i],numbers[j] = numbers[j],numbers[i]\n",
      "\n",
      "\n",
      "get_size(array_size)\t\t\t\t\t\t# Read in the array's size\n",
      "get_array(numbers, array_size)\t\t\t\t\t# Read in the array 'numbers' of size 'array_size'\n",
      "print_array(numbers, array_size, ORIGINAL) \t# Echo the array \n",
      "sort_array(numbers, array_size)\n",
      "print_array(numbers, array_size, SORTED)\t\t# Print the sorted array"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "How many numbers? 10\n",
        "Please enter the integers:\n",
        "10 42 7 0 923 12 -5 6 19 3\n",
        "\n",
        "The original array is:\n",
        "10 42 7 0 923 12 -5 6 19 3\n",
        "\n",
        "The sorted array is:\n",
        "-5 0 3 6 7 10 12 19 42 923\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 11-6, Page Number: 294"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Sorting of array revisited\n",
      "# Two dimensional char array (array of strings)\n",
      "\n",
      "# Macro definition\n",
      "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n",
      "MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n",
      "# Message flags for \"print_array\"\n",
      "ORIGINAL = 0\n",
      "SORTED = 1\n",
      "\n",
      "words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n",
      "array_size = 0\n",
      "\n",
      "def get_size(size) :\n",
      "\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n",
      "\tprint \"How many words? \",\n",
      "\tsize = 8\n",
      "\tprint size\n",
      "\twhile size < 1 or size > MAX_ARRAY_SIZE :\n",
      "\t\tprint \"How many words? \",\n",
      "\t\tsize = 8\n",
      "\t\tprint size\n",
      "\tglobal array_size\n",
      "\tarray_size = size\n",
      "\n",
      "def get_array(array,size) :\n",
      "\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n",
      "\tprint \"Please enter the words:\"\n",
      "\tfor i in array :\n",
      "\t\tprint i\n",
      "\t\n",
      "def print_array(array,size,message) :\n",
      "\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n",
      "\tif message == ORIGINAL :\n",
      "\t\tprint \"\\nThe original array is:\"\n",
      "\telse :\n",
      "\t\tprint \"\\nThe sorted array is:\"\n",
      "\tfor i in array :\n",
      "\t\tprint i\n",
      "\n",
      "def sort_array(array,size) :\n",
      "\t\"\"\" sort string array \"\"\"\n",
      "\tfor i in range (size-1) :\n",
      "\t\tfor j in range (i + 1, size) :\n",
      "\t\t\tif array[i] > array[j] :\n",
      "\t\t\t\tstring_swap(i,j)\n",
      "\n",
      "def string_swap(i, j) :\n",
      "\t\"\"\" Exchange strings \"\"\"\n",
      "\tglobal words\n",
      "\twords[i],words[j] = words[j],words[i]\n",
      "\n",
      "get_size(array_size)\t\t# Read in the array size\n",
      "get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n",
      "print_array(words, array_size, ORIGINAL)\t# Echo the original array\n",
      "sort_array(words, array_size)\t\n",
      "print_array(words, array_size, SORTED) # Print the sorted array"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "How many words?  8\n",
        "Please enter the words:\n",
        "cat\n",
        "pig\n",
        "dog\n",
        "earlobe\n",
        "bingo\n",
        "elbow\n",
        "likely\n",
        "radar\n",
        "\n",
        "The original array is:\n",
        "cat\n",
        "pig\n",
        "dog\n",
        "earlobe\n",
        "bingo\n",
        "elbow\n",
        "likely\n",
        "radar\n",
        "\n",
        "The sorted array is:\n",
        "bingo\n",
        "cat\n",
        "dog\n",
        "earlobe\n",
        "elbow\n",
        "likely\n",
        "pig\n",
        "radar\n"
       ]
      }
     ],
     "prompt_number": 6
    }
   ],
   "metadata": {}
  }
 ]
}