summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_9(1).ipynb
blob: b31c36a09254f4e5b9678432d9e68d63be180e86 (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
{
 "metadata": {
  "name": "Chapter 9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h1>Chapter 9: More Data Types and Operations<h1>"
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.1, Page Number: 182<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Printing a string'''\n#pyhton doesnt have constants, hence a normal object has been used\n\ndef code(str):\n    print str\n\n#Calling function\ncode(\"this is a test\")",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "this is a test\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.2, Page Number: 183<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementing const references'''\n\ndef f(i):\n    i=100\n    print i\n    \n#Variable declaration\nk=10\n\n#function call\nf(k)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "100\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.3, Page Number: 187<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementing extern in pyhton'''\n\n#Variable Declaration\nfirst=10                  #global definition of first and last\nlast=20\n\n#Result\nprint first,last\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "10 20\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.4, Page Number: 188<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0             \ncount=0\nnum=5              #Loop for user entries\n\n#compute a running average\ndef r_avg(i):\n    global sum,count\n    sum=sum+i\n    count+=1\n    return sum/count\n\n\nwhile True:\n    print \"Enter numbers(-1 to quit): \"\n    num-=1                 #User input\n    if not(num==-1):\n        print \"Running average is: \",r_avg(num)   #Result\n    if num<0:\n        break",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter numbers(-1 to quit): \nRunning average is:  4\nEnter numbers(-1 to quit): \nRunning average is:  3\nEnter numbers(-1 to quit): \nRunning average is:  3\nEnter numbers(-1 to quit): \nRunning average is:  2\nEnter numbers(-1 to quit): \nRunning average is:  2\nEnter numbers(-1 to quit): \n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.5, Page Number: 189<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Compute a running average of numbers entered by the user'''\n'''Implementing static variables in python'''\n\n#Variable declaration\nsum=0             \ncount=0\nnum=10              #Loop for user entries\n\n#user input given: 9,8,7,6,-2,4,3,2,1,-1\n\n#compute a running average\ndef r_avg(i):\n    global sum,count\n    sum=sum+i\n    count+=1\n    return sum/count\n\ndef reset():\n    global sum,count\n    sum=0\n    count=0\n    \nwhile True:\n    print \"Enter numbers(-1 to quit, -2 to reset): \"\n    num-=1                 #User input\n    if num==5:\n        num=-2\n    if num==-2:            #for reset\n        num=4\n        reset()\n        continue\n    if not(num==-1):\n        print \"Running average is: \",r_avg(num)   #Result\n    else:\n        break",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Enter numbers(-1 to quit, -2 to reset): \nRunning average is:  9\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  8\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  7\nEnter numbers(-1 to quit, -2 to reset): \nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  3\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  2\nEnter numbers(-1 to quit, -2 to reset): \nRunning average is:  1\nEnter numbers(-1 to quit, -2 to reset): \n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.6, Page Number: 196<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementation of enumeration in python'''\n\n#Array of strings that correspond to the enumeration\nname=[\"Jonathan\",\"Golden Delicious\",\"Red Delicious\",\"Winesap\",\n      \"Cortland\",\"McIntosh\"]\n\n#enumeration type\n(Jonathan,Golden_Delicious,Red_Delicious,Winesap,Cortland,McIntosh) = (0,1,2,3,4,5)\n\nfruit=Jonathan\nprint name[fruit]\n\nfruit = Winesap\nprint name[fruit]\n\nfruit = McIntosh\nprint name[fruit]",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Jonathan\nWinesap\nMcIntosh\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.7, Page Number: 198<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Convert to uppercase letter'''\n\n#Variable declaration\nch='j'                  #User input\nwhile True:\n    #This statement turns off the 6th but.\n    c=chr(ord(ch)&223)  #ch is now uppercase\n    print c\n    if c=='Q':\n        break \n    else:\n        ch = chr(ord(ch)+1)  #incrementing for different user inputs\n    \n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "J\nK\nL\nM\nN\nO\nP\nQ\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.8, Page Number: 200<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Convert to lowercase letter'''\n\n#Variable declaration\nch='J'                  #User input\nwhile True:\n    #This statement turns off the 6th but.\n    c=chr(ord(ch)|32)  #ch is now uppercase\n    print c\n    if c=='q':\n        break \n    else:\n        ch = chr(ord(ch)+1)  #incrementing for different user inputs\n    ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "j\nk\nl\nm\nn\no\np\nq\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.9, Page Number: 201<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Display the bits within a byte'''\n\n#function to display the bits\ndef disp_binary(u):\n    t=128\n    while t:\n        if u&t:\n            print 1,\n        else:\n            print 0,\n        t=t/2\n    print \"\"\n    \n#Variable declaration\nu=99                   #User Input\n\nprint \"Here's the number in binary: \",\ndisp_binary(u)\n\nprint \"Here's the complement of th number: \",\ndisp_binary(~u)\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Here's the number in binary:  0 1 1 0 0 0 1 1 \nHere's the complement of th number:  1 0 0 1 1 1 0 0 \n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.10, Page Number: 202<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Demonstrate bitshifting'''\n\n#unction to display the bits within a byte\ndef disp_binary(u):\n    t=128\n    while t:\n        if u&t:\n            print 1,\n        else:\n            print 0,\n        t=t/2\n    print \"\"\n    \n#Variable dclaration\ni=1\n\n#Result\nfor t in range(8):\n    disp_binary(i)\n    i=i<<1\n\nprint\"\\n\"\n\nfor t in range(8):\n    i=i>>1\n    disp_binary(i)\n    ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0 0 0 0 0 0 0 1 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 \n\n\n1 0 0 0 0 0 0 0 \n0 1 0 0 0 0 0 0 \n0 0 1 0 0 0 0 0 \n0 0 0 1 0 0 0 0 \n0 0 0 0 1 0 0 0 \n0 0 0 0 0 1 0 0 \n0 0 0 0 0 0 1 0 \n0 0 0 0 0 0 0 1 \n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.11, Page Number: 204<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''This program implements ? operator in python'''\n\ndef div_zero():\n    print \"Cannot divide by zero.\"\n    return 0\n \n#Variable declaration\ni=10                     #User Input\nj=0\n\n#This statement prevents a divide by zero\nif j:\n    result=i/j\nelse:\n    result=div_zero()\n\n#Result\nprint \"Result: \",result",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Cannot divide by zero.\nResult:  0\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.12, Page Number: 206<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementation of the comma operator'''\n\n#Variable declaration\nj=10\ni=None\n\nj+=1\nj+100\ni=999+j\n\n#Result\nprint i",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "1010\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.13, Page Number: 207<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Demonstrate sizeof'''\n\nfrom ctypes import *\n\n#Variable declaration\nch=c_char\ni=c_int\n\nprint sizeof(ch),          #size of char\nprint sizeof(i),           #size of int\nprint sizeof(c_float),     #size of float\nprint sizeof(c_double)     #size of double\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "1 4 4 8\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.14, Page Number: 209<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Example of pointers'''\n\nfrom ctypes import *\n\n#Variabke declaration \ni=c_int(20)          #allocate memory for int\np=pointer(i)         #assign a pointer to the memory\n\n#Result\nprint p[0]           #proove that it works by displaying value\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "20\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.15, Page Number: 210<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_int(99)          #initialize with 99\np=pointer(i)         #assign a pointer to the value\n\n#Result\nprint p[0]           #displays 99\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "99\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.16, Page Number: 211<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Imlementation of dynamic initialization in python'''\n\nfrom ctypes import *\n\n#Variable declaration \ni=c_double(10)       \np=pointer(i)\n\n#assign the values 100 to 109\nfor i in range(10):\n    p[i]=100.00+i\n\n#display the contents of the array\nfor i in range(10):\n    print p[i],\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 108.0 109.0\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.17, Page Number: 211<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementation of malloc int python'''\n\n#Variable declaration\ni=c_int()              \nj=c_double()\npi=pointer(i)         #pointer to int\npj=pointer(j)         #pointer to double\n\n#Assign values using pointers\npi[0]=10\npj[0]=100.123\n\n#Result\nprint pi[0],pj[0]\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "10 100.123\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": "<h3>Example 9.18, Page Number: 212<h3>"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''Implementation of dynamic memory allocation'''\nfrom ctypes import *\n\n#Variable declaration\ni=pointer(c_int())\nj=pointer(c_double())\n\n#Checking if i and j have been allocated memory addresses\nif not(id(i)):\n    print \"Allocation Failure.\"\n    \nif not(id(j)):\n    print \"Allocation Failure.\"   \n\ni[0]=10\nj[0]=100.123\n\n\n#Result\nprint i[0],j[0]\n\n",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "10 100.123\n"
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}