{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Arrays and Subscripted Variables" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1, Page Number : ASV-4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"\\nHow many integers ? \"))\n", "x = [0 for i in range(0,n)]\n", "\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\\nEnter the %dth value : \"%(i+1)))\n", " \n", "#Processing\n", "#sum is a predefined function in python. so sum1 is used\n", "sum1 = 0\n", "for i in range(0,n):\n", " sum1 = sum1 + x[i]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nSum of all integers = %d\"%(sum1))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many integers ? 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the 1th value : 36\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the 2th value : 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the 3th value : 52\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the 4th value : 44\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the 5th value : 62\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of all integers = 239" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, Page Number : ASV-5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable initialization\n", "n = int(raw_input(\"\\nHow many numbers ? \"))\n", "sys.stdout.write(\"\\nEnter all those numbers \")\n", "x = [0 for i in range(0,n)]\n", "\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\"))\n", " \n", "#Processing\n", "big = x[0]\n", "for i in range(0,n):\n", " if x[i] > big:\n", " big = x[i]\n", " \n", "#Output\n", "sys.stdout.write(\"\\n%d is the biggest number\"%(big))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many numbers ? 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all those numbers " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-228\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "185\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "36\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "185 is the biggest number" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, Page Number : ASV-6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import math\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"\\nHow many values ? \"))\n", "x = [0.0 for i in range(0,n)]\n", "\n", "sys.stdout.write(\"\\nEnter all values in the list\\n\")\n", "for i in range(0,n):\n", " x[i] = float(raw_input(\"\"))\n", " \n", "#Processing\n", "sum1 = 0\n", "#Since sum is a predefined function in python, sum1 is used\n", "for i in range(0,n):\n", " sum1 = sum1 + x[i]\n", "xbar = sum1 / n\n", "vsum = 0\n", "\n", "for i in range(0,n):\n", " vsum = vsum + (x[i] - xbar) * (x[i] - xbar)\n", "sigmax = vsum / n\n", "sd = math.sqrt(sigmax)\n", "\n", "#Output\n", "sys.stdout.write(\"\\nArithmetic mean = %0.3f\"%(xbar))\n", "sys.stdout.write(\"\\nVariance = %0.3f\"%(sigmax))\n", "sys.stdout.write(\"\\nStandard deviation = %0.3f\"%(sd))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many values ? 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all values in the list\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Arithmetic mean = 3.617\n", "Variance = 0.088\n", "Standard deviation = 0.297" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4, Page Number : ASV-8" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"\\nHow many students ? \"))\n", "\n", "sys.stdout.write(\"\\nEnter all the marks \\n\")\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\"))\n", " \n", "#Processing\n", "sum1 = 0.0\n", "#Since sum is a predefined function in python, sum1 is used\n", "for i in range(0,n):\n", " sum1 = sum1 + x[i]\n", " \n", "mean = float(sum1/n)\n", "\n", "#Output\n", "sys.stdout.write(\"\\nMean = %6.2f\"%(mean))\n", "sys.stdout.write(\"\\n\\nMarks greater than mean : \")\n", "\n", "for i in range(0,n):\n", " if x[i] > mean:\n", " sys.stdout.write(\"%5d\"%(x[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many students ? 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all the marks \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "58\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "63\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "68\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "54\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "48\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Mean = 58.20\n", "\n", "Marks greater than mean : 63 68" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5, Page Number : ASV-9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import math\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"\\nHow many values ? \"))\n", "\n", "sys.stdout.write(\"\\nEnter all values in the list \\n\")\n", "for i in range(0,n):\n", " x[i] = float(raw_input(\"\"))\n", " \n", "psum = 0\n", "nsum = 0\n", "\n", "#Processing\n", "for i in range(0,n):\n", " if x[i] > 0:\n", " psum = psum + x[i]\n", " else:\n", " nsum = nsum + x[i]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nSum of positive values = %0.2f\"%(psum))\n", "sys.stdout.write(\"\\nSum of negative values = %0.2f\"%(nsum))\n", "if psum > math.fabs(nsum):\n", " sys.stdout.write(\"\\nPositive sum is greater in magnitude\")\n", "else:\n", " sys.stdout.write(\"\\nNegative sum is greater in magnitude\")\n", " \n", "diff = math.fabs(psum - math.fabs(nsum))\n", "sys.stdout.write(\"\\nDifference in magnitude = %0.2f\"%(diff))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many values ? 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all values in the list \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-16\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\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" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6, Page Number : ASV-11" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"How many numbers ? \"))\n", "\n", "sys.stdout.write(\"\\nEnter the list of %d numbers\\n\"%(n))\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\"))\n", " \n", "#Processing\n", "for i in range(0,n):\n", " for j in range(i+1,n):\n", " if x[i] > x[j]:\n", " temp = x[i]\n", " x[i] = x[j]\n", " x[j] = temp\n", " \n", "#Output\n", "sys.stdout.write(\"\\nNumbers in ascending order\\n\")\n", "for i in range(0,n):\n", " sys.stdout.write(\"%5d\"%(x[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many numbers ? 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the list of 4 numbers\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "32\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Numbers in ascending order\n", " -10 5 20 32" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, Page Number : ASV-13" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "x = [0 for i in range(0,50)]\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"\\nHow many values in the list ? \"))\n", "\n", "sys.stdout.write(\"\\nEnter all values in the list\\n\")\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\"))\n", "s = int(raw_input(\"\\nEnter the key value to be searched : \"))\n", "\n", "#Processing\n", "for i in range(0,n):\n", " if s == x[i]:\n", " sys.stdout.write(\"\\n%d is available in %dth location\"%(s,i+1))\n", " \n", "if i == n+1:\n", " sys.stdout.write(\"\\nThe key value %d is not present in the list\"%(s))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many values in the list ? 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all values in the list\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "13\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the key value to be searched : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "3 is available in 4th location" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8, Page Number : ASV-15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "exchng = 0\n", "n = int(raw_input(\"\\nHow many numbers ? \"))\n", "sys.stdout.write(\"\\nEnter all numbers in the list\\n\")\n", "for i in range(0,n):\n", " x[i] = int(raw_input(\"\"))\n", "\n", "#Processing\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", "#Output\n", "sys.stdout.write(\"\\nThe sorted list is\\n\")\n", "for i in range(0,n):\n", " sys.stdout.write(\"%5d\"%(x[i]))\n", " \n", "sys.stdout.write(\"\\nSorted in %d passes and %d exchanges\"%(n-1,exchng))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many numbers ? 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all numbers in the list\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The sorted list is\n", " -2 3 6 8\n", "Sorted in 3 passes and 3 exchanges" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9, Page Number : ASV-17" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable declaration and initialization\n", "\n", "m = int(raw_input(\"\\nHow many rows and columns ? \"))\n", "n = int(raw_input(\"\"))\n", "\n", "a = [[0 for i in range(0,m)] for j in range(0,n)]\n", "b = [[0 for i in range(0,m)] for j in range(0,n)]\n", "c = [[0 for i in range(0,m)] for j in range(0,n)]\n", "\n", "sys.stdout.write(\"\\nEnter A matrix\\n\")\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " a[i][j] = int(raw_input(\"\"))\n", " \n", "sys.stdout.write(\"\\nEnter B matrix\\n\")\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " b[i][j] = int(raw_input(\"\"))\n", " \n", "#Processing\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " c[i][j] = a[i][j] + b[i][j]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nResultant matrix is \\n\")\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " sys.stdout.write(\"%6d\"%(c[i][j]))\n", " sys.stdout.write(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many rows and columns ? 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter A matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter B matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Resultant matrix is \n", " 8 0\n", " 4 -1\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10, Page Number : ASV-18" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable declaration and initialization\n", "\n", "m = int(raw_input(\"\\nEnter order of A matrix : \"))\n", "n = int(raw_input(\"\"))\n", "\n", "a = [[0 for i in range(0,m)] for j in range(0,n)]\n", "b = [[0 for i in range(0,m)] for j in range(0,n)]\n", "c = [[0 for i in range(0,m)] for j in range(0,n)]\n", "\n", "sys.stdout.write(\"\\nEnter A matrix\\n\")\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " a[i][j] = int(raw_input(\"\"))\n", "\n", "n = int(raw_input(\"\\nEnter order of B matrix : \"))\n", "l = int(raw_input(\"\"))\n", "\n", "sys.stdout.write(\"\\nEnter B matrix\\n\")\n", "for i in range(0,n):\n", " for j in range(0,l):\n", " b[i][j] = int(raw_input(\"\"))\n", " \n", "#Processing\n", "for i in range(0,n):\n", " for j in range(0,l):\n", " c[i][j] = 0\n", " for k in range(0,n):\n", " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nResultant matrix is \\n\")\n", "for i in range(0,m):\n", " for j in range(0,l):\n", " sys.stdout.write(\"%6d\"%(c[i][j]))\n", " sys.stdout.write(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter order of A matrix : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter A matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter order of B matrix : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter B matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Resultant matrix is \n", " 4 14\n", " 16 -20\n" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11, Page Number : ASV-20" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable declaration and initialization\n", "\n", "m = int(raw_input(\"\\nEnter order of the matrix : \"))\n", "n = int(raw_input(\"\"))\n", "\n", "a = [[0 for i in range(0,10)] for j in range(0,10)]\n", "at = [[0 for i in range(0,10)] for j in range(0,10)]\n", "\n", "sys.stdout.write(\"\\nEnter the matrix values\\n\")\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " a[i][j] = int(raw_input(\"\"))\n", "\n", "#Processing\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " at[j][i] = a[i][j]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nThe transposed matrix is\\n\")\n", "for i in range(0,n):\n", " for j in range(0,m):\n", " sys.stdout.write(\"%6d\"%(at[i][j]))\n", " sys.stdout.write(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter order of the matrix : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the matrix values\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The transposed matrix is\n", " -3 3\n", " 6 2\n", " 0 8\n" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 12, Page Number : ASV-21" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable declaration and initialization\n", "\n", "m = int(raw_input(\"\\nEnter order of the square matrix : \"))\n", "\n", "a = [[0 for i in range(0,10)] for j in range(0,10)]\n", "\n", "sys.stdout.write(\"\\nEnter the matrix\\n\")\n", "for i in range(0,m):\n", " for j in range(0,m):\n", " a[i][j] = int(raw_input(\"\"))\n", "\n", "#Processing\n", "flag = 0\n", "for i in range(0,m):\n", " if flag == 0:\n", " for j in range(0,m):\n", " if flag == 0:\n", " if a[i][j] == a[j][i]:\n", " continue\n", " else:\n", " flag = 1\n", "\n", "#Output\n", "if flag == 0:\n", " sys.stdout.write(\"\\nThe given matrix is a symmetric matrix\")\n", "else:\n", " sys.stdout.write(\"\\nThe given matrix is not a symmetric matrix\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter order of the square matrix : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\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": [ "-7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The given matrix is a symmetric matrix" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13, Page Number : ASV-23" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable declaration and initialization\n", "\n", "m = int(raw_input(\"\\nEnter order of the square matrix : \"))\n", "\n", "a = [[0 for i in range(0,10)] for j in range(0,10)]\n", "\n", "sys.stdout.write(\"\\nEnter the matrix\\n\")\n", "for i in range(0,m):\n", " for j in range(0,m):\n", " a[i][j] = int(raw_input(\"\"))\n", "\n", "#Processing \n", "sum1 = 0\n", "#since sum is a predefined function in python, sum1 is used\n", "for i in range(0,m):\n", " sum1 = sum1 + a[i][i]\n", " \n", "#Output\n", "sys.stdout.write(\"\\nTrace of the matrix = %d\"%(sum1))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter order of the square matrix : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the matrix\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\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": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\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": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Trace of the matrix = 6" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }