{ "metadata": { "name": "", "signature": "sha256:d2add72dea4c498cf89927248000fdbfce0907704975d2bdb6ff1cde1c8f50a7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Chapter 16 : Pointers and Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page No.:5.118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print the value and address of the element\n", "\n", "a=[10,20,30,40,50]\n", "for b in range(0,5):\n", " print \"The value of a[%d]=%d\"%(b,a[b])\n", " print \"Address of a[%d]=%u\"%(b,id(a[b]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of a[0]=10\n", "Address of a[0]=4299197952\n", "The value of a[1]=20\n", "Address of a[1]=4299197712\n", "The value of a[2]=30\n", "Address of a[2]=4299197472\n", "The value of a[3]=40\n", "Address of a[3]=4299199208\n", "The value of a[4]=50\n", "Address of a[4]=4299198968\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No.: 5.119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to print the value and the address of the element using pointer with function\n", "\n", "a=[10,20,30,40,50]\n", "\n", "for c in range(0,5):\n", " print \"The Value of a[%d]=%d\"%(c,a[c])\n", " print \"The address of a[%d]=%u\"%(c,id(b))\n", "def value(b):\n", " b=''\n", " id(b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Value of a[0]=10\n", "The address of a[0]=4296527248\n", "The Value of a[1]=20\n", "The address of a[1]=4296527248\n", "The Value of a[2]=30\n", "The address of a[2]=4296527248\n", "The Value of a[3]=40\n", "The address of a[3]=4296527248\n", "The Value of a[4]=50\n", "The address of a[4]=4296527248\n" ] } ], "prompt_number": 58 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example:3, Page No,: 5.120" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to add the sum of number using pointer\n", "\n", "a=[10,20,30,40,50]\n", "\n", "b=total=0\n", "c=id(c)\n", "\n", "for b in range(0,5):\n", " print \"Enter the number %d: \"%(b+1)\n", " a[b]=input()\n", " \n", "c=a\n", "for b in range(0,5):\n", " total=total+c[b]\n", " c[b]=c[b]+1\n", "\n", "print \"Total= %d\"%(total)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number 1: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number 2: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number 3: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number 4: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "40\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number 5: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total= 150\n" ] } ], "prompt_number": 67 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 4, Page No.: 5.121 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to sort a given number using pointer.\n", "\n", "arr = []\n", "n = input(\"Enter the no. of elements: \")\n", "for i in range(0,n):\n", " arr.append(0)\n", "print \"Enter the element: \" \n", "for i in range(0,n):\n", " arr[i]=input()\n", "print \"Before Sorting:\" \n", "for i in range(0,n):\n", " print \"%d\"%arr[i],\n", " \n", "for i in range(0,n):\n", " for j in range(i,n):\n", " if arr[i]>arr[j]:\n", " t = arr[i]\n", " arr[i] = arr[j]\n", " arr[j] = t\n", "print \"\\nAfter Sorting:\"\n", "for i in range(0,n):\n", " print \"%d\" %arr[i]," ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the no. of elements: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the element: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Before Sorting:\n", "8 5 7 4 6 \n", "After Sorting:\n", "4 5 6 7 8\n" ] } ], "prompt_number": 72 } ], "metadata": {} } ] }