{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 9: Functions in C"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1, Page Number: FNC-5"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def fact(k):\n",
      "    p = 1\n",
      "    for i in range(1,k+1):\n",
      "        p = p * i\n",
      "    return p\n",
      "\n",
      "n = int(raw_input(\"\\nEnter value to n and r : \"))\n",
      "r = int(raw_input(\"\"))\n",
      "\n",
      "ncr = fact(n)/ (fact(r)*fact(n-r))\n",
      "\n",
      "print \"\\nValue of nCr = \",ncr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter value to n and r : 5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Value of nCr =  10\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2, Page Number: FNC-6"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def big(a,b,c):\n",
      "    if a > b:\n",
      "        if a > c:\n",
      "            return a\n",
      "        else:\n",
      "            return c\n",
      "    else:\n",
      "        if b > c:\n",
      "            return b\n",
      "        else:\n",
      "            return c\n",
      "        \n",
      "t1 = int(raw_input(\"\\nEnter three test scores : \"))\n",
      "t2 = int(raw_input(\"\"))\n",
      "t3 = int(raw_input(\"\"))\n",
      "\n",
      "a1 = int(raw_input(\"\\nEnter three assignment scores : \"))\n",
      "a2 = int(raw_input(\"\"))\n",
      "a3 = int(raw_input(\"\"))\n",
      "\n",
      "total = big(t1,t2,t3) + big(a1,a2,a3)\n",
      "\n",
      "print \"\\nTotal marks = \",total"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter three test scores : 62\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "70\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "58\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter three assignment scores : 17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "21\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "23\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Total marks =  93\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 3, Page Number: FNC-8"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "def cosine(x):\n",
      "    x = x * 3.14/180\n",
      "    \n",
      "    s = 0\n",
      "    term = 1\n",
      "    i = 0\n",
      "    \n",
      "    for k in range(1,15+1):\n",
      "        s = s + term\n",
      "        term = term*x*x*(-1)/((i+1)*(i+2))\n",
      "        i = i + 2\n",
      "    return s\n",
      "\n",
      "x = 0\n",
      "print \"\\n------------------------------------\"\n",
      "print \"        x in degrees     cos(x)  \"\n",
      "print \"------------------------------------\"\n",
      "\n",
      "while x <= 180:\n",
      "    sys.stdout.write(\"\\n\\t%6.0f          %6.2f\"%(x,cosine(x)))\n",
      "    x = x + 30\n",
      "    \n",
      "print \"\\n------------------------------------\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "------------------------------------\n",
        "        x in degrees     cos(x)  \n",
        "------------------------------------\n",
        "\n",
        "\t     0            1.00\n",
        "\t    30            0.87\n",
        "\t    60            0.50\n",
        "\t    90            0.00\n",
        "\t   120           -0.50\n",
        "\t   150           -0.87\n",
        "\t   180           -1.00\n",
        "------------------------------------\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4, Page Number: FNC-9"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def gcd(x,y):\n",
      "    if x >= y:\n",
      "        nr = x\n",
      "        dr = y\n",
      "    else:\n",
      "        nr = y\n",
      "        dr = x\n",
      "    r = nr % dr\n",
      "    while r != 0:\n",
      "        nr = dr\n",
      "        dr = r\n",
      "        r = nr % dr\n",
      "        \n",
      "    return dr\n",
      "\n",
      "a = int(raw_input(\"\\nEnter three integers : \"))\n",
      "b = int(raw_input(\"\"))\n",
      "c = int(raw_input(\"\"))\n",
      "\n",
      "d1 = gcd(a,b)\n",
      "d2 = gcd(a,c)\n",
      "d3 = gcd(b,c)\n",
      "\n",
      "if d1 == d2:\n",
      "    if d1 == d3:\n",
      "        print \"\\nGreatest common divisor is \",d1\n",
      "    else:\n",
      "        print \"\\nGreatest common divisor is \",gcd(d1,d3)\n",
      "else:\n",
      "    print \"\\nGreatest common divisor is \",gcd(d1,d2)\n",
      "    \n",
      "print \"\\n\\nPress any key to continue...\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter three integers : 45\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "27\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "81\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Greatest common divisor is  9\n",
        "\n",
        "\n",
        "Press any key to continue...\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5, Page Number: FNC-10"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def reverse(n):\n",
      "    rn = 0\n",
      "    while n > 0:\n",
      "        r = n % 10\n",
      "        rn = rn * 10 + r\n",
      "        n = n / 10\n",
      "    return rn\n",
      "\n",
      "n = int(raw_input(\"\\nEnter the integer : \"))\n",
      "\n",
      "print \"\\n\",n,\" is erversed as \",reverse(n)\n",
      "print \"\\nPress any key to continue...\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the integer : 2846\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "2846  is erversed as  6482\n",
        "\n",
        "Press any key to continue...\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6, Page Number: FNC-11"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def compare(s1,s2):\n",
      "    if s1 == s2:\n",
      "        return 0\n",
      "    if s1 > s2:\n",
      "        return 1\n",
      "    if s1 < s2:\n",
      "        return -1\n",
      "    \n",
      "s1 = raw_input(\"\\nEnter the first string : \")\n",
      "s2 = raw_input(\"\\nEnter the second string : \")\n",
      "\n",
      "print \"\\nThe result is \",compare(s1,s2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the first string : MUMBAI\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the second string : MYSORE\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The result is  -1\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 7, Page Number: FNC-13"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "def amean(x,n):\n",
      "    s = 0\n",
      "    for i in range(0,n):\n",
      "        s = s + x[i]\n",
      "    return s/n\n",
      "\n",
      "n = int(raw_input(\"\\nHow many values? \"))\n",
      "print \"\\nEnter all values\\n\"\n",
      "\n",
      "x = [0.0 for i in range(0,10)]\n",
      "\n",
      "for i in range(0,n):\n",
      "    x[i] = float(raw_input(\"\"))\n",
      "    \n",
      "sys.stdout.write(\"\\nArithmetic mean = %6.2f\"%(amean(x,n)))"
     ],
     "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\n",
        "\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.62"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8, Page Number: FNC-14"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def elem_sum(a,m,n):\n",
      "    s = 0\n",
      "    for i in xrange(m):\n",
      "        for j in xrange(n):\n",
      "            s = s + a[i][j]\n",
      "           \n",
      "    return s\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,10)] for i in range(0,10)]\n",
      "\n",
      "print \"\\nEnter the matrix values\\n\"\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",
      "print \"\\nSum of all elements in the matrix = \",elem_sum(a,m,n)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "How many rows and columns : 3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the matrix values\n",
        "\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": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Sum of all elements in the matrix =  12\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 9, Page Number: FNC-15"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def reverse(st):\n",
      "    \n",
      "    i = len(st) - 1\n",
      "    rst = \"\"\n",
      "    while i >= 0:\n",
      "        rst = rst + st[i]\n",
      "        i = i -1\n",
      "        \n",
      "    return rst\n",
      "\n",
      "st = raw_input(\"\\nEnter a string : \")\n",
      "\n",
      "print \"\\n\",st,\" is reversed as \",reverse(st)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter a string : NEW DELHI\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "NEW DELHI  is reversed as  IHLED WEN\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 10, Page Number: FNC-16"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def bi_search(x,n,s):\n",
      "    flag = 0\n",
      "    start = 0\n",
      "    end = n\n",
      "    \n",
      "    while start < end and flag == 0:\n",
      "        mid = (start+end)/2\n",
      "        if x[mid] > s:\n",
      "            end = mid\n",
      "        else:\n",
      "            if x[mid] < s:\n",
      "                start = mid + 1\n",
      "            else:\n",
      "                flag = 1\n",
      "    return flag\n",
      "\n",
      "n = int(raw_input(\"\\nHow many numbers ? \"))\n",
      "\n",
      "print \"\\nEnter all numbers in the list\\n\"\n",
      "\n",
      "for i in range(0,n):\n",
      "    x[i] = int(raw_input(\"\"))\n",
      "    \n",
      "s = int(raw_input(\"\\nEnter the number to be searched : \"))\n",
      "\n",
      "if bi_search(x,n,s):\n",
      "    print \"\\nThe number \",s,\" is present in the list\"\n",
      "else:\n",
      "    print \"\\nThe number \",s,\" is not present in the list\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "How many numbers ? 10\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter all numbers in the list\n",
        "\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": [
        "13\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "19\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "21\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "25\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "26\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "29\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "35\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "42\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the number to be searched : 19\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The number  19  is present in the list\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 11, Page Number: FNC-19"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def fact(k):\n",
      "    if k == 1:\n",
      "        return 1\n",
      "    else:\n",
      "        return k*fact(k-1)\n",
      "    \n",
      "n = int(raw_input(\"\\nEnter values to n and r : \"))\n",
      "r = int(raw_input(\"\"))\n",
      "\n",
      "ncr = fact(n)/(fact(r)*fact(n-r))\n",
      "\n",
      "print \"\\nValue of nCr = \",ncr\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter values to n and r : 5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Value of nCr =  10\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 12, Page Number: FNC-20"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "def power(x,n):\n",
      "    if n == 1:\n",
      "        return x\n",
      "    else:\n",
      "        return x*power(x,n-1)\n",
      "    \n",
      "x = float(raw_input(\"\\nEnter value to x : \"))\n",
      "n = int(raw_input(\"\\nEnter its power : \"))\n",
      "\n",
      "sys.stdout.write(\"\\n%6.2f raise to %d is %6.2f\"%(x,n,power(x,n)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter value to x : 4.2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter its power : 3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "  4.20 raise to 3 is  74.09"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 13, Page Number: FNC-21"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "\n",
      "def fibo(t1,t2,count):\n",
      "    if count >= n:\n",
      "        return\n",
      "    else:\n",
      "        t3 = t1 + t2\n",
      "        sys.stdout.write(\"%5d\"%(t3))\n",
      "        count = count + 1\n",
      "        t1 = t2\n",
      "        t2 = t3\n",
      "        fibo(t1,t2,count)\n",
      "        \n",
      "n = int(raw_input(\"\\nHow many terms to be printed ? \"))\n",
      "t1 = 0\n",
      "t2 = 1\n",
      "\n",
      "print \"\\nThe first \",n,\" terms in Fibonacci series are\\n\"\n",
      "sys.stdout.write(\"%5d %5d\"%(t1,t2))\n",
      "\n",
      "count = 2\n",
      "fibo(t1,t2,count)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "How many terms to be printed ? 10\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The first  10  terms in Fibonacci series are\n",
        "\n",
        "    0     1    1    2    3    5    8   13   21   34"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 14, Page Number: FNC-28"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "def matadd():\n",
      "    for i in xrange(m):\n",
      "        for j in xrange(n):\n",
      "            c[i][j] = a[i][j] + b[i][j]\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,10)]for i in range(0,10)]\n",
      "b = [[0 for i in range(0,10)]for i in range(0,10)]\n",
      "c = [[0 for i in range(0,10)]for i in range(0,10)]\n",
      "\n",
      "#Read matrices\n",
      "print \"\\nEnter A 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",
      "print \"\\nEnter B matrix values\\n\"\n",
      "for i in range(0,m):\n",
      "    for j in range(0,n):\n",
      "        b[i][j] = int(raw_input(\"\"))\n",
      "\n",
      "matadd()\n",
      "\n",
      "print \"\\nResultant matrix is \\n\"\n",
      "for i in range(0,m):\n",
      "    for j in range(0,n):\n",
      "        sys.stdout.write(\"%5d\"%(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 values\n",
        "\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 values\n",
        "\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",
        "\n",
        "    8    0\n",
        "    4   -1\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 15, Page Number: FNC-29"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "def matmul():\n",
      "    for i in xrange(m):\n",
      "        for j in xrange(l):\n",
      "            c[i][j] = 0\n",
      "            for k in xrange(n):\n",
      "                c[i][j] = c[i][j] + a[i][k] * b[k][j]\n",
      "            \n",
      "m = int(raw_input(\"\\nEnter order of A matrix (m x n) : \"))\n",
      "n = int(raw_input(\"\"))\n",
      "\n",
      "\n",
      "a = [[0 for i in range(0,10)]for i in range(0,10)]\n",
      "b = [[0 for i in range(0,10)]for i in range(0,10)]\n",
      "c = [[0 for i in range(0,10)]for i in range(0,10)]\n",
      "\n",
      "#Read matrices\n",
      "print \"\\nEnter A 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",
      "n = int(raw_input(\"\\nEnter order of B matrix (n x l) : \"))\n",
      "l = int(raw_input(\"\"))\n",
      "\n",
      "print \"\\nEnter B matrix values\\n\"\n",
      "for i in range(0,n):\n",
      "    for j in range(0,l):\n",
      "        b[i][j] = int(raw_input(\"\"))\n",
      "\n",
      "matmul()\n",
      "\n",
      "print \"\\nResultant matrix is \\n\"\n",
      "for i in range(0,m):\n",
      "    for j in range(0,l):\n",
      "        sys.stdout.write(\"%5d\"%(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 (m x n) : 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter A matrix values\n",
        "\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 (n x l) : 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter B matrix values\n",
        "\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",
        "\n",
        "    4   14\n",
        "   16  -20\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 16, Page Number: FNC-31"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "at = [[0,0],[0,0],[0,0]]\n",
      "\n",
      "def transpose():\n",
      "    for i in xrange(m):\n",
      "        for j in xrange(n):\n",
      "            at[j][i] = a[i][j] \n",
      "            \n",
      "m = int(raw_input(\"\\nHow many rows and columns ?\"))\n",
      "n = int(raw_input(\"\"))\n",
      "\n",
      "print \"\\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",
      "transpose()\n",
      "\n",
      "print \"\\nTranspose of the matrix is \\n\"\n",
      "for i in xrange(n):\n",
      "    for j in xrange(m):\n",
      "        sys.stdout.write(\"%5d\"%(at[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": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the matrix values\n",
        "\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",
        "Transpose of the matrix is \n",
        "\n",
        "   -3    3\n",
        "    6    2\n",
        "    0    8\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 17, Page Number: FNC-32"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "\n",
      "def sort():\n",
      "    for i in xrange(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",
      "x = [0 for i in range(0,50)]\n",
      "n = int(raw_input(\"\\nHow many numbers ? \"))\n",
      "print \"\\nEnter the list of values\\n\"\n",
      "\n",
      "for i in range(0,n):\n",
      "    x[i] = int(raw_input(\"\"))\n",
      "\n",
      "sort()\n",
      "\n",
      "print \"\\nThe sorted list is\\n\"\n",
      "\n",
      "for i in xrange(n):\n",
      "    sys.stdout.write(\"%5d\"%(x[i]))"
     ],
     "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 the list of values\n",
        "\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",
        "The sorted list is\n",
        "\n",
        "  -10    5   20   32"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}