{ "metadata": { "name": "", "signature": "sha256:86b94093ee72d30b0a9443a44a1c3905ef6186294337253a1ca56b0318def382" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 17 : Pointers with Multidimensional Array" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page No.:5.123" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print the values and addres of the array elements.\n", "\n", "arr=([[5,100],[10,200],[15,300],[20,400]])\n", "\n", "for a in range(0,4):\n", " print \"Address of %d array= %u\"%(a,id(arr[a]))\n", " for b in range(0,2):\n", " print \"Value =%d\"%(arr[a][b])\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of 0 array= 4336406328\n", "Value =5\n", "Value =100\n", "Address of 1 array= 4336414944\n", "Value =10\n", "Value =200\n", "Address of 2 array= 4336414800\n", "Value =15\n", "Value =300\n", "Address of 3 array= 4366918504\n", "Value =20\n", "Value =400\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No.: 5.125" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print the value and address of the element using array of pointers\n", "\n", "a=[0,1,2]\n", "b=10\n", "c=20\n", "d=30\n", "a[0]=b\n", "a[1]=c\n", "a[2]=d\n", "for i in range(0,3):\n", " print \"Address = %u\"%id(a[i])\n", " print \"Value = %d\" % (a[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address = 4298181184\n", "Value = 10\n", "Address = 4298180944\n", "Value = 20\n", "Address = 4298180704\n", "Value = 30\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page No.: 5.126 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to sort a list of strings in alphabetical order using array of pointers.\n", "\n", "a = []\n", "n= input(\"Enter the number of strings: \")\n", "print \"Enter each string on a separate line below.\"\n", "for i in range(0,n):\n", " a.append(0)\n", " \n", "for i in range(0,n):\n", " a[i]=raw_input(\"String %d: \" %(i+1))\n", "for i in range(0,n):\n", " for j in range(i,n):\n", " if a[i]>a[j]:\n", " t = a[i]\n", " a[i] = a[j]\n", " a[j] = t\n", "print \"\\nReordered List of Strings: \"\n", "for i in range(0,n):\n", " print \"String %d: %s\" %(i+1,a[i])\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of strings: 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter each string on a separate line below.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 1: venkat\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 2: ramesh\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 3: babu\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 4: muni\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 5: shankar\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 6: saravanan\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Reordered List of Strings: \n", "String 1: babu\n", "String 2: muni\n", "String 3: ramesh\n", "String 4: saravanan\n", "String 5: shankar\n", "String 6: venkat\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 4, Page No.: 5.128 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to count number of words\n", "\n", "str=[]\n", "ps=raw_input(\"Enter a string: \")\n", "print len(ps.split()), \"word(s) in the given string\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: I am a student.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4 word(s) in the given string\n" ] } ], "prompt_number": 27 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 5, Page No.: 5.129" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate on strings and pointers\n", "\n", "s1=\"abcd\"\n", "s2='efgh'\n", "\n", "print \"%s %16lu \\n\" % (s1,id(s1))\n", "print \"%s %16lu \\n\" % (s2,id(s2)) \n", "\n", "s1=s2\n", "print \"%s %16lu \\n\" % (s1,id(s1))\n", "print \"%s %16lu \\n\" % (s2,id(s2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "abcd 4363408224 \n", "\n", "efgh 4363332704 \n", "\n", "efgh 4363332704 \n", "\n", "efgh 4363332704 \n", "\n" ] } ], "prompt_number": 34 } ], "metadata": {} } ] }