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
|
{
"metadata": {
"name": "",
"signature": "sha256:ebda2195f5f56b03f9c5aecb268e628cfb74a1a1fa1ea18ae277cc3f964d54bd"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 6: Pointing to data"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.1, Page No.98"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num=100\n",
"sum=0.0123456789\n",
"text=\"C++ Fun\"\n",
"print \"Integer variable starts at\",hex(id(num))\n",
"print \"Double variable starts at\",hex(id(sum))\n",
"print \"String variable starts at\",hex(id(text))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Integer variable starts at 0x126eb04\n",
"Double variable starts at 0x279b490\n",
"String variable starts at 0x2b7a160\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.2, Page No.100"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a=8\n",
"b=16\n",
"aPtr=id(a)\n",
"bPtr=id(b)\n",
"print \"Address of pointers...\"\n",
"print \"aPtr:\",hex(id(aPtr))\n",
"print \"bPtr:\",hex(id(bPtr))\n",
"print \"Values in pointers...\"\n",
"print \"aPtr:\",hex(aPtr)\n",
"print \"bPtr:\",hex(bPtr)\n",
"#As python does not support pointers values can't be displayed using * symbol"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of pointers...\n",
"aPtr: 0x2808ac8\n",
"bPtr: 0x2808b1c\n",
"Values in pointers...\n",
"aPtr: 0x126e794\n",
"bPtr: 0x126e734\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.3, Page No.102"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nums={1,2,3,4,5,6,7,8,9,10}\n",
"ptr=id(nums)\n",
"#As python does not support concept of pointers we can not use pointer arithmetic to print numbers"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.4, Page No.104"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num=5\n",
"def writeOutput(value):\n",
" print \"Current Value: \",value\n",
"def computeTriple(value):\n",
" global num\n",
" num=num*3\n",
"\n",
"\n",
"writeOutput(num)\n",
"num=num+15\n",
"writeOutput(num)\n",
"computeTriple(num)\n",
"writeOutput(num)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Current Value: 5\n",
"Current Value: 20\n",
"Current Value: 60\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.5, Page No.106"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"letters=['C','+','+',' ','F','u','n','\\0']\n",
"text=\"C++ Fun\"\n",
"term=\"Element\"\n",
"lang=\"C++\"\n",
"ap1=[\"Great \",\"Program \",\"Code \"]\n",
"ap2=[lang,\"is \",\"Fun\"]\n",
"ap3=[ap2[0],ap2[1],ap1[0]]\n",
"ap4=[ap1[2],ap2[1],ap2[2]]\n",
"print \"\".join(letters)\n",
"print text\n",
"for i in range(0,3):\n",
" print term,i,\" \",\n",
" print ap1[i],\" \",\n",
" print ap2[i],\" \",\n",
" print ap3[i],\" \",\n",
" print ap4[i],\" \""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"C++ Fun\u0000\n",
"C++ Fun\n",
"Element 0 Great C++ C++ Code \n",
"Element 1 Program is is is \n",
"Element 2 Code Fun Great Fun \n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.6, Page No.108"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num=0\n",
"rNum=400\n",
"num=rNum\n",
"print \"Value direct\",num\n",
"print \"Value via reference \",rNum\n",
"\n",
"print \"Address direct\",hex(id(num))\n",
"print \"Address via reference\",hex(id(rNum))\n",
"\n",
"rNum=rNum*2\n",
"num=rNum\n",
"\n",
"print \"Value direct\",num\n",
"print \"Value via reference \",rNum\n",
"\n",
"#as python does not support pointers"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value direct 400\n",
"Value via reference 400\n",
"Address direct 0x27cd294\n",
"Address via reference 0x27cd294\n",
"Value direct 800\n",
"Value via reference 800\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.7, Page No. 110"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def writeOutput(value):\n",
" print \"Current Value: \",value\n",
"\n",
"def computeTriple(value):\n",
" value=value*3\n",
" print \"Current Value: \",value\n",
" \n",
"num=5\n",
"ref=num\n",
"writeOutput(num)\n",
"num=num+15\n",
"writeOutput(num)\n",
"computeTriple(num)\n",
"\n",
"#as python does not support pointers I dont have used pointer in this program"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Current Value: 5\n",
"Current Value: 20\n",
"Current Value: 60\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6.8, Page No.113"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def add(a,b):\n",
" total=a+b\n",
" print \"total:\",total\n",
"\n",
"num=100\n",
"sum=500\n",
"rNum=num\n",
"ptr=num\n",
"print \"Reference:\",rNum\n",
"print \"Pointer:\",ptr\n",
"ptr=sum\n",
"print \"Pointer now:\",ptr\n",
"add(rNum,ptr)\n",
"\n",
"\n",
"#as python does not support pointers I dont have used pointer in this progaram"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Reference: 100\n",
"Pointer: 100\n",
"Pointer now: 500\n",
"total: 600\n"
]
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
|