summaryrefslogtreecommitdiff
path: root/Structured_Programing_with_C++/Chapter4.ipynb
blob: aff2c5ebbb991774637b842e0d473a06ea6f0c50 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
{
 "metadata": {
  "name": "",
  "signature": "sha256:a27de4c3e22de467837d62cdd94a3b4f20a866984babfe11880e076feb1374ad"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 4 : Arrays"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.1, Page No 79"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Declarations\n",
      "\n",
      "iNoOfDays = 31\n",
      "dSum = 0\n",
      "dblTempApr=[[0 for col in range(31)] for row in range(31)]\n",
      "\n",
      "\n",
      "#Entry and calculation\n",
      "\n",
      "for i in range(1,iNoOfDays):\n",
      "    dblTempApr[i] = raw_input(\"Temperature Day %d: \" % i)\n",
      "    dSum += dblTempApr[i]\n",
      "\n",
      "dAvg = dSum / iNoOfDays\n",
      "\n",
      "#Printout\n",
      "\n",
      "print \"Average temperature: %f\" % dAvg\n",
      "print \"Temperatures exceeding average: \"\n",
      "\n",
      "for i in range(1,iNoOfDays):\n",
      "    if (dblTempApr[i] > dAvg):\n",
      "        print \"Day no.: %d\" % i,\n",
      "        print \" temp: %d\" % dblTempApr[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 1: 32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 2: 33\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 3: 35\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 4: 36\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 5: 34\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 6: 32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 7: 31\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 8: 29\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 9: 28\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 10: 27\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 11: 25\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 12: 26\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 13: 35\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 14: 36\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 15: 35\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 16: 50\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 17: 40\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 18: 42\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 19: 44\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 20: 43\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 21: 41\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 22: 38\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 23: 37\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 24: 3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 25: 36\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 26: 35\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 27: 45\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 28: 32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 29: 21\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Temperature Day 30: 15\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Average temperature: 32.000000\n",
        "Temperatures exceeding average: \n",
        "Day no.: 2  temp: 33\n",
        "Day no.: 3  temp: 35\n",
        "Day no.: 4  temp: 36\n",
        "Day no.: 5  temp: 34\n",
        "Day no.: 13  temp: 35\n",
        "Day no.: 14  temp: 36\n",
        "Day no.: 15  temp: 35\n",
        "Day no.: 16  temp: 50\n",
        "Day no.: 17  temp: 40\n",
        "Day no.: 18  temp: 42\n",
        "Day no.: 19  temp: 44\n",
        "Day no.: 20  temp: 43\n",
        "Day no.: 21  temp: 41\n",
        "Day no.: 22  temp: 38\n",
        "Day no.: 23  temp: 37\n",
        "Day no.: 25  temp: 36\n",
        "Day no.: 26  temp: 35\n",
        "Day no.: 27  temp: 45\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.2, Page No 83"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Declaration\n",
      "\n",
      "iMaxNo = 100\n",
      "dLimit = 5000\n",
      "perc1 = 0.1\n",
      "perc2 = 0.15\n",
      "sales = [[0 for col in range(1)] for row in range(iMaxNo)]\n",
      "\n",
      "#Initialize array\n",
      "\n",
      "for i in range(0,iMaxNo):\n",
      "    sales[i] = 0\n",
      "\n",
      "#Enter Salesman info\n",
      "nr=input(\"Enter Salesman no. : \")\n",
      "dAmount=input(\"Enter sales amount: \")\n",
      "\n",
      "while (nr and dAmount):\n",
      "    if(nr<1 or nr>iMaxNo or dAmount<0):\n",
      "        print \"Input error\"\n",
      "        break;\n",
      "    else:\n",
      "        sales[nr-1]+= dAmount\n",
      "    nr=int(raw_input(\"Enter Salesman no. : \"))\n",
      "    dAmount=float(raw_input(\"Enter sales amount: \"))\n",
      "\n",
      "#Print summary\n",
      "\n",
      "print \"Number        Amount        Fee\"\n",
      "print \"======        ======        ===\"\n",
      "\n",
      "for i in range(0,iMaxNo):\n",
      "    if (sales[i] > 0):\n",
      "        if(sales[i] <= dLimit):\n",
      "            dFee = perc1 * sales[i]\n",
      "        else:\n",
      "            dFee = perc1 * dLimit + perc2*(sales[i] - dLimit)\n",
      "        j=i;\n",
      "        j=j+1;\n",
      "        print \"%4d\" % j,\n",
      "        print \"%13d\" % sales[i],\n",
      "        print \"%10.0f\" % dFee"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 1000\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 2000\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 3000\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 4000\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 5000\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Salesman no. : 6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter sales amount: 00\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Number        Amount        Fee\n",
        "======        ======        ===\n",
        "   1          1000        100\n",
        "   2          2000        200\n",
        "   3          3000        300\n",
        "   4          4000        400\n",
        "   5          5000        500\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.3, Page No 92"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Declaration\n",
      "l=0\n",
      "r=30\n",
      "iFound=0\n",
      "# Array is in sorted form\n",
      "iProdid= [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]\n",
      "\n",
      "iSrch = raw_input(\"Enter the searched product id: \")\n",
      "\n",
      "if(iSrch == iProdid[0]):\n",
      "    iPos = 0\n",
      "    iFound= 1\n",
      "\n",
      "if (iSrch == iProdid[30]):\n",
      "    iPos = 30\n",
      "    iFound = 1\n",
      "\n",
      "while not (iFound):\n",
      "    iMid = (int)(l + ((r-l)/2))\n",
      "    if(iSrch == iProdid[iMid]):\n",
      "        iFound = 1\n",
      "        iPos = iMid\n",
      "\n",
      "    if(iSrch > iProdid[iMid]):\n",
      "        l=iMid\n",
      "    else:\n",
      "        r=iMid"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the searched product id: 15\n"
       ]
      }
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}