summaryrefslogtreecommitdiff
path: root/Fundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Fundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb')
-rwxr-xr-xFundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb987
1 files changed, 987 insertions, 0 deletions
diff --git a/Fundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb b/Fundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb
new file mode 100755
index 00000000..fa4db685
--- /dev/null
+++ b/Fundamental_of_Computing_and_Programming_in_C/Chapter05.ipynb
@@ -0,0 +1,987 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:8b48219cd7d973f69d80d4d491de4bdba5cb9c73135095697b1625a862f6bc97"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 05 : Defining and Processing of Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 1, Page Number: 5.5"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to read 5 data items from input and store them in an array and display their sum\n",
+ "\n",
+ "#Local definitions\n",
+ "a = []\n",
+ "sum = 0\n",
+ "\n",
+ "for i in range(0,5):\n",
+ " a.append(0)\n",
+ "print \"Enter 5 integer numbers\" \n",
+ "for i in range(0,5):\n",
+ " a[i] = eval(raw_input())\n",
+ " \n",
+ "for i in range(0,5): \n",
+ " sum = sum + a[i] \n",
+ " \n",
+ "print \"The sum of given numbers is : %d\\n\" %sum"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 5 integer numbers\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "120\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "69\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "390\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "45\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of given numbers is : 669\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 2 Page No. 5.7"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to accept 5 numbers and print whether the number is even or not\n",
+ "\n",
+ "#Local Variable\n",
+ "a=[]\n",
+ "\n",
+ "for i in range(5):\n",
+ " a.append(0)\n",
+ "for i in range(0,5):\n",
+ " i+1\n",
+ " a[i]=eval(raw_input(\"Enter the %d number: \"%(i+1))) \n",
+ " print \"\\n\"\n",
+ "for i in range (0,5):\n",
+ " if a[i]%2==0:\n",
+ " print \"%d Number is Even\"%a[i]\n",
+ " else:\n",
+ " print \"%d Number is Odd\"%a[i]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 1 number: 17\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 2 number: 38\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 3 number: 79\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 4 number: 80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 5 number: 76\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "17 Number is Odd\n",
+ "38 Number is Even\n",
+ "79 Number is Odd\n",
+ "80 Number is Even\n",
+ "76 Number is Even\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 3 Page No. 5.8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to illustrate how a character array can be used.\n",
+ "\n",
+ "#Local definition\n",
+ "word=['F', 'R', 'I', 'E', 'N', 'D', 'S']\n",
+ "\n",
+ "for i in range(0,7):\n",
+ " word.append(0)\n",
+ "for i in range(0,7):\n",
+ " print word[i],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "F R I E N D S\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 4, Page Number: 5.8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to sort the given strings alphabetically\n",
+ "\n",
+ "b = raw_input(\"Enter the text to sort : \")\n",
+ "print \"Sorted Text are : \",''.join(sorted(b, key=lambda v: (v.upper(), v[0].islower())))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the text to sort : VRBPublishers\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sorted Text are : BbehilPRrssuV\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 5, Page Number: 5.10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using two dimension array\n",
+ "\n",
+ "#Local definition \n",
+ "stud = [[0 for i in xrange(0,4)] for i in xrange(0,4)] #stud is a array name with 4 rows and 2 columns\n",
+ "\n",
+ "for i in range(0,4):\n",
+ " print \"Enter the %d Student roll no and Mark:\" %i\n",
+ " stud[i][1], stud[i][2] = input()\n",
+ " \n",
+ "for i in range(0,4):\n",
+ " print \"%d Student roll no %d mark %d\" %(i, stud[i][1], stud[i][2])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 0 Student roll no and Mark:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3977,80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 1 Student roll no and Mark:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "17776,95\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 2 Student roll no and Mark:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6682,82\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the 3 Student roll no and Mark:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6683,85\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 Student roll no 3977 mark 80\n",
+ "1 Student roll no 17776 mark 95\n",
+ "2 Student roll no 6682 mark 82\n",
+ "3 Student roll no 6683 mark 85\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 6, Page Number: 5.12"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to illustrate Matrix Multiplication\n",
+ "\n",
+ "a = [[0 for i in xrange(0,5)] for i in xrange(0,5)]\n",
+ "b = [[0 for i in xrange(0,5)] for i in xrange(0,5)]\n",
+ "c = [[0 for i in xrange(0,5)] for i in xrange(0,5)]\n",
+ "\n",
+ "while True:\n",
+ " r1, c1 = input(\"Enter the size of the matrix A....\")\n",
+ " r2, c2 = input(\"Enter the size of the matrix B....\")\n",
+ " if c1 == r2:\n",
+ " print \"Enter matrix A elements...\"\n",
+ " for i in range (0,r1):\n",
+ " for j in range (0,c1):\n",
+ " a[i][j] = input()\n",
+ " \n",
+ " print \"Enter matrix B elements...\"\n",
+ " for i in range (0,r2):\n",
+ " for j in range (0,c2):\n",
+ " b[i][j] = input()\n",
+ " \n",
+ " for i in range(0,r1):\n",
+ " for j in range(0,c1):\n",
+ " c[i][j] = 0\n",
+ " for k in range(0,c1):\n",
+ " c[i][j] += a[i][k] * b[k][j]\n",
+ " \n",
+ " print \"The resultant matrix is....\"\n",
+ " for i in range(0,r1):\n",
+ " for j in range(0,c1):\n",
+ " print \"%d\\t\" %c[i][j],\n",
+ " print \"\\n\"\n",
+ " break \n",
+ " else:\n",
+ " print \"Multiplication is not possible\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of the matrix A....3,3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of the matrix B....2,2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Multiplication is not possible\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of the matrix A....3,3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of the matrix B....3,3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter matrix A elements...\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": [
+ "2\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": [
+ "2\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": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter matrix B elements...\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\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": [
+ "The resultant matrix is....\n",
+ "18\t18\t18\t\n",
+ "\n",
+ "18\t18\t18\t\n",
+ "\n",
+ "18\t18\t18\t\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 7, Page Number: 5.13"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to print two dimensional array by row by row\n",
+ "\n",
+ "arr = [[0 for i in xrange(0,4)] for i in xrange(0,4)]\n",
+ "\n",
+ "for i in range(0,3):\n",
+ " for j in range(0,4):\n",
+ " arr[i][j] = (i * 4) + j + 1\n",
+ " \n",
+ " \n",
+ "print \"Printing array contents :\"\n",
+ "for i in range(0,3):\n",
+ " for j in range(0,4):\n",
+ " print \"%3d\" %arr[i][j],\n",
+ " print \"\\n\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Printing array contents :\n",
+ " 1 2 3 4 \n",
+ "\n",
+ " 5 6 7 8 \n",
+ "\n",
+ " 9 10 11 12 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 8, Page Number: 5.14"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to add and display the results of 5 nos\n",
+ "\n",
+ "#Function definition \n",
+ "def add(x,ar):\n",
+ " sum = 0\n",
+ " for i in range(0,5):\n",
+ " sum += ar[i]\n",
+ " print \"Sum is ... %d\" %sum \n",
+ " \n",
+ "a = []\n",
+ "n = 5\n",
+ "for i in range(0,5):\n",
+ " a.append(0)\n",
+ "print \"Enter 5 values\"\n",
+ "for i in range(0,5):\n",
+ " a[i] = input()\n",
+ "add(n,a) #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 5 values\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": [
+ "30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum is ... 150\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 9, Page Number: 5.15"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program for capture and store n values into array and print them\n",
+ "\n",
+ "#Function definition \n",
+ "def arr(a = []):\n",
+ " for i in range(0,5):\n",
+ " print \"Value in array %d\\n\" %a[i]\n",
+ " \n",
+ "a = []\n",
+ "for i in range(0,5):\n",
+ " a.append(0)\n",
+ "for i in range(0,5):\n",
+ " a[i] = i\n",
+ "arr(a) #Function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value in array 0\n",
+ "\n",
+ "Value in array 1\n",
+ "\n",
+ "Value in array 2\n",
+ "\n",
+ "Value in array 3\n",
+ "\n",
+ "Value in array 4\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 10, Page Number: 5.16"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to print the character string from the array\n",
+ "\n",
+ "name = \"LAK\"\n",
+ "print \"%s\" %name"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "LAK\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 11, Page Number: 5.17"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program for explaining the working of 4 dimensional array\n",
+ "\n",
+ "#Local definition\n",
+ "array_4d = [[[[0 for i in xrange(0,3)] for i in xrange(0,3)]for i in xrange(0,3)] for i in xrange(0,3)]\n",
+ "\n",
+ "#looping statements \n",
+ "for a in range(0,3):\n",
+ " for b in range(0,3):\n",
+ " for c in range(0,3):\n",
+ " for d in range(0,3):\n",
+ " array_4d[a][b][c][d] = a + b + c + d\n",
+ "#Result \n",
+ "for a in range(0,3) :\n",
+ " print \"\\n\"\n",
+ " for b in range(0,3):\n",
+ " for c in range(0,3):\n",
+ " for d in range(0,3):\n",
+ " print \"%3d\" %array_4d[a][b][c][d],\n",
+ " print \"\\n\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ " 0 1 2 \n",
+ "\n",
+ " 1 2 3 \n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 1 2 3 \n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ "\n",
+ "\n",
+ " 1 2 3 \n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ " 5 6 7 \n",
+ "\n",
+ "\n",
+ "\n",
+ " 2 3 4 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ " 3 4 5 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ " 5 6 7 \n",
+ "\n",
+ " 4 5 6 \n",
+ "\n",
+ " 5 6 7 \n",
+ "\n",
+ " 6 7 8 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file