summaryrefslogtreecommitdiff
path: root/C_Programming_for_the_Absolute_Beginner/Chapter10.ipynb
blob: c2e528544ac069dbe7fa4f59c81a541b59eee924 (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
{
 "metadata": {
  "name": "",
  "signature": "sha256:af2735a34ebb2cb3c8a379aacf70cb46d8ee4a8ed09afacd9b5d0d21e0a0dd5a"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 10 : Dynamic Memory Allocation"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.1, Page No 229"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "class employee:\n",
      "    def __init__(self,id1,name,salary):\n",
      "        self.id1 = id1\n",
      "        self.name = name\n",
      "        self.salary = salary\n",
      "e = employee(10,\"abc\",12000.02)\n",
      "x = 1\n",
      "f = 1.1\n",
      "d = 1111.11\n",
      "c = 'C'\n",
      "print \"Sizo of integer : \" + str(sys.getsizeof(x)) + \"bytes \\n\"\n",
      "print \"Sizo of float : \" + str(sys.getsizeof(f)) + \"bytes \\n\"\n",
      "print \"Sizo of double : \" + str(sys.getsizeof(d)) + \"bytes \\n\"\n",
      "print \"Sizo of char : \" + str(sys.getsizeof(c)) + \"bytes \\n\"\n",
      "print \"Sizo of employee structure : \" + str(sys.getsizeof(e)) + \"bytes \\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sizo of integer : 24bytes \n",
        "\n",
        "Sizo of float : 24bytes \n",
        "\n",
        "Sizo of double : 24bytes \n",
        "\n",
        "Sizo of char : 34bytes \n",
        "\n",
        "Sizo of employee structure : 64bytes \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.2, Page No 230"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "array = []\n",
      "\n",
      "print \"\\nSize of array: \" + str(sys.getsizeof(array)) +  \"bytes\\n\"\n",
      "print \"Number of elements in array \"\n",
      "print str(sys.getsizeof(array) / sys.getsizeof(int)) + \"\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Size of array: 64bytes\n",
        "\n",
        "Number of elements in array \n",
        "0\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.3, Page No 231"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.4, Page No 232"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.5, Page No 232"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.6, Page No 233"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function\n",
      "#if name is None:\n",
      "#    print \"\\nOut of memory!\\n\"\n",
      "#else:\n",
      "#    print \"\\nMemory allocated.\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.7, Page No 234"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function\n",
      "#if name is not None:\n",
      "#    name = raw_input(\"Enter your name: \")\n",
      "#    print \"\\nHi \" + name + \"\\n\" "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.8, Page No 235"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function\n",
      "#if name is not None:\n",
      "#    name = raw_input(\"Enter your name: \")\n",
      "#    print \"\\nHi \" + name + \"\\n\"\n",
      "#    del name"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.9, Page No 236"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function\n",
      "#if numbers is None:\n",
      "#    return\n",
      "#numbers[0] = 100\n",
      "#numbers[1] = 200\n",
      "#numbers[2] = 300\n",
      "#numbers[3] = 400\n",
      "#numbers[4] = 500\n",
      "#print \"\\nIndividual memory segments initialized to:\\n\"\n",
      "#for x in range(5):\n",
      "#    print \"numbers[\" +x+\"] = \\n\" + numbers[x]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.10, Page No 237"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no calloc function\n",
      "#if numbers is None:\n",
      "#    return"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.11, Page No 239"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# there is no malloc function\n",
      "#if numbers is None:\n",
      "#    print \"\\nOut of memory!\\n\"\n",
      "#    return\n",
      "#else:\n",
      "#    number = newNumber\n",
      "#    for x in range(10):\n",
      "#        numbers[x] = x * 10\n",
      "#    print \"\\nExpanded memory:\\n\"\n",
      "#    for x in range(10):\n",
      "#        print \"numbers[\" +x+\"] = \\n\" + numbers[x]\n",
      "#    del number"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10.12, Page No 241"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import random\n",
      "# there is no calloc function\n",
      "#time\n",
      "#print \"\\nMath Quiz\\n\\n\"\n",
      "#response = raw_input(\"Enter # of problems: \")\n",
      "#if op1 is None || op2 is None || answer is None ||result is None:\n",
      "#    print \"\\nOut of Memory!\\n\"\n",
      "#    return\n",
      "#for x in range(response):\n",
      "#    op1[x] = random.randint(1,200) % 100\n",
      "#    op2[x] = random.randint(1,200) % 100\n",
      "#    print \"\\n\" + op1[x] + \"+\" + op2[x]\"\n",
      "#    answer[x] = raw_input(\"\")\n",
      "#    if answer[x] == op1[x] + op2[x]:\n",
      "#        result[x] = 'c'\n",
      "#    else:\n",
      "#        result[x] = 'i'\n",
      "#print \"\\nQuiz Results\\n\"\n",
      "#print \"\\nQuestion\\tYour Answer\\tCorrect\\n\"\n",
      "#for x in range(response):\n",
      "#    if result[x] == 'c':\n",
      "#        print op1[x] + \"+\" + op2[x] + \"\\t\\t\" + answer[x] + \"\\t\\tYes\\n\"\n",
      "#    else:\n",
      "#        print op1[x] + \"+\" + op2[x] + \"\\t\\t\" + answer[x] + \"\\t\\tNo\\n\"\n",
      "#del op1\n",
      "#del op2\n",
      "#del answer\n",
      "#del result   "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 20
    }
   ],
   "metadata": {}
  }
 ]
}