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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
{
"metadata": {
"name": "",
"signature": "sha256:aced1a8512bbd1c950b44a0ce98d7bb529f62e617c89b1c73eec08ce49b27311"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 6: Arrays and Subscribed Variables"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 1 , Page number: CP-122"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find the sum of n numbers using array\n",
"# Variable declaration\n",
"\n",
"n = 5\n",
"x = [36,45,52,44,62]\n",
"\n",
"print \"How many integers ?\",n\n",
"print \"Enter the 1th value :\",x[0]\n",
"print \"Enter the 2th value :\",x[1]\n",
"print \"Enter the 3th value :\",x[2]\n",
"print \"Enter the 4th value :\",x[3]\n",
"print \"Enter the 5th value :\",x[4]\n",
"\n",
"summ = 0 \n",
"\n",
"# Calculation of sum of numbers\n",
"for i in x:\n",
" summ = summ + i\n",
" \n",
"\n",
"print \"Sum of all integers =\",summ\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many integers ? 5\n",
"Enter the 1th value : 36\n",
"Enter the 2th value : 45\n",
"Enter the 3th value : 52\n",
"Enter the 4th value : 44\n",
"Enter the 5th value : 62\n",
"Sum of all integers = 239\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 2 , Page number: CP-123"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find the biggest of n numbers\n",
"# Variable declaration\n",
"\n",
"n = 5\n",
"x = [25,-228,0,185,36]\n",
"\n",
"print \"How many numbers ?\",n\n",
"print \"Enter all those numbers\"\n",
"for a in x:\n",
" print a,\n",
"print \n",
"\n",
"big = x[0]\n",
"\n",
"\n",
"for i in x:\n",
" if (i > big):\n",
" big = i\n",
"\n",
"\n",
"print \"%d is the biggest number\" % big "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many numbers ? 5\n",
"Enter all those numbers\n",
"25 -228 0 185 36\n",
"185 is the biggest number\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 3 , Page number: Cp-124"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find the arithmetic mean, variance and standard deviation\n",
"# Variable declaration\n",
"\n",
"n = 6\n",
"x = [3.1,3.8,3.6,4.0,3.4,3.8]\n",
"summ = 0\n",
"vsum = 0\n",
"\n",
"print \"How many values ?\",n\n",
"print \"Enter all values in the list \"\n",
"for a in x:\n",
" print a,\n",
"print \n",
"\n",
"# Loop to find sum of all values\n",
"\n",
"for i in x:\n",
" summ = summ + i\n",
"\n",
"xbar = summ / n\n",
"\n",
"# Loop to find the numerator vsum to find variance\n",
"\n",
"for i in x:\n",
" vsum = vsum + (i - xbar) * (i - xbar)\n",
"\n",
"\n",
"sigmax = vsum / n\n",
"sd = sigmax ** 0.5\n",
"\n",
"print \"Arithmetic mean = %0.3f\" % xbar\n",
"print \"Variance = %0.3f \" % sigmax\n",
"print \"Standard deviation = %0.3f\" % sd"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many values ? 6\n",
"Enter all values in the list \n",
"3.1 3.8 3.6 4.0 3.4 3.8\n",
"Arithmetic mean = 3.617\n",
"Variance = 0.088 \n",
"Standard deviation = 0.297\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 4 , Page number: CP-126"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to calculate mean of marks and print list of marks greater than mean\n",
"# Variable declaration\n",
"\n",
"n = 5\n",
"x = [58,63,68,54,48]\n",
"summ = 0 # Used summ instead of sum since it was a inbuilt function\n",
"i = 0\n",
"\n",
"print \"How many students ?\",n\n",
"print \"Enter all the marks \"\n",
"for a in x:\n",
" print a,\n",
"print \n",
"\n",
"for i in x:\n",
" summ = summ + i\n",
"\n",
"mean = float(summ) / n\n",
"\n",
"print \"Mean = %0.2f\" % mean\n",
"print \"Marks greater than mean :\",\n",
"\n",
"i = 0\n",
"for i in x:\n",
" if (i > mean):\n",
" print i,\n",
"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many students ? 5\n",
"Enter all the marks \n",
"58 63 68 54 48\n",
"Mean = 58.20\n",
"Marks greater than mean : 63 68\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 5 , Page number: CP-127"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find sum of all positive and negative numbers and to find out which is larger in magnitude\n",
"# Variable declaration\n",
"import math\n",
"\n",
"n = 6\n",
"x = [8,-12,-16,12,-9,5]\n",
"psum = 0\n",
"nsum = 0\n",
"\n",
"print \"How many values ?\",n\n",
"print \"Enter all values in the list\"\n",
"for i in x:\n",
" print i,\n",
"print \n",
"\n",
"# Loop to calculate sum of positive and negative values\n",
"\n",
"for i in x:\n",
" if i > 0:\n",
" psum = psum + i\n",
" else:\n",
" nsum = nsum + i\n",
"\n",
"print \"Sum of positive values = %0.2f\" % psum\n",
"print \"Sum of negative values = %0.2f\" % nsum\n",
"\n",
"if (psum > abs(nsum)):\n",
" print \"Positive sum is greater in magnitude\"\n",
"else:\n",
" print \"Negative sum is greater in magnitude\"\n",
"\n",
"diff = abs(psum) - abs(nsum)\n",
"print \"Difference in magnitude = %0.2f\" % abs(diff)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many values ? 6\n",
"Enter all values in the list\n",
"8 -12 -16 12 -9 5\n",
"Sum of positive values = 25.00\n",
"Sum of negative values = -37.00\n",
"Negative sum is greater in magnitude\n",
"Difference in magnitude = 12.00\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 6 , Page number: CP-129"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to sort n numbers in ascending order\n",
"# Variable declaration\n",
"\n",
"n = 4\n",
"x = [32,-10,20,5]\n",
"i = 0\n",
"\n",
"\n",
"print \"How many numbers ?\",n\n",
"print \"Enter the list of 4 numbers\"\n",
"for a in x:\n",
" print a,\n",
"print\n",
"\n",
"# Loop to arrange the numbers in ascending order\n",
"\n",
"while i < n-1:\n",
" j = i + 1\n",
" while j < n:\n",
" if x[i] > x[j]:\n",
" temp = x[i]\n",
" x[i] = x[j]\n",
" x[j] = temp\n",
" j = j + 1\n",
" i = i + 1\n",
"\n",
"print \"Numbers in ascending order \"\n",
"\n",
"for a in x:\n",
" print a,\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many numbers ? 4\n",
"Enter the list of 4 numbers\n",
"32 -10 20 5\n",
"Numbers in ascending order \n",
"-10 5 20 32\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 7 , Page number: CP-131"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to search the key value and to print it if the search is successful\n",
"# Variable declaration\n",
"\n",
"n = 6\n",
"x = [6,-2,8,3,13,10]\n",
"s = 3\n",
"\n",
"print \"How many values in the list ?\",n\n",
"print \"Enter all values in the list\"\n",
"for i in x:\n",
" print i,\n",
"print \n",
"print \"Enter the key value to be searched :\",s\n",
"\n",
"# loop to search key value in the list\n",
"\n",
"for i in range(n):\n",
" if s == x[i]:\n",
" print s,\" is available in\",i+1,\"th location\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many values in the list ? 6\n",
"Enter all values in the list\n",
"6 -2 8 3 13 10\n",
"Enter the key value to be searched : 3\n",
"3 is available in 4 th location\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 8 , Page number: CP-133"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to sort n numbers using bubble sort and find number of exchanges and passes\n",
"# Variable declaration\n",
"\n",
"n = 4\n",
"x = [6,-2,8,3]\n",
"exchng = 0\n",
"\n",
"print \"How many numbers?\",n\n",
"print \"Enter all the numbers in the list\"\n",
"for i in x:\n",
" print i,\n",
"\n",
"print\n",
"\n",
"for i in range(0,n-1):\n",
" for j in range(0,n-i-1):\n",
" if x[j] > x[j+1]:\n",
" temp = x[j]\n",
" x[j] = x[j+1]\n",
" x[j+1] = temp\n",
" exchng = exchng + 1\n",
" \n",
"\n",
"print \"The sorted list is\"\n",
"for i in x:\n",
" print i,\n",
"\n",
"print \n",
"\n",
"print \"Sorted in\",n-1,\"passes and\",exchng,\"exchanges\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many numbers? 4\n",
"Enter all the numbers in the list\n",
"6 -2 8 3\n",
"The sorted list is\n",
"-2 3 6 8\n",
"Sorted in 3 passes and 3 exchanges\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 9 , Page number: CP-135"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to add two matrices\n",
"# Variable declaration\n",
"\n",
"a = [[2,-2],\n",
" [0,4]]\n",
"b = [[6,2],\n",
" [4,-5]]\n",
"\n",
"c = [[0,0],\n",
" [0,0]]\n",
"\n",
"m = 2\n",
"n = 2\n",
"\n",
"print \"How many rows and columns ?\",m,n\n",
"print \"Enter A matrix\"\n",
"for i in a:\n",
" print i\n",
"\n",
"print \"Enter B matrix\"\n",
"for j in b:\n",
" print j\n",
"\n",
"# Loop to add two matrices\n",
"\n",
"for i in range(m):\n",
" for j in range(n):\n",
" c[i][j] = a[i][j] + b[i][j]\n",
"\n",
"\n",
"print \"Resultant matrix is\"\n",
"for ci in c:\n",
" print ci"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"How many rows and columns ? 2 2\n",
"Enter A matrix\n",
"[2, -2]\n",
"[0, 4]\n",
"Enter B matrix\n",
"[6, 2]\n",
"[4, -5]\n",
"Resultant matrix is\n",
"[8, 0]\n",
"[4, -1]\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 10 , Page number: CP-136"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to multiply two matrices\n",
"# Variable declaration\n",
"\n",
"m = 2\n",
"n = 2\n",
"l = 2\n",
"a = [[2,-2],\n",
" [0,4]]\n",
"b = [[6,2],\n",
" [4,-5]]\n",
"c = [[0,0],\n",
" [0,0]]\n",
"\n",
"print \"Enter order of A matrix :\",m,n\n",
"print \"Enter A matrix\"\n",
"for i in a:\n",
" print i\n",
"print \"Enter order of B matrix :\",m,n\n",
"print \"Enter B matrix\"\n",
"for j in b:\n",
" print j\n",
"\n",
"# Loop to multiply two matrices\n",
"# iterate through rowa of A\n",
"for i in range(m):\n",
" # iterate through columns of B\n",
" for j in range(l):\n",
" c[i][j] = 0\n",
" # iterate through rows of B\n",
" for k in range(n):\n",
" c[i][j] = c[i][j] + a[i][k] * b[k][j]\n",
"\n",
"\n",
"print \"Resultant matrix is\"\n",
"\n",
"for i in c:\n",
" print i\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter order of A matrix : 2 2\n",
"Enter A matrix\n",
"[2, -2]\n",
"[0, 4]\n",
"Enter order of B matrix : 2 2\n",
"Enter B matrix\n",
"[6, 2]\n",
"[4, -5]\n",
"Resultant matrix is\n",
"[4, 14]\n",
"[16, -20]\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 11 , Page number: CP-138"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find and print the transpose of the matrix\n",
"# Variable declaration\n",
"\n",
"m = 2\n",
"n = 3\n",
"a = [[-3,6,0],\n",
" [3,2,8]]\n",
"at = [[0,0],\n",
" [0,0],\n",
" [0,0]]\n",
"\n",
"print \"Enter order of the matrix :\",m,n\n",
"print \"Enter the matrix values\"\n",
"for i in a :\n",
" print i\n",
"\n",
"# Loop to calculate transpose\n",
"\n",
"for i in range(m):\n",
" for j in range(n):\n",
" at[j][i] = a[i][j]\n",
"\n",
"print \"The transposed matrix is \"\n",
"for i in at:\n",
" print i\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter order of the matrix : 2 3\n",
"Enter the matrix values\n",
"[-3, 6, 0]\n",
"[3, 2, 8]\n",
"The transposed matrix is \n",
"[-3, 3]\n",
"[6, 2]\n",
"[0, 8]\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 12 ,Page number: CP-139"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to check whether a given matrix is symmetric or not\n",
"# Variable declaration\n",
"\n",
"m = 3\n",
"a = [[5,3,8],\n",
" [3,1,-7],\n",
" [8,-7,4]]\n",
"\n",
"print \"Enter order of the square matrix :\",m\n",
"for i in a:\n",
" print i\n",
"\n",
"# Loop to check whether symmetric or not\n",
"\n",
"for i in range(m):\n",
" flag = 0\n",
" for j in range(m):\n",
" flag = 0\n",
" if a[i][j] == a[j][i]:\n",
" continue\n",
" else:\n",
" flag = 1\n",
"\n",
"if flag == 0:\n",
" print \"The given matrix is a symmetric matrix\"\n",
"else:\n",
" print \"The given matrix is not a symmetric matrix\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter order of the square matrix : 3\n",
"[5, 3, 8]\n",
"[3, 1, -7]\n",
"[8, -7, 4]\n",
"The given matrix is a symmetric matrix\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13 , Page number: CP-141"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Program to find the trace of a given square matrix\n",
"# Variable dclaration\n",
"\n",
"m = 3\n",
"a = [[3,2,-1],\n",
" [4,1,8],\n",
" [6,4,2]]\n",
"summ = 0\n",
"\n",
"print \"Enter order of the square matrix :\",m\n",
"print \"Enter the matrix\"\n",
"for i in a:\n",
" print i\n",
"\n",
"# Loop to find trace\n",
"for i in range(m):\n",
" summ = summ + a[i][i]\n",
"\n",
"print \"Trace of the matrix =\",summ "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|