diff options
Diffstat (limited to 'Computer_Programming_Theory_and_Practice_/Chapter6.ipynb')
-rwxr-xr-x | Computer_Programming_Theory_and_Practice_/Chapter6.ipynb | 794 |
1 files changed, 0 insertions, 794 deletions
diff --git a/Computer_Programming_Theory_and_Practice_/Chapter6.ipynb b/Computer_Programming_Theory_and_Practice_/Chapter6.ipynb deleted file mode 100755 index 14969fc1..00000000 --- a/Computer_Programming_Theory_and_Practice_/Chapter6.ipynb +++ /dev/null @@ -1,794 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:aced1a8512bbd1c950b44a0ce98d7bb529f62e617c89b1c73eec08ce49b27311" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Chapter 6: Arrays and Subscribed Variables" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 1 , Page number: CP-122" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find the sum of n numbers using array\n", - "# Variable declaration\n", - "\n", - "n = 5\n", - "x = [36,45,52,44,62]\n", - "\n", - "print \"How many integers ?\",n\n", - "print \"Enter the 1th value :\",x[0]\n", - "print \"Enter the 2th value :\",x[1]\n", - "print \"Enter the 3th value :\",x[2]\n", - "print \"Enter the 4th value :\",x[3]\n", - "print \"Enter the 5th value :\",x[4]\n", - "\n", - "summ = 0 \n", - "\n", - "# Calculation of sum of numbers\n", - "for i in x:\n", - " summ = summ + i\n", - " \n", - "\n", - "print \"Sum of all integers =\",summ\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many integers ? 5\n", - "Enter the 1th value : 36\n", - "Enter the 2th value : 45\n", - "Enter the 3th value : 52\n", - "Enter the 4th value : 44\n", - "Enter the 5th value : 62\n", - "Sum of all integers = 239\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 2 , Page number: CP-123" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find the biggest of n numbers\n", - "# Variable declaration\n", - "\n", - "n = 5\n", - "x = [25,-228,0,185,36]\n", - "\n", - "print \"How many numbers ?\",n\n", - "print \"Enter all those numbers\"\n", - "for a in x:\n", - " print a,\n", - "print \n", - "\n", - "big = x[0]\n", - "\n", - "\n", - "for i in x:\n", - " if (i > big):\n", - " big = i\n", - "\n", - "\n", - "print \"%d is the biggest number\" % big " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers ? 5\n", - "Enter all those numbers\n", - "25 -228 0 185 36\n", - "185 is the biggest number\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 3 , Page number: Cp-124" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find the arithmetic mean, variance and standard deviation\n", - "# Variable declaration\n", - "\n", - "n = 6\n", - "x = [3.1,3.8,3.6,4.0,3.4,3.8]\n", - "summ = 0\n", - "vsum = 0\n", - "\n", - "print \"How many values ?\",n\n", - "print \"Enter all values in the list \"\n", - "for a in x:\n", - " print a,\n", - "print \n", - "\n", - "# Loop to find sum of all values\n", - "\n", - "for i in x:\n", - " summ = summ + i\n", - "\n", - "xbar = summ / n\n", - "\n", - "# Loop to find the numerator vsum to find variance\n", - "\n", - "for i in x:\n", - " vsum = vsum + (i - xbar) * (i - xbar)\n", - "\n", - "\n", - "sigmax = vsum / n\n", - "sd = sigmax ** 0.5\n", - "\n", - "print \"Arithmetic mean = %0.3f\" % xbar\n", - "print \"Variance = %0.3f \" % sigmax\n", - "print \"Standard deviation = %0.3f\" % sd" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many values ? 6\n", - "Enter all values in the list \n", - "3.1 3.8 3.6 4.0 3.4 3.8\n", - "Arithmetic mean = 3.617\n", - "Variance = 0.088 \n", - "Standard deviation = 0.297\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 4 , Page number: CP-126" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to calculate mean of marks and print list of marks greater than mean\n", - "# Variable declaration\n", - "\n", - "n = 5\n", - "x = [58,63,68,54,48]\n", - "summ = 0 # Used summ instead of sum since it was a inbuilt function\n", - "i = 0\n", - "\n", - "print \"How many students ?\",n\n", - "print \"Enter all the marks \"\n", - "for a in x:\n", - " print a,\n", - "print \n", - "\n", - "for i in x:\n", - " summ = summ + i\n", - "\n", - "mean = float(summ) / n\n", - "\n", - "print \"Mean = %0.2f\" % mean\n", - "print \"Marks greater than mean :\",\n", - "\n", - "i = 0\n", - "for i in x:\n", - " if (i > mean):\n", - " print i,\n", - "\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many students ? 5\n", - "Enter all the marks \n", - "58 63 68 54 48\n", - "Mean = 58.20\n", - "Marks greater than mean : 63 68\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 5 , Page number: CP-127" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find sum of all positive and negative numbers and to find out which is larger in magnitude\n", - "# Variable declaration\n", - "import math\n", - "\n", - "n = 6\n", - "x = [8,-12,-16,12,-9,5]\n", - "psum = 0\n", - "nsum = 0\n", - "\n", - "print \"How many values ?\",n\n", - "print \"Enter all values in the list\"\n", - "for i in x:\n", - " print i,\n", - "print \n", - "\n", - "# Loop to calculate sum of positive and negative values\n", - "\n", - "for i in x:\n", - " if i > 0:\n", - " psum = psum + i\n", - " else:\n", - " nsum = nsum + i\n", - "\n", - "print \"Sum of positive values = %0.2f\" % psum\n", - "print \"Sum of negative values = %0.2f\" % nsum\n", - "\n", - "if (psum > abs(nsum)):\n", - " print \"Positive sum is greater in magnitude\"\n", - "else:\n", - " print \"Negative sum is greater in magnitude\"\n", - "\n", - "diff = abs(psum) - abs(nsum)\n", - "print \"Difference in magnitude = %0.2f\" % abs(diff)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many values ? 6\n", - "Enter all values in the list\n", - "8 -12 -16 12 -9 5\n", - "Sum of positive values = 25.00\n", - "Sum of negative values = -37.00\n", - "Negative sum is greater in magnitude\n", - "Difference in magnitude = 12.00\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 6 , Page number: CP-129" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to sort n numbers in ascending order\n", - "# Variable declaration\n", - "\n", - "n = 4\n", - "x = [32,-10,20,5]\n", - "i = 0\n", - "\n", - "\n", - "print \"How many numbers ?\",n\n", - "print \"Enter the list of 4 numbers\"\n", - "for a in x:\n", - " print a,\n", - "print\n", - "\n", - "# Loop to arrange the numbers in ascending order\n", - "\n", - "while i < n-1:\n", - " j = i + 1\n", - " while j < n:\n", - " if x[i] > x[j]:\n", - " temp = x[i]\n", - " x[i] = x[j]\n", - " x[j] = temp\n", - " j = j + 1\n", - " i = i + 1\n", - "\n", - "print \"Numbers in ascending order \"\n", - "\n", - "for a in x:\n", - " print a,\n" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers ? 4\n", - "Enter the list of 4 numbers\n", - "32 -10 20 5\n", - "Numbers in ascending order \n", - "-10 5 20 32\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 7 , Page number: CP-131" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to search the key value and to print it if the search is successful\n", - "# Variable declaration\n", - "\n", - "n = 6\n", - "x = [6,-2,8,3,13,10]\n", - "s = 3\n", - "\n", - "print \"How many values in the list ?\",n\n", - "print \"Enter all values in the list\"\n", - "for i in x:\n", - " print i,\n", - "print \n", - "print \"Enter the key value to be searched :\",s\n", - "\n", - "# loop to search key value in the list\n", - "\n", - "for i in range(n):\n", - " if s == x[i]:\n", - " print s,\" is available in\",i+1,\"th location\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many values in the list ? 6\n", - "Enter all values in the list\n", - "6 -2 8 3 13 10\n", - "Enter the key value to be searched : 3\n", - "3 is available in 4 th location\n" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 8 , Page number: CP-133" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to sort n numbers using bubble sort and find number of exchanges and passes\n", - "# Variable declaration\n", - "\n", - "n = 4\n", - "x = [6,-2,8,3]\n", - "exchng = 0\n", - "\n", - "print \"How many numbers?\",n\n", - "print \"Enter all the numbers in the list\"\n", - "for i in x:\n", - " print i,\n", - "\n", - "print\n", - "\n", - "for i in range(0,n-1):\n", - " for j in range(0,n-i-1):\n", - " if x[j] > x[j+1]:\n", - " temp = x[j]\n", - " x[j] = x[j+1]\n", - " x[j+1] = temp\n", - " exchng = exchng + 1\n", - " \n", - "\n", - "print \"The sorted list is\"\n", - "for i in x:\n", - " print i,\n", - "\n", - "print \n", - "\n", - "print \"Sorted in\",n-1,\"passes and\",exchng,\"exchanges\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many numbers? 4\n", - "Enter all the numbers in the list\n", - "6 -2 8 3\n", - "The sorted list is\n", - "-2 3 6 8\n", - "Sorted in 3 passes and 3 exchanges\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 9 , Page number: CP-135" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to add two matrices\n", - "# Variable declaration\n", - "\n", - "a = [[2,-2],\n", - " [0,4]]\n", - "b = [[6,2],\n", - " [4,-5]]\n", - "\n", - "c = [[0,0],\n", - " [0,0]]\n", - "\n", - "m = 2\n", - "n = 2\n", - "\n", - "print \"How many rows and columns ?\",m,n\n", - "print \"Enter A matrix\"\n", - "for i in a:\n", - " print i\n", - "\n", - "print \"Enter B matrix\"\n", - "for j in b:\n", - " print j\n", - "\n", - "# Loop to add two matrices\n", - "\n", - "for i in range(m):\n", - " for j in range(n):\n", - " c[i][j] = a[i][j] + b[i][j]\n", - "\n", - "\n", - "print \"Resultant matrix is\"\n", - "for ci in c:\n", - " print ci" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "How many rows and columns ? 2 2\n", - "Enter A matrix\n", - "[2, -2]\n", - "[0, 4]\n", - "Enter B matrix\n", - "[6, 2]\n", - "[4, -5]\n", - "Resultant matrix is\n", - "[8, 0]\n", - "[4, -1]\n" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 10 , Page number: CP-136" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to multiply two matrices\n", - "# Variable declaration\n", - "\n", - "m = 2\n", - "n = 2\n", - "l = 2\n", - "a = [[2,-2],\n", - " [0,4]]\n", - "b = [[6,2],\n", - " [4,-5]]\n", - "c = [[0,0],\n", - " [0,0]]\n", - "\n", - "print \"Enter order of A matrix :\",m,n\n", - "print \"Enter A matrix\"\n", - "for i in a:\n", - " print i\n", - "print \"Enter order of B matrix :\",m,n\n", - "print \"Enter B matrix\"\n", - "for j in b:\n", - " print j\n", - "\n", - "# Loop to multiply two matrices\n", - "# iterate through rowa of A\n", - "for i in range(m):\n", - " # iterate through columns of B\n", - " for j in range(l):\n", - " c[i][j] = 0\n", - " # iterate through rows of B\n", - " for k in range(n):\n", - " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", - "\n", - "\n", - "print \"Resultant matrix is\"\n", - "\n", - "for i in c:\n", - " print i\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of A matrix : 2 2\n", - "Enter A matrix\n", - "[2, -2]\n", - "[0, 4]\n", - "Enter order of B matrix : 2 2\n", - "Enter B matrix\n", - "[6, 2]\n", - "[4, -5]\n", - "Resultant matrix is\n", - "[4, 14]\n", - "[16, -20]\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 11 , Page number: CP-138" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find and print the transpose of the matrix\n", - "# Variable declaration\n", - "\n", - "m = 2\n", - "n = 3\n", - "a = [[-3,6,0],\n", - " [3,2,8]]\n", - "at = [[0,0],\n", - " [0,0],\n", - " [0,0]]\n", - "\n", - "print \"Enter order of the matrix :\",m,n\n", - "print \"Enter the matrix values\"\n", - "for i in a :\n", - " print i\n", - "\n", - "# Loop to calculate transpose\n", - "\n", - "for i in range(m):\n", - " for j in range(n):\n", - " at[j][i] = a[i][j]\n", - "\n", - "print \"The transposed matrix is \"\n", - "for i in at:\n", - " print i\n", - " " - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of the matrix : 2 3\n", - "Enter the matrix values\n", - "[-3, 6, 0]\n", - "[3, 2, 8]\n", - "The transposed matrix is \n", - "[-3, 3]\n", - "[6, 2]\n", - "[0, 8]\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 12 ,Page number: CP-139" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to check whether a given matrix is symmetric or not\n", - "# Variable declaration\n", - "\n", - "m = 3\n", - "a = [[5,3,8],\n", - " [3,1,-7],\n", - " [8,-7,4]]\n", - "\n", - "print \"Enter order of the square matrix :\",m\n", - "for i in a:\n", - " print i\n", - "\n", - "# Loop to check whether symmetric or not\n", - "\n", - "for i in range(m):\n", - " flag = 0\n", - " for j in range(m):\n", - " flag = 0\n", - " if a[i][j] == a[j][i]:\n", - " continue\n", - " else:\n", - " flag = 1\n", - "\n", - "if flag == 0:\n", - " print \"The given matrix is a symmetric matrix\"\n", - "else:\n", - " print \"The given matrix is not a symmetric matrix\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter order of the square matrix : 3\n", - "[5, 3, 8]\n", - "[3, 1, -7]\n", - "[8, -7, 4]\n", - "The given matrix is a symmetric matrix\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "Example 13 , Page number: CP-141" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "# Program to find the trace of a given square matrix\n", - "# Variable dclaration\n", - "\n", - "m = 3\n", - "a = [[3,2,-1],\n", - " [4,1,8],\n", - " [6,4,2]]\n", - "summ = 0\n", - "\n", - "print \"Enter order of the square matrix :\",m\n", - "print \"Enter the matrix\"\n", - "for i in a:\n", - " print i\n", - "\n", - "# Loop to find trace\n", - "for i in range(m):\n", - " summ = summ + a[i][i]\n", - "\n", - "print \"Trace of the matrix =\",summ " - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}
\ No newline at end of file |