diff options
Diffstat (limited to 'Mastering_C/chapter7.ipynb')
-rw-r--r-- | Mastering_C/chapter7.ipynb | 2482 |
1 files changed, 2482 insertions, 0 deletions
diff --git a/Mastering_C/chapter7.ipynb b/Mastering_C/chapter7.ipynb new file mode 100644 index 00000000..e41cbc36 --- /dev/null +++ b/Mastering_C/chapter7.ipynb @@ -0,0 +1,2482 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:df255fa9a4e53028d13370030da5fb9293c0b07c60111131baf99b22bd751792" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7 : Array and Strings" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, page no. 221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0\n", + "a1 = int(raw_input(\"Age:\"))\n", + "sum += a1\n", + "a2 = int(raw_input(\"Age:\"))\n", + "sum += a2\n", + "a3 = int(raw_input(\"Age:\"))\n", + "sum += a3\n", + "a4 = int(raw_input(\"Age:\"))\n", + "sum += a4\n", + "a5 = int(raw_input(\"Age:\"))\n", + "sum += a5\n", + "print \"The ages that were input are:\"\n", + "print a1\n", + "print a2\n", + "print a3\n", + "print a4\n", + "print a5\n", + "print \"Average = %d\" %(sum/5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ages that were input are:\n", + "10\n", + "20\n", + "30\n", + "40\n", + "50\n", + "Average = 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2, page no. 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sum = 0.0\n", + "age = []\n", + "n = int(raw_input(\"Number of persons:\"))\n", + "if n<=0 or n>5:\n", + " print \"Invalid number of person entered\"\n", + "else:\n", + " for i in range(n):\n", + " age.append(int(raw_input(\"Age :\")))\n", + " sum += age[i]\n", + " print \"The ages input are:\"\n", + " for i in range (n):\n", + " print age[i]\n", + " print \"The average is :\",(sum/n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of persons:5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age :50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ages input are:\n", + "10\n", + "20\n", + "30\n", + "40\n", + "50\n", + "The average is : 30.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.1, page no. 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "large = max(a)\n", + "small = min(a)\n", + "print \"Largest element in vector is %8.2f\" %large\n", + "print \"Smallest element in vector is %8.2f\" %small" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-9.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "12.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-6.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "36.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Largest element in vector is 36.00\n", + "Smallest element in vector is -9.00\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.2, page no. 226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "for i in range (n-1):\n", + " for j in range(i+1,n):\n", + " if a[i]> a[j]:\n", + " a[i],a[j]=a[j],a[i]\n", + "print \"Vector elements in ascending order :\"\n", + "for i in range(n):\n", + " print \"%8.2f\" %a[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-2.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6.00\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements in ascending order :\n", + " -2.00\n", + " 1.00\n", + " 6.00\n", + " 7.00\n", + " 11.00\n", + " 20.00\n", + " 34.00\n", + " 91.00\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.3, page no. 227" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "n = int(raw_input(\"Size of vector ? \"))\n", + "print \"Vector elements ? \"\n", + "for i in range (n):\n", + " a.append(float(raw_input()))\n", + "item = float(raw_input(\"Element to be inserted ?\"))\n", + "pos = int(raw_input(\"Position of insertion ?\"))\n", + "a.insert(pos-1, item)\n", + "print \"Vector after insertion\"\n", + "for ele in a:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ? 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector elements ? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Element to be inserted ?10.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Position of insertion ?1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector after insertion\n", + "10.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example arrfunc.c, page no. 230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "rollno = 4\n", + "subject = 3\n", + "def display(studentmarks):\n", + " for r in range(rollno):\n", + " print \"\\nRoll no.: \", r+1,\n", + " for s in range(subject):\n", + " print \"%10.2f\" %studentmarks[r][s],\n", + "marks =[[35.5, 40.5, 45.5],\n", + " [50.5, 55.5, 60.5],\n", + " [65.0, 70.0, 75.5],\n", + " [80.0, 85.0, 90.0]]\n", + "display(marks)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Roll no.: 1 35.50 40.50 45.50 \n", + "Roll no.: 2 50.50 55.50 60.50 \n", + "Roll no.: 3 65.00 70.00 75.50 \n", + "Roll no.: 4 80.00 85.00 90.00\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.4, page no. 240" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "c = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input row & column of B matrix: \"\n", + "p = int(raw_input(\"row: \"))\n", + "q = int(raw_input(\"column: \"))\n", + "\n", + "if n == p and m == q:\n", + " print \"Matrices can be added\"\n", + " print \"Input A matrix: \",\n", + " for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = int(raw_input())\n", + " print \"\"\n", + " print \"Input B matrix: \",\n", + " for i in range(p):\n", + " for j in range(q):\n", + " b[i][j] = int(raw_input())\n", + " print \"\"\n", + " for i in range(p):\n", + " for j in range(q):\n", + " c[i][j] = a[i][j] + b[i][j]\n", + " print \"Sum of A & B matrices: \"\n", + " for i in range(n):\n", + " for j in range(m):\n", + " print \"%5d\" %c[i][j],\n", + " print \"\"\n", + "else:\n", + " print \"Matricess cannot be added\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of B matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrices can be added\n", + "Input A matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Input B matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Sum of A & B matrices: \n", + " 2 4 6 \n", + " 8 10 12 \n", + " 14 16 18 \n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.5, page no. 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "print \"Enter the order of A matrix: \",\n", + "n = int(raw_input())\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "trace = 0\n", + "for i in range(n):\n", + " trace += a[i][i]\n", + "print \"Trace = \", trace" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the order of A matrix: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Trace = 15.0\n" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.6, page no. 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "def read_mat(a, m, n):\n", + " for i in range(m):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "def write_mat(a, m, n):\n", + " for i in range(m):\n", + " for j in range(n):\n", + " print \"%d\" %a[i][j],\n", + " print \"\"\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "c = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input row & column of b matrix: \"\n", + "k = int(raw_input(\"row: \"))\n", + "q = int(raw_input(\"column: \"))\n", + "\n", + "if n == k:\n", + " print \"Matrices can be multiplied\"\n", + " print \"Resultant Matrix is %d x %d\" %(m, q)\n", + " print \"Input A matrix\"\n", + " read_mat(a, m, n)\n", + " print \"Input B matrix\"\n", + " read_mat(b, k, q)\n", + " for i in range(m):\n", + " for j in range(q):\n", + " c[i][j] = 0\n", + " for ip in range(n):\n", + " c[i][j] = c[i][j]+a[i][ip]*b[ip][j]\n", + " print \"Resultant of A and B matrices: \"\n", + " write_mat(c, m, q)\n", + "else:\n", + " print \"col of matrix A must be equal to row of matrix B\"\n", + " print \"Matrices cannot be multiplied\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of b matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrices can be multiplied\n", + "Resultant Matrix is 3 x 3\n", + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input B matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Resultant of A and B matrices: \n", + "30 36 42 \n", + "66 81 96 \n", + "102 126 150 \n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.7, page no. 235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "\n", + "print \"Input row & column of A matrix: \"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "\n", + "print \"Input A matrix: \"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = int(raw_input())\n", + "print \"Transpose of matrix A is\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print \"%5d\" %a[j][i],\n", + " print \"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transpose of matrix A is\n", + " 1 5 9 \n", + " 2 6 6 \n", + " 3 7 7 \n", + " 4 8 8 \n" + ] + } + ], + "prompt_number": 55 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.8, page no. 237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "strc = \"This is a string literal\"\n", + "print strc" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is a string literal\n" + ] + } + ], + "prompt_number": 57 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example starray.c, page no. 238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "names = [\"Tejaswi\",\n", + " \"Prasad\",\n", + " \"Prasanth\",\n", + " \"Prakash\",\n", + " \"Anand\",]\n", + "for name in names:\n", + " print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tejaswi\n", + "Prasad\n", + "Prasanth\n", + "Prakash\n", + "Anand\n" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example strcat.c, page no. 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "oneString = \"Fish\"\n", + "twoString = \"face\"\n", + "oneString += twoString\n", + "print oneString" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fishface\n" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.9, page no. 241" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = []\n", + "print \"Size of vector ?\"\n", + "n = int(raw_input())\n", + "num = n\n", + "print \"Vector Elements ?\"\n", + "for i in range(n):\n", + " a.append(float(raw_input()))\n", + "a = set(a)\n", + "a = list(a)\n", + "length = len(a)\n", + "print length\n", + "if length == n:\n", + " print \"No duplicates found\"\n", + "else:\n", + " print \"Vector has %d duplicates\" %(num - length)\n", + " print \"Vector after deleting duplicates: \",\n", + " for ele in a:\n", + " print ele," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of vector ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Vector Elements ?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n", + "Vector has 2 duplicates\n", + "Vector after deleting duplicates: 1.0 2.0 3.0 4.0\n" + ] + } + ], + "prompt_number": 73 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.10, page no. 242" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "grade = ['', '', '', '', '', '', '', '', '', '', '', '', '', '']\n", + "a = numpy.zeros((50,10))\n", + "print \"Number of students ?\",\n", + "n = int(raw_input())\n", + "for i in range(n):\n", + " sum = 0.0\n", + " print \"Enter 3 socres of student %d\" %(i+1)\n", + " for j in range(3):\n", + " a[i][j] = (float(raw_input()))\n", + " sum += a[i][j]\n", + " avg = sum/3\n", + " a[i][3] = avg\n", + " if avg < 30.0:\n", + " grade[i] = 'E'\n", + " elif avg < 60.0:\n", + " grade[i] = 'D'\n", + " elif avg < 75.0:\n", + " grade[i] = 'B'\n", + " else:\n", + " grade[i] = 'A'\n", + "print \"S1.no scores AVERAGE GRADE\"\n", + "print \"---------------------------------------------\"\n", + "for i in range(n):\n", + " print i+1,\n", + " for j in range(4):\n", + " print \"%8.2f\" %a[i][j],\n", + " print \"%6c\" %grade[i],\n", + " print \"\"\n", + " print \"---------------------------------------------\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of students ?" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter 3 socres of student 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "86\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "58\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "23.9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "97\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "73\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "56.8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "47.9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "62.0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 3 socres of student 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "35\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "40\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "S1.no scores AVERAGE GRADE\n", + "---------------------------------------------\n", + "1 67.00 86.00 58.00 70.33 B \n", + "---------------------------------------------\n", + "2 20.00 30.00 23.90 24.63 E \n", + "---------------------------------------------\n", + "3 80.00 97.00 73.00 83.33 A \n", + "---------------------------------------------\n", + "4 56.80 47.90 62.00 55.57 D \n", + "---------------------------------------------\n", + "5 45.00 35.00 40.00 40.00 D \n", + "---------------------------------------------\n" + ] + } + ], + "prompt_number": 85 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.11, page no. 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "sumsq = 0\n", + "sum = 0\n", + "print \"Calculation standard deviation of a\"\n", + "print \"Enter size of the list: \",\n", + "n = int(raw_input())\n", + "x = []\n", + "print \"Enter the %d items \" %n\n", + "for i in range(n):\n", + " x.append(float(raw_input()))\n", + " sum += x[i]\n", + "mean = sum/n\n", + "for i in range(n):\n", + " sumsq = sumsq + (mean-x[i]) * (mean-x[i])\n", + "variance = sumsq/n\n", + "sdn = math.sqrt(variance)\n", + "print \"Mean of %5d items : %10.6f\" %(n, mean)\n", + "print \"Variance : %10.6f\" %(variance)\n", + "print \"Standard Deviation : %10.6f\" %sdn" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Calculation standard deviation of a\n", + "Enter size of the list: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the 7 items \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "52\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mean of 7 items : 35.714286\n", + "Variance : 685.918367\n", + "Standard Deviation : 26.190043\n" + ] + } + ], + "prompt_number": 89 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.12, page no. 244" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "import math\n", + "\n", + "def nrm(a, n, m):\n", + " sum = 0.0\n", + " for i in range(n):\n", + " for j in range(m):\n", + " sum = sum + a[i][j] * a[i][j]\n", + " print \"Sum = %6.2f\" %sum\n", + " return math.sqrt(sum)\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "print \"Input row & column of A matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " a[i][j] = float(raw_input())\n", + "norm = nrm(a, n, m)\n", + "print \"Norm = %6.2f\" %norm" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input row & column of A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum = 285.00\n", + "Norm = 16.88\n" + ] + } + ], + "prompt_number": 90 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.13, page no. 245" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((5, 5))\n", + "flag = True\n", + "print \"Input size of matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "m = int(raw_input(\"column: \"))\n", + "print \"Enter the elements of the matrix: \"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " a[i][j] = float(raw_input())\n", + "for i in range(m):\n", + " min = a[i][0]\n", + " p = i\n", + " q = 0\n", + " for j in range(n):\n", + " if min > a[i][j]:\n", + " min = a[i][j]\n", + " p = i\n", + " q = j\n", + " for j in range(m):\n", + " if j != q:\n", + " if a[j][q] > a[p][q]:\n", + " flag = False\n", + " if flag:\n", + " print \"Saddle point a[%d][%d] = %d\" %(p+1, q+1, a[p][q])\n", + " else:\n", + " print \"No saddle is in row %d\" %(i+1)\n", + " flag = True" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input size of matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "column: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the elements of the matrix: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Saddle point a[1][2] = 5\n", + "Saddle point a[2][2] = 5\n", + "No saddle is in row 3\n" + ] + } + ], + "prompt_number": 91 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7.14, page no. 246" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy\n", + "\n", + "a = numpy.zeros((10, 10))\n", + "b = numpy.zeros((10, 10))\n", + "flag = None\n", + "print \"Input order of A matrix\"\n", + "n = int(raw_input(\"row: \"))\n", + "print \"Input A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " a[i][j] = int(raw_input())\n", + "for i in range(n):\n", + " for j in range(n):\n", + " b[i][j] = a[j][i]\n", + "print \"Transpose of A matrix\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " print \"%5d\" %b[i][j],\n", + " print \"\"\n", + "for i in range(n):\n", + " for j in range(n):\n", + " if a[i][j] != b[i][j]:\n", + " flag = True\n", + "if flag:\n", + " print \"Matrix is not symmetric\"\n", + "else:\n", + " print \"Matrix is symmetric\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input order of A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "row: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input A matrix\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transpose of A matrix\n", + " 1 3 \n", + " 2 4 \n", + "Matrix is not symmetric\n" + ] + } + ], + "prompt_number": 96 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |