summaryrefslogtreecommitdiff
path: root/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb')
-rwxr-xr-xMagnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb1093
1 files changed, 1093 insertions, 0 deletions
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb
new file mode 100755
index 00000000..acb61edf
--- /dev/null
+++ b/Magnifying_C_by_Arpita_Goyal/Chapter_9_3.ipynb
@@ -0,0 +1,1093 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9: More on Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.1, Page number: 318"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [[1,2,3,4,5],\n",
+ " [6,7,8,9,10],\n",
+ " [11,12,13,14,15],\n",
+ " [16,17,18,19,20],\n",
+ " [21,22,23,24,25]]\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The value of a is %x ' % id(a))\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tprint ('The value of a[%d] is %x ' % (i, id(i)))\n",
+ "\n",
+ "for i in range (0, 5) :\n",
+ "\tfor j in range (0, 5) : \n",
+ "\t\tprint ('The cell [%d][%d] i.e. %x has %d ' % (i, j, id(a[i][j]), a[i][j]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of a is 7f55dc3e08c0 \n",
+ "The value of a[0] is 18861b0 \n",
+ "The value of a[1] is 1886198 \n",
+ "The value of a[2] is 1886180 \n",
+ "The value of a[3] is 1886168 \n",
+ "The value of a[4] is 1886150 \n",
+ "The cell [0][0] i.e. 1886198 has 1 \n",
+ "The cell [0][1] i.e. 1886180 has 2 \n",
+ "The cell [0][2] i.e. 1886168 has 3 \n",
+ "The cell [0][3] i.e. 1886150 has 4 \n",
+ "The cell [0][4] i.e. 1886138 has 5 \n",
+ "The cell [1][0] i.e. 1886120 has 6 \n",
+ "The cell [1][1] i.e. 1886108 has 7 \n",
+ "The cell [1][2] i.e. 18860f0 has 8 \n",
+ "The cell [1][3] i.e. 18860d8 has 9 \n",
+ "The cell [1][4] i.e. 18860c0 has 10 \n",
+ "The cell [2][0] i.e. 18860a8 has 11 \n",
+ "The cell [2][1] i.e. 1886090 has 12 \n",
+ "The cell [2][2] i.e. 1886078 has 13 \n",
+ "The cell [2][3] i.e. 1886060 has 14 \n",
+ "The cell [2][4] i.e. 1886048 has 15 \n",
+ "The cell [3][0] i.e. 1886030 has 16 \n",
+ "The cell [3][1] i.e. 1886018 has 17 \n",
+ "The cell [3][2] i.e. 1886000 has 18 \n",
+ "The cell [3][3] i.e. 1885fe8 has 19 \n",
+ "The cell [3][4] i.e. 1885fd0 has 20 \n",
+ "The cell [4][0] i.e. 1885fb8 has 21 \n",
+ "The cell [4][1] i.e. 1885fa0 has 22 \n",
+ "The cell [4][2] i.e. 1885f88 has 23 \n",
+ "The cell [4][3] i.e. 1885f70 has 24 \n",
+ "The cell [4][4] i.e. 1885f58 has 25 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.2, Page number: 320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from array import *\n",
+ "\n",
+ "# Variable declaration\n",
+ "a = [[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]],\n",
+ " [[21,22,23,24,25], [26,27,28,29,30], [31,32,33,34,35], [36,37,38,39,40]]]\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The value of a is %x ' % id(a))\n",
+ "\n",
+ "for i in range (0, 2) :\n",
+ "\tprint ('The value of a[%d] is %x ' % (i, id(a[i])))\n",
+ "\tfor j in range (0, 4) :\n",
+ "\t\tprint ('The value of a[%d][%d] is %x ' % (i, j, id(a[i][j])))\n",
+ "\tprint\n",
+ "\n",
+ "for i in range (0, 2) :\n",
+ "\tfor j in range (0, 4) : \n",
+ "\t\tfor k in range (0, 5) :\n",
+ "\t\t\tprint ('The cell [%d][%d][%d] i.e. %x has %d ' % (i, j, k, id(a[i][j][k]), a[i][j][k]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of a is 7f55de4772d8 \n",
+ "The value of a[0] is 7f55de486b90 \n",
+ "The value of a[0][0] is 7f55de4db368 \n",
+ "The value of a[0][1] is 7f55de48f050 \n",
+ "The value of a[0][2] is 7f55de4db3f8 \n",
+ "The value of a[0][3] is 7f55de4c7908 \n",
+ "\n",
+ "The value of a[1] is 7f55de477320 \n",
+ "The value of a[1][0] is 7f55de4ff3f8 \n",
+ "The value of a[1][1] is 7f55de477290 \n",
+ "The value of a[1][2] is 7f55de4773b0 \n",
+ "The value of a[1][3] is 7f55de477368 \n",
+ "\n",
+ "The cell [0][0][0] i.e. 1886198 has 1 \n",
+ "The cell [0][0][1] i.e. 1886180 has 2 \n",
+ "The cell [0][0][2] i.e. 1886168 has 3 \n",
+ "The cell [0][0][3] i.e. 1886150 has 4 \n",
+ "The cell [0][0][4] i.e. 1886138 has 5 \n",
+ "The cell [0][1][0] i.e. 1886120 has 6 \n",
+ "The cell [0][1][1] i.e. 1886108 has 7 \n",
+ "The cell [0][1][2] i.e. 18860f0 has 8 \n",
+ "The cell [0][1][3] i.e. 18860d8 has 9 \n",
+ "The cell [0][1][4] i.e. 18860c0 has 10 \n",
+ "The cell [0][2][0] i.e. 18860a8 has 11 \n",
+ "The cell [0][2][1] i.e. 1886090 has 12 \n",
+ "The cell [0][2][2] i.e. 1886078 has 13 \n",
+ "The cell [0][2][3] i.e. 1886060 has 14 \n",
+ "The cell [0][2][4] i.e. 1886048 has 15 \n",
+ "The cell [0][3][0] i.e. 1886030 has 16 \n",
+ "The cell [0][3][1] i.e. 1886018 has 17 \n",
+ "The cell [0][3][2] i.e. 1886000 has 18 \n",
+ "The cell [0][3][3] i.e. 1885fe8 has 19 \n",
+ "The cell [0][3][4] i.e. 1885fd0 has 20 \n",
+ "The cell [1][0][0] i.e. 1885fb8 has 21 \n",
+ "The cell [1][0][1] i.e. 1885fa0 has 22 \n",
+ "The cell [1][0][2] i.e. 1885f88 has 23 \n",
+ "The cell [1][0][3] i.e. 1885f70 has 24 \n",
+ "The cell [1][0][4] i.e. 1885f58 has 25 \n",
+ "The cell [1][1][0] i.e. 1885f40 has 26 \n",
+ "The cell [1][1][1] i.e. 1885f28 has 27 \n",
+ "The cell [1][1][2] i.e. 1885f10 has 28 \n",
+ "The cell [1][1][3] i.e. 1885ef8 has 29 \n",
+ "The cell [1][1][4] i.e. 1885ee0 has 30 \n",
+ "The cell [1][2][0] i.e. 1885ec8 has 31 \n",
+ "The cell [1][2][1] i.e. 1885eb0 has 32 \n",
+ "The cell [1][2][2] i.e. 1885e98 has 33 \n",
+ "The cell [1][2][3] i.e. 1885e80 has 34 \n",
+ "The cell [1][2][4] i.e. 1885e68 has 35 \n",
+ "The cell [1][3][0] i.e. 1886618 has 36 \n",
+ "The cell [1][3][1] i.e. 1886600 has 37 \n",
+ "The cell [1][3][2] i.e. 18865e8 has 38 \n",
+ "The cell [1][3][3] i.e. 18865d0 has 39 \n",
+ "The cell [1][3][4] i.e. 18865b8 has 40 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.3, Page number: 326"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, num) :\n",
+ "\twhile num > 0 :\n",
+ "\t\ta[num-1] = int(raw_input('Enter value in cell[%d] ' % num))\n",
+ "\t\tnum = num - 1\n",
+ "\t\t\n",
+ "def printdata (a, num) :\n",
+ "\twhile num > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (num, a[num-1]))\n",
+ "\t\tnum = num - 1\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements '))\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[3] is 30 \n",
+ "Value in cell[2] is 20 \n",
+ "Value in cell[1] is 10 \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.4, Page number: 328"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "def printdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
+ "\t\tn = n - 1\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements '))\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[5] 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[4] 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[5] is 1 \n",
+ "Value in cell[4] is 2 \n",
+ "Value in cell[3] is 3 \n",
+ "Value in cell[2] is 4 \n",
+ "Value in cell[1] is 5 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.5, Page number: 330"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "global n\n",
+ "n = 0\n",
+ "\n",
+ "def makearray (n) :\n",
+ "\tn = int(raw_input('Enter the number of elements '))\t\n",
+ "\treturn n\n",
+ "\t\n",
+ "def readdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\ta[n-1] = int(raw_input('Enter value in cell[%d] ' % n))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "def printdata (a, n) :\n",
+ "\twhile n > 0 :\n",
+ "\t\tprint ('Value in cell[%d] is %d ' % (n, a[n-1]))\n",
+ "\t\tn = n - 1\n",
+ "\t\t\n",
+ "n = makearray (n)\n",
+ "a = [0 for i in xrange(n)]\n",
+ "readdata (a, n)\n",
+ "print\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[4] 400\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[3] 300\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[2] 200\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value in cell[1] 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value in cell[4] is 400 \n",
+ "Value in cell[3] is 300 \n",
+ "Value in cell[2] is 200 \n",
+ "Value in cell[1] is 100 \n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.6, Page number: 335"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "string = raw_input('Enter the string: ')\n",
+ "\n",
+ "rev_string = reversed(string)\n",
+ "\n",
+ "if list(string) == list(rev_string) :\n",
+ "\tprint ('%s is a PALINDROME ' % string)\n",
+ "else :\n",
+ "\tprint ('%s is not a PALINDROME ' % string)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string: malayalam\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "malayalam is a PALINDROME \n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.7, Page number: 337"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (a, n) :\n",
+ "\tfor p in range (0, n) :\n",
+ "\t\tfor i in range (0, n-1-p) :\n",
+ "\t\t\tif a[i] > a[i+1] :\n",
+ "\t\t\t\ttemp = a[i]\n",
+ "\t\t\t\ta[i] = a[i+1]\n",
+ "\t\t\t\ta[i+1] = temp\n",
+ "\treturn a\n",
+ "\n",
+ "def printdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\tprint ('The data in cell [%d] is %d ' % (i, a[i]))\n",
+ "\n",
+ "n = int(raw_input('Enter the number of elements to be entered: '))\n",
+ "a = [0] * n\n",
+ "a = readdata (a, n)\n",
+ "a = bubble (a, n)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (a, n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the number of elements to be entered: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [0] 46\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [1] 75\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [2] 68\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [3] 34\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter data in cell [4] 49\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [0] is 34 \n",
+ "The data in cell [1] is 46 \n",
+ "The data in cell [2] is 49 \n",
+ "The data in cell [3] is 68 \n",
+ "The data in cell [4] is 75 \n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.8, Page number: 341"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration, calculation and result\n",
+ "i = 6\n",
+ "c = 'a'\n",
+ "\n",
+ "the_data = i\n",
+ "print ('the_data points to the integer value %d ' % the_data)\n",
+ "\n",
+ "the_data = c\n",
+ "print ('the_data points to the character value %c ' % the_data)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "the_data points to the integer value 6 \n",
+ "the_data points to the character value a \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.9, Page number: 343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
+ "avg = 0\n",
+ "percent = [0 for x in xrange(5)]\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readncompute (marks, percent, avg) :\n",
+ "\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
+ "\t\t\n",
+ "\t\tpercent[i] = percent[i]/5\n",
+ "\t\tavg = avg + percent[i]\n",
+ "\treturn avg/no_of_students, no_of_students\n",
+ "\n",
+ "def printdata (score, per, avg, no_of_students) :\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tprint ('%d ' % score[i][j])\n",
+ "\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
+ "\t\tif per[i] < avg :\n",
+ "\t\t\tprint ('Below average')\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Above average')\n",
+ "\n",
+ "average, nos = readncompute(marks, percent, avg)\n",
+ "print ('\\nThe average marks are %.2f' % average)\n",
+ "printdata (marks, percent, average, nos)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students marks do you want to enter? 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 54\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 83\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 94\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 84\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 62\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 94\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 73\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 62\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average marks are 74.00\n",
+ "\n",
+ "Marks for roll no. 1 are \n",
+ "65 \n",
+ "76 \n",
+ "54 \n",
+ "83 \n",
+ "94 \n",
+ "Percentage for roll no. is 74.00 \n",
+ "Above average\n",
+ "\n",
+ "Marks for roll no. 2 are \n",
+ "84 \n",
+ "62 \n",
+ "94 \n",
+ "73 \n",
+ "62 \n",
+ "Percentage for roll no. is 75.00 \n",
+ "Above average\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.10, Page number: 349"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "marks = [[0 for x in xrange(5)] for x in xrange(5)]\n",
+ "avg = 0\n",
+ "percent = [0 for x in xrange(5)]\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def readncompute (marks, percent, avg) :\n",
+ "\tno_of_students = int(raw_input('How many students marks do you want to enter? '))\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nEnter marks of 5 subjects for roll no. %d ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tmarks[i][j] = int(raw_input('Subject %d: ' % (j+1)))\n",
+ "\t\t\tpercent[i] = percent[i] + marks[i][j]\n",
+ "\t\t\n",
+ "\t\tpercent[i] = percent[i]/5\n",
+ "\t\tavg = avg + percent[i]\n",
+ "\treturn avg/no_of_students, no_of_students\n",
+ "\n",
+ "def printdata (score, per, avg, no_of_students) :\n",
+ "\tfor i in range (0, no_of_students) :\n",
+ "\t\tprint ('\\nMarks for roll no. %d are ' % (i+1))\n",
+ "\t\tfor j in range (0, 5) :\n",
+ "\t\t\tprint ('%d ' % score[i][j])\n",
+ "\t\tprint ('Percentage for roll no. is %.2f ' % per[i])\n",
+ "\t\tif per[i] < avg :\n",
+ "\t\t\tprint ('Below average')\n",
+ "\t\telse :\n",
+ "\t\t\tprint ('Above average')\n",
+ "\n",
+ "average, nos = readncompute(marks, percent, avg)\n",
+ "print ('\\nThe average marks are %.2f' % average)\n",
+ "printdata (marks, percent, average, nos)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students marks do you want to enter? 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 1 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 65\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 54\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 32\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 89\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter marks of 5 subjects for roll no. 2 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 1: 76\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 2: 86\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 3: 43\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 4: 32\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subject 5: 21\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average marks are 57.00\n",
+ "\n",
+ "Marks for roll no. 1 are \n",
+ "65 \n",
+ "76 \n",
+ "54 \n",
+ "32 \n",
+ "89 \n",
+ "Percentage for roll no. is 63.00 \n",
+ "Above average\n",
+ "\n",
+ "Marks for roll no. 2 are \n",
+ "76 \n",
+ "86 \n",
+ "43 \n",
+ "32 \n",
+ "21 \n",
+ "Percentage for roll no. is 51.00 \n",
+ "Below average\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9.11, Page number: 351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Function declaration, calculation and result\n",
+ "def readdata (a, n) :\n",
+ "\tfor i in range (0, n) :\n",
+ "\t\ta[i] = int(raw_input('Enter data in cell [%d] ' % i))\n",
+ "\treturn a\n",
+ "\n",
+ "def bubble (name) :\n",
+ "\tfor p in range (0, 4) :\n",
+ "\t\tfor i in range (0, 3-p) :\n",
+ "\t\t\tif name[i] > name[i+1] :\n",
+ "\t\t\t\ttemp = name[i]\n",
+ "\t\t\t\tname[i] = name[i+1]\n",
+ "\t\t\t\tname[i+1] = temp\n",
+ "\treturn name\n",
+ "\n",
+ "def printdata (name) :\n",
+ "\tfor i in range (0, 4) :\n",
+ "\t\tprint ('The data in cell [%d] is %s ' % ((i+1), name[i]))\n",
+ "\n",
+ "name = [0] * 4\n",
+ "for i in range (0, 4) :\n",
+ "\tname[i] = raw_input('Enter string[%d] ' % (i+1))\n",
+ "\n",
+ "name = bubble (name)\n",
+ "print ('\\nThe sorted data is ')\n",
+ "printdata (name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[1] zaheer\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[2] ashita\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[3] tamanna\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string[4] purti\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The sorted data is \n",
+ "The data in cell [1] is ashita \n",
+ "The data in cell [2] is purti \n",
+ "The data in cell [3] is tamanna \n",
+ "The data in cell [4] is zaheer \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file